在CoordinatorLayout中工具栏下方的进度条

4

我有一个像这样的布局XMl:

<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            <RelativeLayout/>
            <Toolbar/>
        </CollapsingToolbarLayout>
    </AppBarLayout>
    <RecyclerView/>
</CoordinatorLayout>

我希望在工具栏下方和回收视图上方添加一个水平进度条,以指示回收视图项目的数据加载。
使用相对布局,我可以为进度条提供 android:layout_below 属性来使其位于操作栏下方。但由于我对 Coordinator Layout 和 Toolbar 不熟悉,因此无法获得相应的结果。
3个回答

3
将进度条(ProgressBar)作为CoordinatorLayout的直接子级。
<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            <ImageView/>
            <Toolbar/>
        </CollapsingToolbarLayout>
    </AppBarLayout>
    <NestedScrollView>
    </NestedScrollView>
    <ProgressBar/>
    <BottomAppBar/>
    <FloatingActionButton/>
</CoordinatorLayout>

对于ProgressBar,使用layout_anchor将其与AppBarLayout(与AppBarLayout的id相同)相关联。将layout_anchorGravity设置为bottom。

    <ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:visibility="@{viewModel.loading ? View.VISIBLE : View.GONE}"
    app:layout_anchor="@id/appBarLayout"
    app:layout_anchorGravity="bottom" />

我附上了代码和预览截图。 水平进度条正常显示


2
如果你想要在Toolbar中隐藏ProgressBar,需要将Toolbar放置在LinearLayout中,并将ProgressBar作为该LinearLayout的第一个子元素。
orientation="vertical"

如果您希望ProgressBar始终可见,则将RecyclerView包装在LinearLayout中,并将ProgressBar放置在第一个子元素位置。设置方向。请记得添加。
app:layout_behavior="@string/appbar_scrolling_view_behavior"

作为LinearLayout的一个参数(必须将滚动传递给子元素)。

1
<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            <RelativeLayout/>
            <Toolbar/>
        </CollapsingToolbarLayout>
    </AppBarLayout>
    <LinearLayout>
      <ProgressBar>
      <RecyclerView/>
    </LinearLayout>
</CoordinatorLayout>

上面的代码应该可以满足你的需求。请确保线性布局方向为垂直。唯一需要注意的是,如果你的可回收视图很大,你可能需要将线性布局包裹在一个嵌套滚动视图中,以便进度条也能滚动。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接