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.
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.
{ "messages" : { "pushed_id_1" : { "message_sender" : "AppleJuice", "message_text" : "Are you free?" }, "pushed_id_2" : { "message_sender" : "AppleJuice", "message_text" : "On Saturday I mean..." } } }
The FirebaseRecyclerAdapter would look like this.
FirebaseRecyclerAdapter<Message, MessageViewHolder> adapter = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(Message.class, R.layout.message_item, MessageViewHolder.class, myRef) { @Override protected void populateViewHolder(MyBookingsViewHolder viewHolder, Booking model, int position) { viewHolder.messageSender.setText(model.getMessage_sender()); viewHolder.messageText.setText(model.getMessage_text()); //put some code here to implement the feature that we need } }; messages_recycler_menu.setAdapter(adapter);
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:
if (TextUtils.isEqual(storedSender,model.getMessage_sender())){ viewHolder.messageSender.setVisiblity(View.GONE) } else{ // do your normal flow viewHolder.messageSender.setVisiblity(View.VISIBLE); storedSender = model.getMessage_sender(); }
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!