首页 > 解决方案 > viewpager2 中的 recyclerView 将在导航中从下一页返回后自动向上滚动

问题描述

viewpager2 布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/home_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:tabGravity="center"
        app:tabIndicatorFullWidth="false" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/home_viewpager2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/home_tab" />

</androidx.constraintlayout.widget.ConstraintLayout>

viewpager2 的适配器创建了两个包含 recyclerView 片段布局的片段:

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

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

</androidx.constraintlayout.widget.ConstraintLayout>

滚动到recyclerView的底部并单击一项以导航到下一页并单击系统后退按钮,recyclerView将自动滚动仅在recyclerView底部发生的一项

标签: androidandroid-recyclerviewnavigation

解决方案


推荐阅读