我尝试使用 recyclerview
函数以编程方式将 smoothScrollTo(position)
滚动到特定位置。但它不起作用。
当我尝试执行 smoothScrollTo(position)
下面是xml代码:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="true"
android:fillViewport="true"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/layout"
layout="@layout/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_20dp"
android:layout_marginTop="@dimen/margin_20dp"
android:layout_marginEnd="@dimen/margin_20dp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="@dimen/home_promotion_pager_height"
android:layout_marginTop="@dimen/margin_10dp"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:visibility="visible"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="@+id/layout"
app:setAdapter="@{viewModel.getPromotionsAdapter()}"
tools:listitem="@layout/delivery_promotion_item" />
<include
android:id="@+id/safetyCheckLayout"
layout="@layout/safety_check_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/height_50dp"
android:layout_marginStart="@dimen/margin_20"
android:layout_marginTop="@dimen/margin_top_15dp"
android:layout_marginEnd="@dimen/margin_20"
app:layout_constraintTop_toBottomOf="@+id/pager" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/veg_non_veg_switch"
android:layout_width="wrap_content"
android:layout_height="@dimen/margin_20"
android:layout_marginTop="@dimen/margin_15dp"
android:layout_marginEnd="@dimen/margin_40dp"
android:background="@drawable/switch_drawable"
android:buttonTint="@color/address_text_color"
android:checked="false"
android:elevation="@dimen/pad_5dp"
android:shadowColor="#00000059"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/safetyCheckLayout"
app:thumbTint="@color/blurred_grey_text"
app:trackTint="@color/white" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/veg"
style="@style/Nunito_Sans_Semibold"
android:layout_width="wrap_content"
android:layout_height="@dimen/margin_20"
android:layout_marginEnd="@dimen/dimen_35"
android:gravity="center_vertical"
android:text="@string/veg"
android:textColor="@color/grey_text"
android:textSize="@dimen/font_12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/veg_non_veg_switch" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/budget_friendly_boxes_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="@dimen/margin_10dp"
android:orientation="vertical"
android:nestedScrollingEnabled="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/veg_non_veg_switch"
app:setAdapter="@{viewModel.saveDeliverySubCategoryParentAdapter()}"
tools:listitem="@layout/delivery_sub_category_parent_item" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recommended_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_20"
android:layout_marginTop="@dimen/margin_10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/budget_friendly_boxes_recyclerview"
app:setAdapter="@{viewModel.getImageParentAdapter()}"
tools:listitem="@layout/delivery_image_parent_single_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
下面是我尝试平滑滚动的代码:
viewModel.boxItemSelected.observe(viewLifecycleOwner, Observer {
if (it != null) {
setCategoryRecyclerAdapter(deliveryHomeModelData?.data!!, it)
deliveryHomeModelData?.data?.box?.forEachIndexed { index, box ->
if (box.category_code == it.categoryId) {
(activity as LandingActivity).runOnUiThread {
// binding.nestedScrollView.smoothScrollTo(
// 0,
// height
// )
binding.budgetFriendlyBoxesRecyclerview.smoothScrollToPosition(index)
}
}
}
}
})
nestedScrollView.smoothScrollTo
工作正常,但 recyclerview.smoothScrollTo
不工作,但我无法提供 nestedScrollview
滚动到特定位置的确切高度。
在这方面请求您的帮助。