Gulir Recyclerview ke posisi tertentu di dalam NestedScrollView

Asumsikan, saya memiliki 40 item di RecyclerView dan ingin menggulir ke Item nomor 20

Berikut tampilan XML saya:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nestedScrollView"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/testRecyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.core.widget.NestedScrollView>

Kode Java, saya gunakan di onCreate() untuk mengatur adaptor di RecyclerView:

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            testRecyclerview.setLayoutManager(linearLayoutManager);
            adapter = new ProductsAdapter(getActivity(), productModels);
            testRecyclerview.setAdapter(adapter);

Dalam kasus LinearLayout, ini berfungsi dengan sangat baik, tetapi tidak berfungsi saat kita menggunakan NestedScrollView

testRecyclerview.getLayoutManager().scrollToPosition(20);

person new member    schedule 24.02.2020    source sumber


Jawaban (1)


Ini seharusnya berhasil dalam kasus saya

 testRecyclerview.post(() -> {
            float y = testRecyclerview.getY() + testRecyclerview.getChildAt(50).getY();
            nestedScrollView.smoothScrollTo(0, (int) y);
        });
person Jignesh Mayani    schedule 24.02.2020
comment
coba Android:nestedScrollingEnabled=false ini di recycleview xml - person Jignesh Mayani; 24.02.2020
comment
berapa angka 100 diatasnya (maksudku yang di offset) - person new member; 24.02.2020
comment
Ini adalah jarak dari atas ke item tinjauan daur ulang - person Jignesh Mayani; 24.02.2020
comment
mencoba dengan false untuk nestedScrollingEnabled di recyclerview, tetapi tidak berhasil - person new member; 24.02.2020
comment
Bisakah Anda membagikan file java Anda? saya akan memeriksa apakah ada masalah karena dalam kasus saya ini akan berhasil - person Jignesh Mayani; 24.02.2020
comment
tentu... saya hargai - person new member; 24.02.2020
comment
silakan gunakan RecyclerView di dalam NestedScrollView, sama seperti implementasi saya - person new member; 24.02.2020