RecyclerView for chat应用 [英] RecyclerView for chat app

查看:74
本文介绍了RecyclerView for chat应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a chat app and present messages using RecyclerView. Since this a chat app, last messages should appear at the bottom of the list. To achieve this I use LinearManager in the following way:

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

如果对话中有很多消息,它会很好地工作.但是,如果用户之间只有一则或两则消息,则RecyclerView会将其显示在屏幕底部,并在其上方放置空白.

And it works fine if there are many messages in conversation. However, if there are only one or two messages between users, RecyclerView displays them at the bottom of the screen and puts white space above them.

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

Is it possible to display recycler items at the top of the screen in this case?

推荐答案

我创建了所有正常内容,并添加了setStackFromEnd(true)setReverseLayout(true),并使用此方法在列表中有很多标签时将列表设置为最底端,将从底部开始,否则即使顶部的大小小于屏幕大小,也会从顶部显示注释.

I create everything normal and added setStackFromEnd(true), setReverseLayout(true), and use this method bellow to set list to the bottom when it have many itens, the recyclerview will start from the bottom, otherwise it will aways show comments from the top even if it have less itens then the size of the screen.

//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);
}

RecyclerView Java

RecyclerView Java

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 XML

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

这篇关于RecyclerView for chat应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆