2

I have read many posts about implementing an OnItemClickListener in the RecyclerView, but the more I read, the more I get confused. It seems that we have two ways to implement the OnItemClickListener:

  1. Adding setOnClickListener inside the adapter as shown here
  2. Implementing RecyclerView.OnItemTouchListener as shown here

As I read the posts I figure out that the first method is better and has more features than the second method. For example, there's item click support. What is the benefit of the second method? Why and whe should I use it? Any suggestions?

Community
  • 1
  • 1

1 Answers1

1

An OnItemTouchListener functions a bit differently than the normal OnItemClickListener. Using the OnItemTouchListener, it is possible to allow the application to intercept touch events from the View hierarchy. What this basically means is that you can implement various forms of gesture manipulation like swipe straight into the Views of your RecyclerView.

When should you use it?
An OnItemClickListener should be used when you need to determine what happens when the user clicks on a View in your RecyclerView. This could be deleting something or starting up a new activity. The OnItemTouchListener Is generally used to create gestural interactivity to certain Views in your RecyclerView.

If you want to implement an OnItemTouchListener into your RecyclerView, you will need to determine the MotionEvent that you're going to use. For more information, I suggest you read more about OnItemTouchListener from the Android Developers site.

Razor
  • 1,778
  • 4
  • 19
  • 36