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