在回收视图中设置notifyItemChanged时,嵌套滚动视图自动滚动到顶部

问题描述 投票:0回答:1

我在NestedScrollView中使用RecyclerView,我的RecyclerView设置layoutManager = GridLayoutManager。 当我在recycleView中设置notifyItemChange时,我的问题发生了,在nestedscrollView中查看自动滚动到顶部,它不保持位置 这是我的代码 XML

<androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingHorizontal="@dimen/vtb_sdk_15sdp"
            android:paddingBottom="@dimen/vtb_sdk_90sdp">


            //other code

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_qr_background"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/vtb_sdk_15sdp"
                android:nestedScrollingEnabled="false"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tv_background" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

代码块设置回收视图

 val linearLayoutManager = GridLayoutManager(context, 3, LinearLayoutManager.VERTICAL, false)
        mBinding.rvQrBackground.apply {
            adapter = qrBackgroundAdapter
            layoutManager = linearLayoutManager
        }

适配器中设置的代码块

root.setThrottleClickListener {
                    if (selectedItem != position) {
                        val oldPosition = selectedItem
                        selectedItem = position
                        notifyItemChanged(oldPosition)
                        notifyItemChanged(position)
                    }
                    onItemClickListener?.invoke(this, position)
                }

我添加了

android:descendantFocusability="blocksDescendants"

进入ConstraintLayout,但不起作用。

java android kotlin view
1个回答
0
投票

RecyclerView 上方的某些视图会出现此问题。显示所有代码。

© www.soinside.com 2019 - 2024. All rights reserved.