2015-12-22 85 views
6

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

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

而且如果会话中有很多消息,它就可以正常工作。但是,如果用户之间只有一条或两条消息,则RecyclerView会将它们显示在屏幕底部,并在其上方放置空白区域。

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

回答

3

我创造一切正常,并添加setStackFromEnd(true)setReverseLayout(true),并用这个方法下面链接列表设置为底部时,它有许多itens,该recyclerview会从底层做起,否则会跳投显示从顶部即使评论它比屏幕的大小还要小。

//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 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" /> 

enter image description here