RecyclerView for chat app

前端 未结 1 685
鱼传尺愫
鱼传尺愫 2021-01-02 07:13

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

相关标签:
1条回答
  • 2021-01-02 07:56

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

    0 讨论(0)
提交回复
热议问题