Skip to content
Advertisement

Handling positions in FirebaseRecyclerAdapter

I have fiddled with the FirebaseRecyclerAdapter for quite some time now. It’s really a great tool to populate a custom list/recycler view very fast with all of its features. However, one thing that I would like to ask is how to handle positions of items inside the adapter itself.

So for example, I want to mimic this small feature that WhatsApp has in their chats.

enter image description here

So, in a group chat setting, if a person sends more than one consecutive message in a row, the display name of that particular person will be invisible.

The logic behind it according to my understanding: if the person who sends the message is the same for (position – 1), then I will just make the EditText invisible for (position). This is, of course, to prevent a very long stream of text with minimum amounts of repetitive information.

Let’s say the JSON tree from Firebase database is as follows.

JavaScript

The FirebaseRecyclerAdapter would look like this.

JavaScript

The furthest I have gone is to use getItemCount() method in the FirebaseRecyclerAdapter, but I am still unable to achieve the feature that mimics that of Whatsapp’s that I was talking about previously.

Is there a method that can achieve this? Or am I missing something very important in this example?

Advertisement

Answer

As discussed in comments:

Let’s suppose I received message and stored sender’s name in constant String that should be static constant in some class i.e. AppConstants so that It can be accessed everywhere therefore after that:

in populateViewHolder or in your message receiver do something like this:

JavaScript

In this way automatically the last message’s sender’s name will be updated , this is exactly what you were trying to achieve by adapter position!

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