为什么单击对回收器不起作用查看项目



我有一个简单的RecyclerView,它有一个链接到活动的接口。问题是指需要双击recyclerView中的项目才能执行操作

这是AllProductsAdapter代码:

public class AllProductsAdapter extends RecyclerView.Adapter<AllProductsAdapter.ViewHolder> {
RecyclerViewClickInterfaceNew recyclerViewClickInterface;
List<ProductsModel> productsModelList;
Context context;
public AllProductsAdapter(List<ProductsModel> productsModelList, Context context, RecyclerViewClickInterfaceNew recyclerViewClickInterface) {
this.productsModelList = productsModelList;
this.context = context;
this.recyclerViewClickInterface = recyclerViewClickInterface;
}
@NonNull
@Override
public AllProductsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.product_items, parent, false);
return new AllProductsAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AllProductsAdapter.ViewHolder holder, int position) {

ProductsModel productsModel = productsModelList.get(position);
PicassoTrustAll.getInstance(context)
.load(productsModel.getProduct_image())
.into(holder.catImage);
}
@Override
public int getItemCount() {
return productsModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView catImage;
TextView catName;
CardView categoryCard;
public ViewHolder(@NonNull View itemView) {
super(itemView);
catImage = itemView.findViewById(R.id.catImage);
catName = itemView.findViewById(R.id.catName);
categoryCard = itemView.findViewById(R.id.categoryCard);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recyclerViewClickInterface != null)
recyclerViewClickInterface.onItemClick(v, getAdapterPosition());
}
});
}
}

这是我的RecyclerViewClickInterfaceNew:

public interface RecyclerViewClickInterfaceNew {
void onItemClick(View view , int position);
void onLongItemClick(int position);
}

这是我的主要活动代码:

@Override
public void onItemClick(View view , int position) {
ImageView img= view.findViewById(R.id.catImage);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AllProductsList.this, "Imgclciked", Toast.LENGTH_SHORT).show();
}
});
}

在MainActivity中,Toast shows after clicking 2 times on the Image.如何将其修复为正常的单击?请告诉我你的答案。

从此代码

@Override
public void onItemClick(View view , int position) {
//when different item is FIRST clicked, a new instance of catImage is produced 
ImageView img= view.findViewById(R.id.catImage);
//the img onClick is set here to require a second click
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(AllProductsList.this, "Imgclciked", Toast.LENGTH_SHORT).show();
}
});
}

对于Recycler View上的每个新项目,您都会重置点击处理。

我建议您在ViewHolder上设置onClickListener。这不是最佳实践,但它足以满足您的代码

我认为这是因为您使用的是NestedScrollView,你应该把这行添加到你的回收查看

android:nestedScrollingEnabled="false"

相关内容

  • 没有找到相关文章

最新更新