You can use the class to view the Recycler TouchListener.
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{ private ClickListener clicklistener; private GestureDetector gestureDetector; public RecyclerTouchListener(Context context, final RecyclerView recycleView, final 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=recycleView.findChildViewUnder(e.getX(),e.getY()); if(child!=null && clicklistener!=null){ clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child)); } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { View child=rv.findChildViewUnder(e.getX(),e.getY()); RecyclerView.ViewHolder viewHolder=rv.findContainingViewHolder(child); if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){ clicklistener.onClick(child,rv.getChildAdapterPosition(child),viewHolder); } return false; } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } } }
Implement interface
public interface ClickListener{ public void onClick(View view, int position,RecyclerView.ViewHolder viewHolder); public void onLongClick(View view, int position); }
And in your main activity (here I installed the Image resource, which you can install as you like, Anything)
Create an arraylist named "selected" as Integer and add the following code as OnClickListener
recyclerViewSchedule.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerViewSchedule, new ClickListener() { @Override public void onClick(View view, final int position, RecyclerView.ViewHolder v) { //Values are passing to activity & to fragment as well if(!selected.contains(position)) { selected.add(position); ((ScheduleAdapter.MyHolder) v).txtImage.setBackgroundColor(getResources().getColor(R.color.colorAccent)); }else { ((ScheduleAdapter.MyHolder) v).txtImage.setBackgroundColor(getResources().getColor(R.color.colorTransparent)); selected.remove(new Integer(position)); } Toast.makeText(ScheduleActivity.this, "Single Click on position :"+position, Toast.LENGTH_SHORT).show(); } Toast.makeText(ScheduleActivity.this, "Single Click on position :"+position, Toast.LENGTH_SHORT).show(); } @Override public void onLongClick(View view, int position) { Toast.makeText(ScheduleActivity.this, "Long press on position :"+position, Toast.LENGTH_LONG).show(); } }));
The txtImage specified here is a RecyclerAdapter, replace it with your view
Happy coding :-)