无法滚动到位于NestedScrollView中的RecyclerView中的项目



我正试图以编程方式滚动到我的RecyclerView中嵌套在NestedScrollView中的特定项目。

问题NestedScrollView滚动到整个底部,而不是所需的项目。
注意:当所需项目是第二项时,它会正确工作,可能是因为该项目在屏幕上是可见的。

我已经试过了

我已经从StackOverFlow上搜索了十几个解决方案,并提出了下面的函数。
我试过了:

  • binding.variantsRecyclerView.getChildAt()
  • binding.variantsRecyclerView.findViewWithTag()
  • binding.variantsRecyclerView.findViewHolderForAdapterPosition()

所有这些都返回正确的项目(我知道,因为该项目中的编辑文本是按编码聚焦的),但是,NestedScrollView不能正确滚动到该项目。它几乎总是滚动到底部。然而,有时它会滚动到所需项目之间的某个地方,而不是它的开始。只有当项目是第一项或第二项时,这种方法才有效。(如前所述)

private fun scrollToPosition(position: Int) {
if (position in 0 until variantsAdapter.itemCount) {
val view = binding.variantsRecyclerView.findViewWithTag<ConstraintLayout>("pos_$position")
if (view != null) {
val height = binding.nestedScrollView.height
val target = view.y.toInt()
binding.nestedScrollView.postDelayed({
binding.nestedScrollView.smoothScrollTo(0, target)
view.requestFocus()
}, 200)
}
}
}
我XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eff1f4">

<LinearLayout>...</LinearLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toTopOf="@+id/btnNextLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbarLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ui_10_dp"
android:layout_marginEnd="@dimen/ui_10_dp"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/variantsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"
android:paddingTop="12dp"
android:paddingBottom="12dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="2"
tools:listitem="@layout/sell_variant_row_item" />
<androidx.constraintlayout.widget.ConstraintLayout>
...
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<LinearLayout>...</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

我的理解调试后,我发现NestedScrollView的高度小于所需项目的y坐标。因此,它滚动到视图的底部,而不是所需的项目。我的理解可能是完全错误的,如果是这样,请纠正我。

我用一个非常简单的方法解决了这个问题。

private fun scrollToPosition(position: Int) {
if (position in 0 until variantsAdapter.itemCount) {
val view = binding.variantsRecyclerView.getChildAt(position)
if (view != null) {
val target = binding.variantsRecyclerView.top + view.top
binding.nestedScrollView.scrollY = target 
}
}
}

我想要的只是把RecyclerView中想要的项目放到屏幕的顶部。