Recyclerview เลื่อนไปยังตำแหน่งเฉพาะภายใน NestedScrollView

สมมติว่าฉันมี 40 รายการใน RecyclerView และต้องการเลื่อนไปที่รายการหมายเลข 20

นี่คือลักษณะของ XML ของฉัน:-

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

รหัส Java ฉันใช้ onCreate() เพื่อตั้งค่าอะแดปเตอร์ใน RecyclerView:

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

ในกรณีของ LinearLayout มันทำงานได้ดีมาก แต่จะไม่ได้ผลเมื่อเราใช้ NestedScrollView

testRecyclerview.getLayoutManager().scrollToPosition(20);

person new member    schedule 24.02.2020    source แหล่งที่มา


คำตอบ (1)


สิ่งนี้ควรจะได้ผลในกรณีของฉัน

 testRecyclerview.post(() -> {
            float y = testRecyclerview.getY() + testRecyclerview.getChildAt(50).getY();
            nestedScrollView.smoothScrollTo(0, (int) y);
        });
person Jignesh Mayani    schedule 24.02.2020
comment
ลองใช้ android:nestedScrollingEnabled=false ใน recycleview xml - person Jignesh Mayani; 24.02.2020
comment
100 ข้างต้นคืออะไร (ฉันหมายถึงสิ่งที่ชดเชย) - person new member; 24.02.2020
comment
เป็นระยะห่างจากด้านบนถึงรายการรีไซเคิล - person Jignesh Mayani; 24.02.2020
comment
พยายามด้วย false สำหรับ NestedScrollingEnabled ใน recyclerview แต่ไม่ได้ผล - person new member; 24.02.2020
comment
คุณสามารถแชร์ไฟล์ java ของคุณได้หรือไม่? ฉันจะตรวจสอบว่ามีปัญหาบางอย่างเพราะในกรณีของฉันสิ่งนี้จะได้ผล - person Jignesh Mayani; 24.02.2020
comment
แน่นอน... ฉันซาบซึ้ง - person new member; 24.02.2020
comment
โปรดใช้ RecyclerView ภายใน NestedScrollView เช่นเดียวกับการใช้งานของฉัน - person new member; 24.02.2020