简体   繁体   中英

Recycler View with Chat like Behaivour

I'm using a recycler view to load different content. I want a chat like behavior as it is seen on chat apps. Items are arranged from top to bottom but when the element is large, newer messages are attached at the bottom and the user need not to scrool to the bottom.

I have tried:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_comments_recycler_view);
LinearLayoutManager manager = LinearLayoutManager(this);
manager.setStackFromEnd(true);          
manager.setReverseLayout(true);         
recyclerView.setLayoutManager(manager); 


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

Still not able to achieve the desired behavior. How do i fix this. Thanks.

Have you tried adding the following for when you add a new chat message view to the recycler?

recyclerView.scrollToPosition(messages.size()-1);

You would call this at the same time you added a new object to the recycler view representing a new message for the chat-like behavior

I am not exactly sure what results are you exactly getting currently and what is your remaining relevant code. Possibly you have forgotten to call notifyDataSetChanged() method for RecyclerView's Adapter to make the updated data visible in RecyclerView and get it refreshed.

Also there are relevant questions here How to scroll to the bottom of a RecyclerView? scrollToPosition doesn't work and how to keep RecyclerView always scroll bottom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM