我正在构建一个聊天应用程序并使用 RecyclerView 呈现消息。由于这是一个聊天应用程序,因此最后一条消息应显示在列表底部。为了实现这一点,我通过以下方式使用 LinearManager:
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setStackFromEnd(true);
layoutManager.setSmoothScrollbarEnabled(false);
如果对话中有很多消息,它就可以正常工作。但是,如果用户之间只有一两条消息,RecyclerView 会将它们显示在屏幕底部,并在其上方放置空白。
在这种情况下,是否可以在屏幕顶部显示回收项目?
我创建一切正常并添加setStackFromEnd(true)
, setReverseLayout(true)
,并使用下面的方法将列表设置为底部,当它有很多itens时,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);
}
Java RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_comments_recycler_view);
LinearLayoutManager manager = LinearLayoutManager(this);
manager.setStackFromEnd(true);
manager.setReverseLayout(true);
recyclerView.setLayoutManager(manager);
RecyclerView XML
<RecyclerView
android:id="@+id/activity_comments_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)