I have made a horizontal recyclerview inside a fragment. Now when I click on any item I don't see the on click listener working. Here is my code for the Adapter class:
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);
}
}
I think I am not getting how to use the view holder properly. While I have used the same code for recyclerView in another activities and it works like charm.