【问题标题】:Android CoordinatorLayout Behaviour with complex layout具有复杂布局的 Android CoordinatorLayout 行为
【发布时间】:2016-08-25 14:42:15
【问题描述】:

我有一个简单的布局,它是一个CoordinatorLayout,在AppBarLayout 和一个RecyclerView 中包含一个Toolbar。为了在将内容加载到 RecyclerView 时允许进度条,我将它包装在 FrameLayout 旁边,并与 ProgressBar 一起包含在另一个文件中。内容加载时,ProgressBar 设置为VISIBLE,完成后设置为GONE,显示RecyclerView。我想使用ScrollingViewBehavior,这样当我滚动我的RecyclerView 时,Toolbar 就被隐藏了。我尝试将其添加到 FrameLayoutRecyclerView 中,但似乎都不起作用。

我必须做什么才能获得我正在寻找的行为?我需要创建一个新的ViewGroup 或类似的东西来显示/隐藏ProgressBar 并为此定义一个新的Behavior,还是有更简单的方法?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ToolbarTheme"
            android:background="?attr/colorPrimary"/>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:id="@+id/recycler_view" />

        <include layout="@layout/progress_circle" />

    </FrameLayout>

    <include layout="@layout/floating_action_button"/>
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-layout android-recyclerview android-framelayout


    【解决方案1】:

    我的设置几乎与您相同,但我添加了两个进度条。加载活动时出现一个,并覆盖整个事物。第二个出现在滑动刷新时,仅替换 RecyclerView(实际上是 RecyclerView 的父 SwipeRefreshLayout)。

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true">
    
        <include layout="@layout/loading_progress"/>
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator_layout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="false">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_bar_height"
                android:fitsSystemWindows="false"
                android:theme="@style/AppTheme.AppBarOverlay">
    
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/toolbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="false"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways">
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar_landing_page"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/AppTheme.PopupOverlay"/>
    
                </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>
    
            <include layout="@layout/loading_progress"/>
    
            <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipe_refresh"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/landing_page_top_margin"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
    
            </android.support.v4.widget.SwipeRefreshLayout>
        </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>
    

    而我的进度条只是:

    <ProgressBar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/loading_progress"
        style="@android:style/Widget.ProgressBar.Inverse"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    当显示进度条时,我将其设置为 View.VISIBLE,并将 SwipeRefreshLayout(或加载 Activity 时的 CoordinatorLayout)设置为 View.GONE。然后在加载数据时反转 VISIBLE 和 GONE。

    【讨论】:

    • 哦,这是有道理的,因为CoordinatorLayout 基本上只是一个FrameLayout。我会试试这个并回复你。
    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多