I want to set a different color to the Items added in a Firebase Recycler view. I am using a Card View to display data. What I want is : suppose I already have some data in the list, and they will be having white background. Now suppose some new values are added in the list. I want the newly added items to have a different background tint(Say green) than the items already present. I am fetching data from Firebase. Now after I view the card details or after viewing the new data for first time,the color of these new cards should now be white because now they are old.It would be helpful if I the card retain their color until the card details are read (The card details are accessed by tapping the card which starts a new activity with detailed info).
Asked
Active
Viewed 755 times
0

KENdi
- 7,576
- 2
- 16
- 31

Joel Mathew
- 40
- 1
- 6
-
And did you try anything ?? – Adithya Jul 09 '17 at 16:08
-
No...actually I didn't know how to get started... @Adithya – Joel Mathew Jul 09 '17 at 17:05
1 Answers
0
You've to implement
getItemViewType
method ofAdapter
class like below@Override public int getItemViewType(int position) { // here i'm assuming that you've some method isNewItem(position) // which returns item type as integer and // ITEM_TYPE_NEW and ITEM_TYPE_OLD are declared as constants if (isNewItem(position)) { return ITEM_TYPE_NEW; } else { return ITEM_TYPE_OLD; } }
In method
onBindViewHolder
you should change background colour if it is of type new.@Override public void onBindViewHolder(ViewHolder holder, int position) { final int itemType = getItemViewType(position); if (itemType == ITEM_TYPE_NEW) { // here I'm assuming that there is setNewItemBackColor method // present in your adapter class which changes background color // of card view of ViewHolder ((YourViewHolderClassNameHere)holder).setNewItemBackColor(); } }
Refer this answer for more detailed implementation.

ELITE
- 5,815
- 3
- 19
- 29