我的应用程序中有一个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
之外,它们可以正确滚动并将所有内容向下移动应用栏的高度?
您的片段的内容应作为AppBarLayout
的同级放置。您应该将所有内容包装在CoordinatorLayout
中,并在片段所在的FrameLayout
中设置此属性:app:layout_behavior="@string/appbar_scrolling_view_behavior"
。