gpt4 book ai didi

android - 用于聊天应用的 RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:47 24 4
gpt4 key购买 nike

我正在构建一个聊天应用程序并使用 RecyclerView 显示消息。由于这是一个聊天应用程序,最后的消息应该出现在列表的底部。为此,我通过以下方式使用 LinearManager:

    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setStackFromEnd(true);
layoutManager.setSmoothScrollbarEnabled(false);

如果对话中有很多消息,它也能正常工作。但是,如果用户之间只有一两条消息,RecyclerView 会将它们显示在屏幕底部并在它们上方放置空白。

在这种情况下是否可以在屏幕顶部显示回收站元素?

最佳答案

我创建一切正常并添加了setStackFromEnd(true), setReverseLayout(true),并使用下面的方法将列表设置为底部,当它有很多元素时, recyclerview 将从底部开始,否则它将远离顶部显示评论,即使它的 itens 小于屏幕大小也是如此。

//method will set the recyclerview to the end, in other words its going to the 
//end of the recyclerview, starting from the bottom.
//call scrollToBottom() after add your items in the adapter
public void scrollToBottom(){
recyclerView.scrollVerticallyTo(0);
}

回收 View Java

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_comments_recycler_view);
LinearLayoutManager manager = LinearLayoutManager(this);
manager.setStackFromEnd(true);
manager.setReverseLayout(true);
recyclerView.setLayoutManager(manager);

回收器 View XML

<RecyclerView
android:id="@+id/activity_comments_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

enter image description here

关于android - 用于聊天应用的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com