jlively
jlively

Reputation: 743

How to make multiple views in recyclerview item clickable?

I have this adapter class where I have an object with a list in it.

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private Context context;
private Shop shop;

public ProductAdapter(Context context, Shop shop) {
    this.context = context;
    this.shop = shop;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.product_row, parent, false);
    return new ProductViewHolder(v);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
    holder.tvProductTitle.setText(shop.products.get(position).getTitle());
    holder.tvProductDescription.setText(shop.products.get(position).getDescription());
    Glide.with(context)
            .load(shop.products.get(position).getImageUrl())
            .placeholder(android.R.drawable.ic_menu_upload_you_tube)
            .error(android.R.drawable.stat_notify_error)
            .diskCacheStrategy(DiskCacheStrategy.RESULT)
            .into(holder.ivProduct);
}

@Override
public int getItemCount() {
    if (shop.products != null) {
        return shop.products.size();
    }
    return 0;
}

public static class ProductViewHolder extends RecyclerView.ViewHolder {
    private TextView tvProductTitle, tvProductDescription;
    private ImageView ivProduct;

    public ProductViewHolder(View itemView) {
        super(itemView);
        tvProductTitle = (TextView) itemView.findViewById(R.id.tvProductTitle);
        tvProductDescription = (TextView) itemView.findViewById(R.id.tvProductDescription);
        ivProduct = (ImageView) itemView.findViewById(R.id.ivProduct);
    }
}

public interface ClickListener {
    void onClick(View v, int position);

    void onLongClick(View v, int position);
}

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
    private GestureDetector gestureDetector;
    private ProductAdapter.ClickListener clickListener;

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ProductAdapter.ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildAdapterPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

I use this adapter in my fragment where I can click an item from the recyclerview which will open another activity with the selected product and it's parametars. Instead of clicking the whole item, I would like to click on part of it (lets say imageview) which will do the same as it is doing now. But, I would also like to put a checkbox in the recyclerview item itself which will have different function. At the moment, I can't use an intent in the adapter class. The way it is set up now I can only click the whole item. How can I make it work like I explained above?

public class ProductsFragment extends android.support.v4.app.Fragment {
private RecyclerView rvProduct;
private RecyclerView.LayoutManager layoutManager;
private ProductAdapter productAdapter;

public static ProductsFragment newInstance(Bundle args) {
    ProductsFragment fragment = new ProductsFragment();
    fragment.setArguments(args);
    return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_products, container, false);
    rvProduct = (RecyclerView) v.findViewById(R.id.rvProduct);
    rvProduct.setHasFixedSize(true);
    layoutManager = new GridLayoutManager(getContext(), 2);
    rvProduct.setLayoutManager(layoutManager);
    Bundle bundle = getArguments();
    final Shop shop = (Shop) bundle.getSerializable("shop");
    productAdapter = new ProductAdapter(getContext(), shop);
    rvProduct.setAdapter(productAdapter);
    productAdapter.notifyDataSetChanged();

    rvProduct.addOnItemTouchListener(new ProductAdapter.RecyclerTouchListener(getContext(), rvProduct, new ProductAdapter.ClickListener() {
        @Override
        public void onClick(View v, int position) {
            Intent details = new Intent(getContext(), ProductDetailsActivity.class);
            details.putExtra("product", shop.products.get(position));
            startActivity(details);
        }

        @Override
        public void onLongClick(View v, int position) {

        }
    }));
    return v;
}
}

EDIT: I've managed to do this, please tell me if this is right and efficient way:

holder.tvProductTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, ProductDetailsActivity.class);
            i.putExtra("product", shop.products.get(position));
            context.startActivity(i);
        }
    });

Upvotes: 0

Views: 1583

Answers (2)

Prince Bansal
Prince Bansal

Reputation: 1655

The way you doing it is not correct. As your onBindViewHolder will be called repeatedly as you scroll through the list, every time a new listener will be set.

Instead you should set the listeners in the ViewHolder class of Adapter. And use getAdapterPosition() method to get the position of item clicked. For example:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {     
private Context context; private Shop shop;
 public ProductAdapter(Context context, Shop shop) { 
this.context = context;
 this.shop = shop;
 } 
@Override public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
 View v = inflater.inflate(R.layout.product_row, parent, false);
 return new ProductViewHolder(v); 
}   
@Override public void onBindViewHolder(ProductViewHolder holder, int position) { 
holder.tvProductTitle.setText(shop.products.get(position).getTitle());
 holder.tvProductDescription.setText(shop.products.get(position).getDescription()); 
Glide.with(context) .load(shop.products.get(position).getImageUrl()) .placeholder(android.R.drawable.ic_menu_upload_you_tube) .error(android.R.drawable.stat_notify_error) .diskCacheStrategy(DiskCacheStrategy.RESULT) .into(holder.ivProduct); 
} 
@Override public int getItemCount() 
{ 
   if (shop.products != null) {
     return shop.products.size();
    } return 0;
 } 

public static class ProductViewHolder extends RecyclerView.ViewHolder {
 private TextView tvProductTitle, tvProductDescription;
 private ImageView ivProduct; 
public ProductViewHolder(View itemView)
 { 
    super(itemView); 
    tvProductTitle = (TextView) itemView.findViewById(R.id.tvProductTitle);
  tvProductDescription = (TextView) itemView.findViewById(R.id.tvProductDescription); 
ivProduct = (ImageView) itemView.findViewById(R.id.ivProduct); 
// Set onclickListener on image
ivProduct.setOnCliCkListener(new onClickListener(){
   @Override
    public void onClick(View v){
     Intent i = new Intent(context, ProductDetailsActivity.class);    
i.putExtra("product",    
shop.products.get(getAdapterPosition()));
 context.startActivity(i);
}
}
} 
 } 
public interface ClickListener { 
 void onClick(View v, int position);
  void onLongClick(View v, int position); 
}
   }

Upvotes: 1

Al Wld
Al Wld

Reputation: 939

You can add click listeners to each respective view of the inflated item in public onBindViewHolder. Did you consider that?

Edit: Ok it's what you did. I don't know about the efficiency though, but this is the way I would do it.

Upvotes: 0

Related Questions