Прокрутка Recyclerview для указания c позиции внутри NestedScrollView - PullRequest
0 голосов
/ 24 февраля 2020

Предположим, у меня есть 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);

1 Ответ

0 голосов
/ 24 февраля 2020

Это должно работать в моем случае

 testRecyclerview.post(() -> {
            float y = testRecyclerview.getY() + testRecyclerview.getChildAt(50).getY();
            nestedScrollView.smoothScrollTo(0, (int) y);
        });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...