简体   繁体   English

插入新项目时如何防止recyclerview自动滚动到底部?

[英]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. 我正在使用RecyclerView来查看我的数据,但是当有很多项目时,每次插入新项目时, RecyclerView都会自动滚动到底部。 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:

<?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>

Try using 尝试使用

android:descendantFocusability="blocksDescendants"

in recyclerview. 在recyclerview中。 This will help you avoid autoscroll 这将帮助您避免自动滚动

I think in order to disable the autoscroll you need to use notifyItemRangeInserted instead of notifyItemInserted . 我认为,要禁用自动滚动,您需要使用notifyItemRangeInserted而不是notifyItemInserted This should not make your RV scroll to the bottom. 这不应使您的RV滚动到底部。

final int positionStart = mConvers_List.size() + 1;
mConvers_List.add(conversation);
notifyItemRangeInserted(positionStart, mConvers_List.size());

您可以使用scrollToPosition(int position)方法滚动回零位置。

RecyclerView.scrollToPosition(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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