如何创建类似消息堆栈的whatsapp(首先是所有聊天屏幕)

时间:2019-04-04 13:09:13

标签: android android-recyclerview android-adapter

我正在开发Android聊天应用程序,其中我想像whatsapp一样在recyclerview的顶部显示新到达的消息块。 whatsapp在顶部显示新消息,并将所有其他聊天块堆栈向下推。

我所做的事情能够将新的消息聊天块显示在顶部,但无法将其他聊天块推下。

这是我的代码:

int readindex = 0;

for (int i = 0; i < messageStackDtoList.size(); i++) {
    Log.d("iiiiiiiiii",messageStackDtoList.get(i).getMessageId());
    Log.d("iiiiiiiiii",latestObject.getString("messageId"));
    if (messageStackDtoList.get(i).getMessageId().equals(latestObject.getString("messageId"))) {

        readindex = i;
        Toast.makeText(getActivity(),messageStackDtoList.get(i).getMessage(),Toast.LENGTH_SHORT).show();
        break;
    }
}



messageStackDtoList.set(readindex, new MessageStackDto(latestObject.getString("threadId"), latestObject.getString("messageId"), latestObject.getString("sender"), latestObject.getString("senderId"), latestObject.getString("receiverId"), latestObject.getInt("messageStatus"), latestObject.getString("receiver"), latestObject.getString("message"), latestObject.getString("type"), latestObject.getString("audioUrl"), latestObject.getString("date"), outputData.getInt("newMsgCount"), null));

messageStackAdapter.notifyItemChanged(readindex);

Screenshot

这里计数为11的块是新的消息堆栈隐藏/覆盖其他用户聊天,而计数8以下的消息块是旧的。 其他聊天块将替换为具有11个计数的新聊天块。 我尽了所有可能,但没有结果。

1 个答案:

答案 0 :(得分:1)

try {

for (int i = 0; i < messageStackDtoList.size(); i++) {
Log.d("From message list::::::", messageStackDtoList.get(i).getThreadId());
Log.d("new data::::::::", outputData.getString("threadID"));
if (messageStackDtoList.get(i).getThreadId().equals(outputData.getString("threadID"))) {


    Log.d("Contact already present", messageStackDtoList.get(i).getMessage() + "");

    messageStackDtoList.set(i, new MessageStackDto(latestObject.getString("threadId"), latestObject.getString("messageId"), latestObject.getString("sender"), latestObject.getString("senderId"), latestObject.getString("receiverId"), latestObject.getInt("messageStatus"), latestObject.getString("receiver"), latestObject.getString("message"), latestObject.getString("type"), latestObject.getString("audioUrl"), latestObject.getString("date"), outputData.getInt("newMsgCount"), null));
    messageStackAdapter.notifyItemChanged(i);
    messageStackDtoList.sort(new CustomComparator());
    messageStackAdapter.notifyDataSetChanged();
    break;
}
 }

for (MessageStackDto messageStackDto : messageStackDtoList) {
if (!messageStackDto.getThreadId().equals(outputData.getString("threadID"))) {
    Log.d("I","Not Present");
    isNewContact = false;
} else {
    Log.d("I","Present");
    isNewContact=true;
    break;
}
                                            }

if(!isNewContact){
MessageStackDto audioMessageStackDto = new MessageStackDto(latestObject.getString("threadId"), latestObject.getString("messageId"), latestObject.getString("sender"), latestObject.getString("senderId"), latestObject.getString("receiverId"), latestObject.getInt("messageStatus"), latestObject.getString("receiver"), latestObject.getString("message"), latestObject.getString("type"), latestObject.getString("audioUrl"), latestObject.getString("date"), outputData.getInt("newMsgCount"), null);
messageStackDtoList.add(audioMessageStackDto);
messageStackDtoList.sort(new CustomComparator());
messageStackAdapter.notifyDataSetChanged();
 }


 } catch (IndexOutOfBoundsException e) {    e.printStackTrace();

} catch (Exception e) {
   e.printStackTrace();
 }

这是我的比较器类:

package in.demo.androidchatdemo.Comparator;

import java.util.Comparator;

import in.demo.androidchatdemo.Dto.MessageStackDto;


public class CustomComparator implements Comparator<MessageStackDto> {

@Override


public int compare(MessageStackDto o1, MessageStackDto o2) {

    try {

//    System.out.println(o1.getLatestMsg().get(0).getDate());

        return o1.getDate().compareTo(o2.getDate()) * (-1);

    } catch (Exception e) {
        e.printStackTrace();
    }


   // System.out.println(o1.getNewMessages().get(0).getDate().compareTo(o2.getNewMessages().get(0).getDate()));

    return o1.getDate().compareTo(o2.getDate());
}


}