Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent recyclerview auto scroll to bottom when insert new items?

I am using RecyclerView to view my data, but when there are many items ,RecyclerView will auto scroll to the bottom everytime a new item is inserted. how to prevent it ?

this is the insert code:

@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

    getOneMessage(dataSnapshot.getKey(), new OneMessageCallBack() {
        @Override
        public void OnCallBack(Object_Message message) {

            Object_Conversation conversation=new Object_Conversation(dataSnapshot.getKey(),message);

            mConvers_List.add(conversation);
            mConvers_Adapter.notifyItemInserted(mConvers_List.size()-1); 
        }
    });
}

this the the XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_conversations"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:background="@color/white">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

like image 519
Trần Quốc Trung Avatar asked Oct 18 '25 12:10

Trần Quốc Trung


1 Answers

Try using

android:descendantFocusability="blocksDescendants"

in recyclerview. This will help you avoid autoscroll

like image 177
Viswanath Kumar Sandu Avatar answered Oct 21 '25 01:10

Viswanath Kumar Sandu