Достижение правильной прокрутки с помощью RecyclerView, навигации Android Jetpack и сворачивающейся панели инструментов

Я пытаюсь реализовать правильное поведение прокрутки со следующей настройкой в ​​​​своем приложении для Android.

Для навигации я использую навигацию с реактивным ранцем в сочетании с раскладкой Toolbar и нижней навигацией. Я также использую принцип «одна деятельность, много фрагментов». Каждый элемент нижней навигации запускает соответствующий Fragment на основе моего графика навигации. Это требует от меня использования NavHostFragment в моем макете.

Мой макет Toolbar является частью макета активности и заполняется на основе текущего фрагмента. Некоторые фрагменты требуют свертывания Toolbar, которое также добавляется при необходимости. Но когда у меня сворачивается панель инструментов, я сталкиваюсь со следующей проблемой:

В конкретном случае у меня есть разрушающийся Toolbar, NavHostFragment заполняется RecyclerView. В других случаях кажется, что я могу добавить app:layout_behavior="@string/appbar_scrolling_view_behavior" к RecyclerView и получить ожидаемый результат, потому что RecyclerView является прямым потомком CoordinatorLayout. В моем случае RecyclerView является дочерним элементом Fragment, который в основном является FrameLayout (согласно инспектору макета). Это приводит к проблеме, что layout_behaviour на RecyclerView не имеет никакого эффекта, так как RecyclerView не является прямым потомком CoordinatorLayout.

Я не мог найти рабочего решения для этой проблемы. У кого-нибудь есть идея?

layout/activity_overview.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OverviewActivity">

<androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="wrap_content"
                                                     android:layout_height="wrap_content"
                                                     android:id="@+id/toolbarCoordiantor"
                                                     android:layout_marginTop="?attr/actionBarSize"
                                                     app:layout_constraintTop_toTopOf="parent"
                                                     app:layout_constraintBottom_toTopOf="@id/overview_bottom_navigation"
                                                     app:layout_constraintStart_toStartOf="parent"
                                                     app:layout_constraintEnd_toEndOf="parent">

    <fragment android:layout_width="match_parent" android:layout_height="wrap_content"
              android:id="@+id/overview_fragmentholder"
              android:name="androidx.navigation.fragment.NavHostFragment"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/toolbar"
              app:layout_constraintBottom_toBottomOf="parent"
              app:defaultNavHost="true"
              android:layout_marginBottom="?attr/actionBarSize"
              android:layout_marginTop="?attr/actionBarSize"
              app:navGraph="@navigation/nav_graph"
              app:layout_constraintVertical_bias="1.0"/>

    <include layout="@layout/toolbar"
             android:id="@+id/toolbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/overview_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foregroundGravity="bottom"
        app:menu="@menu/navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/bottom_navigation_color_states"
        app:itemTextColor="@color/bottom_navigation_color_states"/>

layout/toolbar.xml

<com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap|snapMargins"
        android:minHeight="?attr/actionBarSize">

    <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                       android:layout_height="wrap_content"
                                                       android:id="@+id/expandedToolbarContentContainer"
                                                       android:layout_marginTop="?attr/actionBarSize"
                                                       app:layout_collapseMode="parallax"/>

    <androidx.appcompat.widget.Toolbar android:layout_width="match_parent"
                                       android:layout_height="?attr/actionBarSize"
                                       app:layout_collapseMode="pin"
                                       style="@style/AppTheme.DarkToolbar"
                                       android:id="@+id/toolbarView">

        <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
                                                           android:layout_height="match_parent"
                                                           android:background="@drawable/round_outline"
                                                           style="@style/AppTheme.DarkToolbar.Container"
                                                           android:id="@+id/toolbarContentContainer"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.CollapsingToolbarLayout>

макет/список.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dish_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layoutAnimation="@anim/layout_animation_fall_down"
    />




Ответы (2)


Попробуйте обернуть host_fragment с помощью NestedScrollView с необходимым поведением, например:

 <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:fitsSystemWindows="true"
        android:transitionGroup="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/my_nav_host" />
    </androidx.core.widget.NestedScrollView>
person Jurij Pitulja    schedule 27.03.2019
comment
Это предотвратит работу scrollToPosition метода RecyclerView. Кроме того, возврат к фрагментам с помощью RecyclerView сбросит позицию прокрутки вверх. См. мой ответ для лучшего подхода. - person irieill; 03.12.2019

подход Юрия Питуляса не позволит scrollToPosition методу RecyclerView работать. Кроме того, возврат к фрагментам с помощью RecyclerView сбросит позицию прокрутки вверх.

Лучший подход описан здесь. Даже нет необходимости в окружающих FrameLayouts в упомянутой статье. Вместо того, чтобы оборачивать фрагмент в NestedScrollView, просто оберните все представления макета фрагмента без прокрутки в NestedScrollView.

Макет действий с NavHostFragment: нет необходимости оборачивать фрагмент в представление с прокруткой. Просто установите layout_behavior.

...
<fragment
  android:id="@+id/navigation_host_fragment"
  android:name="androidx.navigation.fragment.NavHostFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:defaultNavHost="true"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  app:navGraph="@navigation/navigation_graph" />
...

Макет фрагмента с корнем RecyclerView: Ничего особенного делать не нужно, так как RecyclerView имеет возможности прокрутки.

<androidx.recyclerview.widget.RecyclerView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scrollbars="vertical" />

Фрагмент макета, например. TextView: заключить в корень NestedScrollView, чтобы добавить возможности прокрутки.

<androidx.core.widget.NestedScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="non scrolling content wrapped" />
  </LinearLayout>
</androidx.core.widget.NestedScrollView>
person irieill    schedule 03.12.2019