1. 程式人生 > >How To Handle Click Events In Android RecyclerViews

How To Handle Click Events In Android RecyclerViews

According to the documentation, a RecyclerView is a flexible view for providing a limited window into a large data set. If you have done any android development before, you definitely know the popular ListView class used to display lists of things. After its release by Google however, most of us have moved on and now use RecyclerViews

. In this post, I want to show you how to handle click events like regular touch event and also long click in recyclerviews because you cannot just set OnClickListeners like you would for a ListView.

recyclerview

How I do RecyclerViews Click Events

In a given project, I am sure there will be more than one instances where I will need to use recyclerviews and so I will need to handle click events! So, to have a solution that can be reused, I normally create a class:

RecyclerItemClickListener.java

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 publicclassRecyclerItemClickListener implementsRecyclerView.OnItemTouchListener{privateGestureDetector mGestureDetector;privateOnItemClickListener mListener;publicinterfaceOnItemClickListener{voidonItemClick(View view,intposition);voidonItemLongClick(View view,intposition);}publicRecyclerItemClickListener(Context context,finalRecyclerView recyclerView,OnItemClickListener listener){mListener=listener;mGestureDetector=newGestureDetector(context,newGestureDetector.SimpleOnGestureListener(){@OverridepublicbooleanonSingleTapUp(MotionEvente){returntrue;}@OverridepublicvoidonLongPress(MotionEvente){View childView=recyclerView.findChildViewUnder(e.getX(),e.getY());if(childView!=null&&mListener!=null){mListener.onItemLongClick(childView,recyclerView.getChildAdapterPosition(childView));}}});}@OverridepublicbooleanonInterceptTouchEvent(RecyclerView view,MotionEvente){View childView=view.findChildViewUnder(e.getX(),e.getY());if(childView!=null&&mListener!=null&&mGestureDetector.onTouchEvent(e)){mListener.onItemClick(childView,view.getChildAdapterPosition(childView));}returnfalse;}@OverridepublicvoidonTouchEvent(RecyclerView view,MotionEvent motionEvent){}@OverridepublicvoidonRequestDisallowInterceptTouchEvent(booleandisallowIntercept){}}

Now, we are ready to implement this code in our recyclerview reference; could be inside your fragment or activity! Here is how you do that:

12345678910111213141516 mRecyclerView.addOnItemTouchListener(newRecyclerItemClickListener(this,mRecyclerView,newRecyclerItemClickListener.OnItemClickListener(){@OverridepublicvoidonItemClick(View view,intposition){//handle click events here}@OverridepublicvoidonItemLongClick(View view,intposition){//handle longClick if any}}));

RECYCLERVIEWS SUMMARY

So now that we have a reusable class, you can listen for clicks on those recyclerviews and respond appropriately since you have the position handed to you by the interface!

I hope you found this post helpful and please consider sharing and subscribing for future posts from yours truly!

Meanwhile, happy coding my people!