Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the same RecyclerView Adapter for different layouts

I am developing an application which heavily relies on the usage of RecyclerView.

I really need to know how to use the same RecyclerView for different item layouts. An example of layouts are:

1) A list item with a name and description

2) A list item with an Image and a name

You can see that they are similar, but with slight changes to the layout.

Thanks in advance!

like image 574
TareK Khoury Avatar asked Jul 21 '15 11:07

TareK Khoury


Video Answer


1 Answers

Since the recyclerview item in xml doesn't depend on the kind of items that you will inflating in it, you can go ahead and use the same recycler view layout file for the three fragments.

As for the adapters, your lists seem to be homogeneous (i.e., single kind of view item). Its best that you use 3 different adapters for each case. You can customise the constructor, add custom helper methods for each kind of adapter as per your convenience.

Now if you have a heterogeneous list, you will have to override getItemViewType() in your adapter and use this appropriately in onCreateViewHolder() and onBindViewHolder()

Hope this helps! :)

like image 80
Awanish Raj Avatar answered Oct 11 '22 22:10

Awanish Raj