2014-09-18 13:26:06 8 Comments
I'm trying to work with the new RecyclerView
, but I could not find an example of a RecyclerView
with different types of rows/cardviews getting inflated.
With ListView
I override the getViewTypeCount
and getItemViewType
, for handling different types of rows.
Am I supposed to do it like the "old" way or should I do something with LayoutManager
? I was wondering if someone could point me to the right direction. Because I can only find examples with one type.
I want to have a list of slightly different cards. Or should I just use a scrollView
with cardViews
inside of it...make it without the adapter and recyclerView
?
Related Questions
Sponsored Content
44 Answered Questions
[SOLVED] RecyclerView onClick
- 2014-06-28 21:27:43
- CurtJRees
- 537798 View
- 547 Score
- 44 Answer
- Tags: android onclick android-recyclerview onclicklistener
17 Answered Questions
[SOLVED] How to create RecyclerView with multiple view type?
- 2014-10-07 20:55:30
- Pongpat
- 386692 View
- 796 Score
- 17 Answer
- Tags: android user-interface android-recyclerview
39 Answered Questions
[SOLVED] How to add dividers and spaces between items in RecyclerView?
- 2014-07-07 20:01:19
- EyesClear
- 557879 View
- 886 Score
- 39 Answer
- Tags: android android-recyclerview divider
33 Answered Questions
[SOLVED] What is the difference between "px", "dip", "dp" and "sp"?
- 2010-01-08 03:23:46
- capecrawler
- 1207257 View
- 5681 Score
- 33 Answer
- Tags: android android-layout user-interface dimension units-of-measurement
2 Answered Questions
Recyclerview inside Recyclerview not scrolling smooth
- 2016-07-27 10:13:16
- Anant Shah
- 1623 View
- 3 Score
- 2 Answer
- Tags: android android-recyclerview
14 Answered Questions
[SOLVED] RecyclerView vs. ListView
- 2014-11-04 05:57:27
- Blaze Tama
- 174454 View
- 271 Score
- 14 Answer
- Tags: android listview android-recyclerview
16 Answered Questions
[SOLVED] What is the difference between match_parent and fill_parent?
- 2011-04-23 03:31:10
- vnshetty
- 271290 View
- 1372 Score
- 16 Answer
- Tags: android android-layout
2 Answered Questions
[SOLVED] I want to load different layouts with fragments in recyclerview. is that possible?
- 2017-03-16 18:03:30
- praveen reddy
- 73 View
- 1 Score
- 2 Answer
- Tags: android android-recyclerview
10 comments
@Rohit Singh 2019-01-09 11:21:10
getItemViewType(int position) is the key
How to do it ?
You can create a
RecyclerView
with any number of different Views(ViewHolders). But for better readability lets take an example ofRecyclerView
with twoViewholders
.Remember these 3 simple steps and you will be good to go.
getItemViewType(int position)
ViewType
in onCreateViewHolder() methodPopulate View based on the itemViewType in
onBindViewHolder()
methodHere is a code snippet for you
GitHub Code:
Here is a project where I have implemented a RecyclerView with multiple ViewHolders.
@esQmo_ 2019-08-30 14:17:43
What about the same but having also multiple data set?
@Rohit Singh 2019-08-30 16:38:23
What do you mean? @esQmo_
@esQmo_ 2019-08-31 13:01:12
I mean what if every vieholder has also different dataset (data source)?
@kmfish 2017-04-25 09:49:08
You can use this library:
https://github.com/kmfish/MultiTypeListViewAdapter (written by me)
Setup adapter:
For each line item:
@Vitaly 2017-03-21 03:47:14
You can use the library: https://github.com/vivchar/RendererRecyclerViewAdapter
For each item, you should to implement a ViewRenderer, ViewHolder, SomeModel:
ViewHolder - it is a simple view holder of recycler view.
SomeModel - it is your model with
ItemModel
interfaceFor more details you can look documentations.
@Gil Moshayof 2014-09-21 14:15:50
Handling the rows / sections logic similar to iOS's UITableView is not as simple in Android as it is in iOS, however, when you use RecyclerView - the flexibility of what you can do is far greater.
In the end, it's all about how you figure out what type of view you're displaying in the Adapter. Once you got that figured out, it should be easy sailing (not really, but at least you'll have that sorted).
The Adapter exposes two methods which you should override:
This method's default implementation will always return 0, indicating that there is only 1 type of view. In your case, it is not so, and so you will need find a way to assert which row corresponds to which view type. Unlike iOS, which manages this for you with rows and sections, here you will have only one index to rely on, and you'll need to use your developer skills to know when a position correlates to a section header, and when it correlates to a normal row.
You need to override this method anyway, but usually people just ignore the viewType parameter. According to the view type, you'll need to inflate the correct layout resource and create your view holder accordingly. The RecyclerView will handle recycling different view types in a way which avoids clashing of different view types.
If you're planning on using a default LayoutManager, such as
LinearLayoutManager
, you should be good to go. If you're planning on making your own LayoutManager implementation, you'll need to work a bit harder. The only API you really have to work with isfindViewByPosition(int position)
which gives a given view at a certain position. Since you'll probably want to lay it out differently depending on what type this view is, you have a few options:Usually when using the ViewHolder pattern, you set the view's tag with the view holder. You could use this during runtime in the layout manager to find out what type the view is by adding a field in the view holder which expresses this.
Since you'll need a function which determines which position correlates to which view type, you might as well make this method globally accessible somehow (maybe a singleton class which manages the data?), and then you can simply query the same method according to the position.
Here's a code sample:
Here's a sample of how these view holders should look like:
Of course, this sample assumes you've constructed your data source (myData) in a way that makes it easy to implement an adapter in this way. As an example, I'll show you how I'd construct a data source which shows a list of names, and a header for every time the 1st letter of the name changes (assume the list is alphabetized) - similar to how a contacts list would look like:
This example comes to solve a fairly specific issue, but I hope this gives you a good overview on how to handle different row types in a recycler, and allows you make the necessary adaptations in your own code to fit your needs.
@sud007 2015-08-05 06:04:46
Beautifully explained! Usually we look for Code snippets but this gave much better understanding indeed. Kudos!
@Amt87 2016-08-13 21:45:48
Pretty awsome, and I think this is a great sample for solving such issue Sample Solution
@Ravindra Kushwaha 2016-10-19 13:39:06
Another example for infalting the different rows inside the recyelview that is :- stackoverflow.com/a/39972276/3946958
@Kyle 2016-12-25 12:41:58
Should be
names[i].substring(0, 1)
@Mahori 2017-04-24 04:40:09
Also, for recycler views with heterogeneous items, it is a good idea to look at SpanSizeLookup as well. stackoverflow.com/questions/26869312/…
@quangson91 2017-11-12 07:59:48
It is useful. Base on this answer I also have an idea to implement multi type view in Adapter by using enum. The enum will have method
onCreateViewHolder
that help us create view holder. For more detail you may want to check my share: stackoverflow.com/questions/47245398/…@duggu 2016-04-28 09:50:14
We can achieve multiple view on single RecyclerView from below way :-
Dependencies on Gradle so add below code:-
RecyclerView in XML
Activity Code
First XML
Second XML
Third XML
Now time to make adapter and this is main for showing different -2 view on same recycler view so please check this code focus fully :-
You can check also this link for more information.
@Rjz Satvara 2016-12-22 12:43:43
but that's works fine but when i am scrolling fast from top to bottom and vice versa i am getting some weird output... means data are not set proper,. what is its solution ?
@ticofab 2015-03-31 07:08:35
The trick is to create subclasses of ViewHolder and then cast them.
And then, at runtime do something like this:
Hope it helps.
@user1928896 2016-02-17 23:05:29
This is the direct answer to question. The only missing part is the need to override onCreateViewHolder(ViewGroup parent, int viewType) and handle different view types based on viewType
@Ravindra Kushwaha 2017-01-23 06:23:04
Another example for infalting the different rows inside the recyelview that is :- stackoverflow.com/questions/39971350/…
@Vahid Ghadiri 2017-09-15 21:35:32
Is there any generic solution instead of casting view holder base on switch case values ?
@yubaraj poudel 2015-08-28 09:17:26
It is quite tricky but that much hard, just copy the below code and you are done
enjoy !!!!
@Ankesh kumar Jaisansaria 2016-06-29 11:18:25
My Viewholder1 has layout named myLaout1.xml and has ScrollView in it. So now when i scroll this thing the recyclerview gets scrolled. How to scroll Viewholder1 's content
@iGio90 2014-11-17 09:46:26
According to Gil great answer I solved by Overriding the getItemViewType as explained by Gil. His answer is great and have to be marked as correct. In any case, I add the code to reach the score:
In your recycler adapter:
By doing this, you can set whatever custom layout for whatever row!
@Mark Martinsson 2014-12-08 12:09:25
Just a minor comment: The second parameter in onCreateViewHolder should be the viewType, not the index. According to API: developer.android.com/reference/android/support/v7/widget/…, int)
@Rjz Satvara 2016-12-22 12:39:35
but what about when user scrolling fast at that moment some weird output i am getting .
@김영호 2015-05-14 09:30:49
You can just return ItemViewType and use it. See below code:
@Amandeep Rohila 2015-03-25 12:40:22
You have to implement
getItemViewType()
method inRecyclerView.Adapter
. By defaultonCreateViewHolder(ViewGroup parent, int viewType)
implementationviewType
of this method returns0
. Firstly you need view type of the item at position for the purposes of view recycling and for that you have to overridegetItemViewType()
method in which you can passviewType
which will return your position of item. Code sample is given below