recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
this,
recyclerView,
new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
switch (position){
case 0:
Toast.makeText(MainActivity.this, "item-1 Clicked", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "item-2 Clicked", Toast.LENGTH_SHORT).show();
break;
default:
}
}
@Override
public void onLongItemClick(View view, int position) {
switch (position){
case 0:
Toast.makeText(MainActivity.this, "Get 10% Discount", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "Get 20% Discount", Toast.LENGTH_SHORT).show();
break;
default:
}
}
}
));
}
This code is best while I have only two or up to 5 items in my recycler view. But what happens if I was thousands of items inside my recycler view then how can I use the onClickItem or onLongItemClick, because using switch statement will be the worst case.
implement an interface inside the adapter of you RecycleView
Something like:
Then Implement the onClick in your Activity class.