2016-11-10 142 views
1

当我实现了折叠式工具在我的活动之一,并在滚动工具栏不垮recyclerview不垮:折叠工具栏和滚动

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/swipeContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@color/white" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:fitsSystemWindows="true" 
     android:layout_height="@dimen/app_bar_height" 
     android:layout_width="match_parent" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/toolbar_layout" 
      android:fitsSystemWindows="true" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      app:contentScrim="?attr/colorPrimary"> 

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

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/my_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:scrollbars="none" /> 


</LinearLayout> 
</android.support.v4.widget.SwipeRefreshLayout> 

我还添加了所需的库摇篮。向下滚动时,工具栏保持在同一位置。任何想法为什么发生这种情况?

回答

1

AppBarLayout需要CoordinatorLayout。

该视图在很大程度上依赖于在CoordinatorLayout中用作直接子对象。如果你在不同的ViewGroup中使用AppBarLayout,它的大部分功能将不起作用。

从developer.android.com/reference/android/support/design/widget/AppBarLayout.html

引用

而且看看这些教程: http://antonioleiva.com/collapsing-toolbar-layout/

+0

我有recyclerview –

+0

@RainMan在另一个布局:你不需要在这个LinearLayout中。 CoordinatorLayout将根据他们的行为相应地定位视图。删除'LinearLayout',将'SwipeRefreshLayout'移动到'RecyclerView'周围,然后将顶层设置为'CoordinatorLayout'。 – DeeV

+0

@DeeV,感谢您的评论,我做了与您所描述的相同的方法,但是现在当recycleview的内容位于工具栏的顶部时。 –

1

此代码可以帮助你。在您的活动中定义CollapsingToolbar并写入 展开和折叠的方法。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_car_tracker" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     tools:context="com.example.user.Activity" /> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="180dp" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/toolbarbackgroundcolor" 
      android:fitsSystemWindows="true" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="@color/toolbarbackgroundcolor" 
       android:minHeight="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_gravity="center" 
        android:foregroundGravity="center" 
        android:src="@drawable/location" 
        android:scaleType="centerCrop" 
        app:layout_collapseMode="parallax"/> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 



</android.support.design.widget.CoordinatorLayout> 
相关问题