簡體   English   中英

使用 RecyclerView、android jetpack 導航和折疊工具欄實現正確滾動

[英]Achieving proper scrolling with RecyclerView, android jetpack navigation and a collapsing Toolbar

我嘗試在我的 android 應用程序中使用以下設置來實現正確的滾動行為。

對於導航,我將噴氣背包導航與Toolbar布局和底部導航結合使用。 我也使用“一個活動,多個片段”的原則。 底部導航的每個項目根據我的導航圖啟動一個相應的Fragment 這要求我在布局中使用NavHostFragment

我的Toolbar布局是活動布局的一部分,並根據當前片段進行填充。 一些片段需要一個折疊的Toolbar ,它也會在需要時添加。 但是當有一個折疊工具欄時,我面臨以下問題:

在特定情況下,我有一個折疊的ToolbarNavHostFragment填充了RecyclerView 在其他情況下,我似乎可以將app:layout_behavior="@string/appbar_scrolling_view_behavior"RecyclerView並獲得預期結果,因為RecyclerViewCoordinatorLayout的直接子級。 在我的情況下, RecyclerViewFragment的子級,它基本上是FrameLayout (根據布局檢查器)。 這導致的問題,即在該layout_behaviour RecyclerView有沒有影響,因為該RecyclerView不是直接孩子CoordinatorLayout

我無法為這個問題想出一個可行的解決方案。 有人有想法嗎?

布局/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"/>

布局/工具欄.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>

布局/list.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"
    />

嘗試使用具有所需行為的 NestedScrollView 包裝 host_fragment,如下所示:

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

Jurij Pituljas 方法將阻止RecyclerView scrollToPosition方法工作。 另外使用RecyclerView導航回片段會將其滾動位置重置到頂部。

這里描述一種更好的方法。 甚至在引用的文章中也不需要周圍的FrameLayout 無需將片段包裝在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM