You can set click listener inside adapter class and also you can use RecyclerView click listener class.
If you use RecyclerView custom click than you have to use this class and use this listener in activity or fragment wherever you need. this is the class and you have to keep in util package.
public class RecyclerOnClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerOnClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = 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 && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
and after this, you can use this class by calling a method like this, here is the code.
private void recycleClick() {
product_recycleview.addOnItemTouchListener(
new RecyclerOnClickListener(ProductListActivity.this, product_recycleview, new RecyclerOnClickListener.OnItemClickListener() { @Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(ProductListActivity.this, ProductDetailsActivity.class); intent.putExtra("data", detailList.get(position));
startActivity(intent);
overridePendingTransition(R.anim.slide_in_left, R.anim.fade_out);
}
@Override
public void onLongItemClick(View view, int position) {
}
}));
}
you can send data by intent to next activity and another solution is this, you can implement click listener on items of ViewHolder class and you can manage click inside adapter class like this, below code
itemRowHolder.lnr_layout_package.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, SinglePackageShowOnlyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra("list", dataList.get(i).getSingleItemModels());
intent.putExtra("title", model.getType());
intent.putExtra("image", model.getImages());
intent.putExtra("service_name", service_title);
intent.putExtra("pack_name",package_title);
mContext.getApplicationContext().startActivity(intent);
}
});
remember if you can set click on an individual item then you have to prefer to click in adapter class and if you can set individual click listener inside an adapter then you have to put it in onBindView class