Start a Project

applying touch listener on recycler view so that you can perform the click on whole item view

While working with the recycler view , we often feel the need to implement a listener on the whole recycler view so  that if any one item is selected we may trigger the action as per our need.

One way is to perform the same action on click of every element of the itemView in Recycler view’s onBindViewHolder function. But then that redundant, repetitive and also not at all  good approach.

Another is to add the addOnItemTouchListener to your recycler view.
But problem with this is that you need to override the three methods viz :

  1. onInterceptTouchEvent
  2. onTouchEvent
  3. onRequestDisallowInterceptTouchEvent

and implementing this is not very easy, as two of these take motionevents as input and you need to know how to handle this or you can convert these to something that most of the listeners have(like onClick) and use them as per your need .

So lets begin the coding :

  1. Make a class that extends RecyclerView.OnItemTouchListener
  2. Make a interface in this class.
  3. Override the methods as shown in the code snippet.
  4. Use an instance of this class while adding addOnItemTouchListener to your recycler view.

CODE : for the class

Now with this being done , all you need to do to your recycler view is to set an instance of this class.
take a look at how to do the same.

 

Exit mobile version