如何在Android中对RecyclerView项目进行排序 [英] How to sort RecyclerView item in android
本文介绍了如何在Android中对RecyclerView项目进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个android聊天应用程序.当我调用我的api时,它会返回给我按 user_id排序的聊天列表..但是,我需要先通过 message_id 进行序列化,因为我想首先显示最后一条消息.是我的 onBindViewHolder 方法,我从中获取值.
I'm working on a android chatting application. When I called my api it returns me the chat list sorted by a user_id. But what I need to do is serialized by message_id as I want to show last message first.Here is my onBindViewHolder method in which i get the values.
public void onBindViewHolder(final MyAdapter_HomeViewHolder holder, final int position) {
holder.userNameTV.setText(data.get(position).getUserInfo().getFullName());
holder.msgBodyTV.setText(data.get(position).getBody());
holder.originator_iD.setText(data.get(position).getUserInfo().getId().toString());
//message ID. I need to serialize my recyclerView by this ID.biggest id should appear first.
holder.messageId.setText(data.get(position).getId().toString());
holder.owner_type_ET.setText("1");
holder.subject_ET.setText("Message");
}
如果您需要查看完整的代码,请 https://pastebin.com/Zxmq36Gn >
In case of you need to see the full code, https://pastebin.com/Zxmq36Gn
推荐答案
在将列表传递给适配器之前(在API调用之后和适配器notifydatasetchanged之前)尝试以下操作:
try this before passing your list to the adapter (after API call and before adapter notifydatasetchanged):
Collections.sort(data, new Comparator<CustomData>() {
@Override
public int compare(CustomData lhs, CustomData rhs) {
// -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
return lhs.getId() > rhs.getId() ? -1 : (lhs.customInt < rhs.customInt ) ? 1 : 0;
}
});
这篇关于如何在Android中对RecyclerView项目进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文