简体   繁体   English

如何将最后一个文档添加到recyclerview的底部?

[英]How to put the last document added to the bottom of recyclerview?

Basically I want to align the messages as they are supposed to be in the usual chat app. 基本上,我想对齐消息,因为它们应该在通常的聊天应用程序中。

Now messages are aligned properly in the RecyclerView . 现在,消息在RecyclerView已正确对齐。 But whenever I send the new message it puts them on the top of the other messages. 但是,每当我发送新消息时,它将它们置于其他消息的顶部。 And whenever I go back and come again to that activity messages are arranged properly(even the top ones). 每当我回头再来一次,活动消息都会被正确安排(甚至是最上面的消息)。

I just want the message which I send to show up at the bottom. 我只希望我发送的消息显示在底部。 Any help will be appreciated 任何帮助将不胜感激

mLinearLayout.setReverseLayout(true);

Then: 然后:

private void loadmessage() {
mFirestore.collection("Users").orderBy("Timestamp", Query.Direction.DESCENDING).limit(10).addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot snapshots,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w("TAG", "listen:error", e);
            return;
        }

        for (DocumentChange dc : snapshots.getDocumentChanges()) {
            switch (dc.getType()) {
                case ADDED:
                    Message message = dc.getDocument().toObject(Message.class);
                    messageList.add(message);
                    mAdapter.notifyDataSetChanged();
                    mMessagesList.scrollToPosition(messageList.size()-10);
                    break;
            }
        }
    }
});

} }

To solve this, you need to change your query like this: 为了解决这个问题,您需要像这样更改查询:

mFirestore.collection("Users")
    .orderBy("Timestamp", Query.Direction.DESCENDING)
    .limit(10)
    .addSnapshotListener(/* ... */);

And your RecyclerView should look like this: 而且您的RecyclerView应该看起来像这样:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

This approch will reverse you the order as you want. 该方法将根据您的需要将订单撤消。

Edit: 编辑:

You can verride the getItem() method to invert the item lookup like this: 您可以遍历getItem()方法来像这样反转项目查找:

@Override
public HashMap getItem(int pos) {
    return super.getItem(getCount() - 1 - pos);
}

Try this... 尝试这个...

 recyclerView.scrollToPosition(messageList.size()-1);
 mAdapter.notifyDataSetChanged();

Here is the same case scenario and it has the answer too. 是相同的情况,它也有答案。 So in case anyone looking for the answer. 因此,以防万一有人在寻找答案。 Alex has stated various ways to solve this type of condition. 亚历克斯(Alex)提出了解决这种情况的各种方法。

And This is the github repository for the features of the firestore chat app. 是用于Firestore聊天应用程序功能的github存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM