2016-08-25 35 views
0

我有一个简单的布局是CoordinatorLayout包含ToolbarAppBarLayoutRecyclerView。为了在内容加载到RecyclerView时允许进度条,我将其包装在一个FrameLayout中,旁边还有一个ProgressBar,这是我从另一个文件中包含的。当内容被加载时,ProgressBar被设置为VISIBLE,并且当它完成时,它被设置为GONE,显示RecyclerView。我想使用ScrollingViewBehavior,这样当我滚动我的RecyclerView时,隐藏了Toolbar。我试过把它添加到FrameLayoutRecyclerView,似乎都没有工作。Android CoordinatorLayout行为与复杂的布局

我需要做些什么来获得我正在寻找的行为?我是否需要制作新的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> 

回答

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(或CoordinatorLayout当活动被加载)到View.GONE。然后在加载数据时反转VISIBLE和GONE。

+0

哦,这是有道理的,因为'CoordinatorLayout'基本上只是一个'FrameLayout'。我会尝试一下并回复你。 – mrobinson7627

+0

完全作品,谢谢! – mrobinson7627