Y
Y
ymi2015-12-22 15:04:26
Android
ymi, 2015-12-22 15:04:26

Implementing click listener in RecyclerView?

Good afternoon! Very often in the examples I see the click processing logic directly in the RecyclerView adapter, and in connection with this, the question arose - shouldn't the adapter and logic be separated? After all, it is possible to hang a listener on the view in the adapter, and write the processing in another class? However, I often see that the logic is written directly in the adapter. How common is this practice in real projects, are there any conventions or best practices in this regard?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Perelygin, 2015-12-22
@orcDamnar

Necessarily. We put RecyclerView.OnItemsTouchListener in it, override onInterceptTouchEvent () - if it is necessary to process for the incoming event - then we call the onTouchEvent () method of the previously created GestureDetector with the GestureListener implemented and installed, in which we process the touch we need. The only way.

T
Tiberal, 2015-12-22
@Tiberal

In the ViewHolder heir to the view that is passed to the constructor, hang a listener (on the root one or not) in the simplest and most logical way.

O
Oleg Gamega, 2015-12-22
@gadfi

I like to use EventBus for this, you can pass the interface through the constructor to anyone as you like and depending on the situation

A
Artemych, 2015-12-26
@Artemych

Definitely separate logic and GUI.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question