为什么将RecyclerView放置在AppBarLayout中时不滚动?

问题描述 投票:0回答:1

我的应用程序中有一个AppBarLayout以及一个用作占位符以加载片段的FrameLayout:

 <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <FrameLayout android:id="@+id/main_content_fragment"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@color/white" />
    </android.support.design.widget.AppBarLayout>

有问题的片段如下:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ProgressBar android:id="@+id/loading_downloaded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <android.support.v7.widget.RecyclerView
            android:id = "@+id/items_downloaded"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            />
</LinearLayout>

您可能期望从代码中替换片段:

  val fragMan: FragmentManager = getSupportFragmentManager()
   fragMan.beginTransaction().add(R.id.main_content_fragment, fragment).commit()     

问题是,以这种方式格式化RecyclerView时,我无法滚动它。如果我将FrameLayout移到AppBarLayout之外,则效果很好,但是片段位于应用栏的后面,非常不整洁。这为我确认了片段工作正常,我只是想不出为什么片段在AppBarLayout内时滚动行为会发生变化。

要滚动内容,我需要做什么?还是我误解了,需要将片段显示在AppBarLayout之外,它们可以正确滚动并将所有内容向下移动应用栏的高度?

android android-fragments android-appbarlayout
1个回答
0
投票

您的片段的内容应作为AppBarLayout的同级放置。您应该将所有内容包装在CoordinatorLayout中,并在片段所在的FrameLayout中设置此属性:app:layout_behavior="@string/appbar_scrolling_view_behavior"

© www.soinside.com 2019 - 2024. All rights reserved.