Skip to content
Advertisement

how to handle multiple views in RecyclerView adapter

the problem that I implement a ReyclerView with a custom ItemClickListener inside the adapter:

JavaScript

and here is the whole code (for Adapter):

JavaScript

so the problem that when I have overridden the ItemClickListener inside the activity to control the views inside the list-item( I will attach the code of the activity and the layout below):

MainActivity.java

JavaScript

list_item.xml

JavaScript

the problem when I clicked the ImageView (favIcon), it’s not enabled directly! which the onItemClick for(ItemClickListener) interface executed first, then the onClick for the nested onClickListener(favIcon) executed -I have to make a double click to enable the red favIcon, so there is a way that I can control multiple views inside the list_item without using nested Listener?

here is a snapshot of my app:

enter image description here

the result after double click

enter image description here

Advertisement

Answer

You can use your ItemClickListener to get what is being clicked and perform action related to it. Just changed your ItemClickListener to following:

JavaScript

Now add click listener to all the views you require for your item. Eg. for you favIcon you can send type=fav, for your download icon type=download, etc.

JavaScript

Now just use switch case using type and handle your click.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement