我在一個片段中製作了水平回收站。現在,當我點擊任何項目時,我看不到點擊監聽器正在工作。這裏是我的適配器類代碼:
RecyclerView OnClick不能正常工作
public class FeaturedProductsAdapter extends RecyclerView.Adapter<FeaturedProductsAdapter.CustomViewHolder> {
private List<FeaturedProductInfo> feedItemList;
private Context mContext;
public FeaturedProductsAdapter(Context context, List<FeaturedProductInfo> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView imageView;
protected TextView textView,priceView;
private Context context;
public CustomViewHolder(View view,Context context) {
super(view);
this.context=context;
this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
this.textView = (TextView) view.findViewById(R.id.prodTitle);
this.priceView = (TextView) view.findViewById(R.id.prodPrice);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getLayoutPosition(); // gets item position
Log.e("Check", position + "");
FeaturedProductInfo user = feedItemList.get(position);//[position];
// We can access the data within the views
Intent intent = new Intent(context, ProductDescription.class);
intent.putExtra("id", user.getId());
mContext.startActivity(intent);
}
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.featured_product_list_item_card, null);
Context context = viewGroup.getContext();
CustomViewHolder viewHolder = new CustomViewHolder(view,context);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeaturedProductInfo feedItem = feedItemList.get(i);
//Download image using picasso library
if(!feedItem.getUrl().contains("."))
{
feedItem.setUrl("nothing");
}
Picasso.with(mContext).load(feedItem.getUrl())
.error(R.drawable.unavailable)
.placeholder(R.drawable.unavailable)
.resize(110,110)
.into(customViewHolder.imageView);
//Setting text view title
customViewHolder.textView.setText(feedItem.getTitle());
customViewHolder.priceView.setText(feedItem.getPrice());
//Log.e("Featured: ","SET");
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
}
我覺得我沒有得到如何正確使用的觀點持有者。雖然我在其他活動中使用了與recyclerView相同的代碼,但它的作用與魅力相似。
它是不是在所有的射擊或它只是正確的射擊。您應該在bindViewHolder()期間設置onClick,因爲您只在初始時創建視圖保持器,然後在滾動列表時「回收」它。 – Helix
@Helix它根本沒有發射。 –
未設置視圖的背景嗎?也爲調試添加敬酒。 – Helix