Handling different View Types in the FirebaseRecyclerAdapter
See original GitHub issueI’m working on a messaging app, and I think that overriding the original getItemViewType(int)
in order to handle different view types (eg. sent vs received text layouts) can complicate code with no clear benefit. You can also have a single layout file and hide/show different elements in the populateViewHolder(VH, int, int)
method, but I find it an even worse choice.
I’d like to change this, introducing a new constructor: The adapter will handle the possibility of having more than just one view model.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:11
- Comments:10 (3 by maintainers)
Top Results From Across the Web
FirebaseRecyclerAdapter and multiply item types on android
Your ViewHolder class will need to be able to handle both item types. Then in populateViewHolder you can populate the appropriate views ......
Read more >FirebaseUI for Realtime Database - Firebase Open Source
FirebaseUI offers two types of RecyclerView adapters for the Realtime Database: FirebaseRecyclerAdapter — binds a Query to a RecyclerView and responds to ...
Read more >FirebaseRecyclerAdapter using Recyclerview with Firebase ...
In this tutorial, we will learn how to use FirebaseRecyclerAdapter using Recyclerview with Firebase Realtime Database in 2020Link Assets: ...
Read more >Firebase Recycler Adapter onclick item listener - YouTube
Firebase Recycler Adapter onclick item listener - Handle item ... key android). so that we can display profile information of each user on ......
Read more >How to populate RecyclerView with Firebase data using ...
How to add RecyclerView in the android app and display the data in Firebase Realtime Database. Step 1: Add the following dependencies to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I see your point. Maybe a dedicated subclass might be a very good solution, in order to avoid unnecessary boilerplate.
My original idea was a constructor like this
in which I would set
mModelLayout = -1;
andmViewHolderClass = null;
In this way, I can change
onCreateViewHolder
andgetItemViewType
in order to handle both the functionalities. Something like this:I assume that the items in the list of layouts have the same index of the items in the list of
ViewHolder
s. And I also assume that the developers might want to add their own logic when deciding which view type to apply, depending on the data.However, a subclass might be a cleaner solution, using the same pattern applied by
onBindViewHolder
:A third approach could be seeing the multiple-views case as an extension of the particular case of a single view. In this sense, the single-view constructor could create a list with just a single item in it.
Edit: On a second thought, I think that instead of two lists, it would be better to just provide a map:
Map<Integer, VH>
.@samtstern I believe this issue can be closed since it falls under the category of “the developer can do this themselves fairly easily and implementing it would make things too complicated for users who just want a quick solution”.
@gwidaz @brad007 @Sottti I know this issue has been open for a while, but if any of you are still in need of a fix, here’s how you would do it:
If anyone wants to look at my source in a little bit more detail to get a better understanding of how to handle different view types, here’s the full
ScoutAdapter.java
and the package where I keep myViewHolder
s.