Issue
I am developing a chat application where there are users to select and chat with. Check the picture below-
As you can see the first user or List Item of the RecyclerView is "Cassie Ventura". But I want to show "Rachel Clark" on top because I chatted with her. (for example) That means I want to know how to put a specific list item on top. How to change the positions of the list items inside the RecyclerView? Whenever I chat with someone, his/her name should be on top and that's what I want to do. But I don't know how to do that. I tried ScrolltoPosition but it doesn't put the list item on top. How to do this specific task? Is there any function for it in the RecyclerView? Please let me know if you want to see any code snippet. Thank you.
Solution
What you are really trying to achieve is sorting the list of your RecyclerView.
Create a sorting algorithm (you can implement is with a Comparator) which sorts your list first by lastContactedDate descending, and after that by name ascending.
Collections.sort(chats, new Comparator<Pizza>() {
@Override
public int compare(Chat o1, Chat o2) {
int lastContactedDateCompResult = o1.getLastContactedDate.compareTo(p2.getLastContactedDate); // you need to check for null here.
if (lastContactedDateCompResult != 0) {
return lastContactedDateCompResult;
}
return o1.getName().compareTo(o2.getName());
}
});
Answered By - David Weber

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.