我正在android中进行基于XMPP的聊天。.当我需要将new.message到达时,需要将列表视图中项目的位置更新到顶部时,我感到很震惊。
用例是..我在应用程序的“联系人”屏幕上,并且出现了一条新消息..因此此联系人应移至列表顶部并加粗。这也和whatsapp类似
如何才能做到这一点。我的班级imolemebts活动,我已经实现了自定义列表适配器。
那么我如何找到列表视图中是否存在项目,其次如何动态更改位置
这就是我解决的方法
private void moveMessageToTop(MessageObject message) {
int index = 0;
for (Friends friend : mFriends) {
if (friend.getName().equalsIgnoreCase(message.getFrom().split("@")[0])) {
index = mFriends.indexOf(friend);
break;
}
}
if (index != 0) {
mFriends.add(0,new Friends(message.getFrom().split("@")[0], message
.getMessage()));
} else {
Friends frnd = mFriends.get(index);
frnd.setStatus(message.getMessage());
mFriends.add(0, frnd);
mFriends.remove(index);
}
((ListAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句