【问题标题】:CoordinatorLayout with RecyclerView And Collapsing header带有 RecyclerView 和折叠标题的 CoordinatorLayout
【发布时间】:2016-04-30 21:58:03
【问题描述】:

我的布局如下:

(工具栏, 标题视图、文本视图、RecyclerView)

当我滚动 recyclerview 的项目时,我需要折叠标题。 这样视图“Choose item”和recyclerview就留在了屏幕上。

我看到了工具栏被折叠时的示例,但我需要工具栏始终存在。

我应该使用哪些布局/行为来完成这项工作?

【问题讨论】:

    标签: android android-layout material-design android-coordinatorlayout androiddesignsupport


    【解决方案1】:

    你可以通过这个布局来实现它:

    <android.support.design.widget.CoordinatorLayout
        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.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <!-- HEADER -->
                <RelativeLayout
                    ...
                    app:layout_collapseMode="parallax">
                    .....
                </RelativeLayout>
    
                <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
           <!-- IF YOU WANT TO KEEP "Choose Item" always on top of the RecyclerView, put this TextView here
            <TextView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_gravity="bottom"
                 android:text="choose item" />
           -->
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    您可以通过设置app:layout_collapseMode="pin" 属性来固定您的工具栏。您可以通过设置 app:layout_behavior="@string/appbar_scrolling_view_behavior" 使 RecyclerView 正确滚动,仅此而已。

    注意!“选择项目”TextView 的位置取决于您想要实现的特定行为:

    • 您可以将其作为RecyclerViewAdapter 的第一个元素包含在用户开始滚动浏览RecyclerView 时将其滚开;
    • 您可以将其添加到AppBarLayout,这样无论您是否滚动它,它都会始终贴在RecyclerView 之上;

    你可以在这里Android Design Support LibraryDesign Support Library (III): Coordinator Layout阅读更多

    希望对你有帮助!

    【讨论】:

    • 如果我们在底部添加一个滚动视图或 NestedScrollView 而不是 recyclerview,它是否仍然有效。我也在考虑在带有粘性标题的滚动视图中拥有多个回收器视图。这行得通吗?
    【解决方案2】:

    下面的代码可以正常工作,但我认为滚动比较 reqular recyclerview 不平滑。

    <?xml version="1.0" encoding="utf-8"?>
    <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_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <com.sliderbanner.views.BannerSlider
                    android:id="@+id/banner_slider1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:animateIndicators="true"
                    app:defaultIndicators="dash"
                    app:interval="5000"
                    app:loopSlides="true"
    
                    />
    
                <android.support.v7.widget.Toolbar
    
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?actionBarSize">
    
                    <ImageView
                        android:id="@+id/image_github"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:layout_gravity="right"
                        android:layout_marginRight="8dp" />
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fontFamily="sans-serif-bold"
                        android:gravity="center_vertical|left"
                        android:text="Banner Slider"
                        android:textColor="@android:color/black"
                        android:textSize="18sp" />
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
    
    
        </android.support.design.widget.AppBarLayout>
        <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">
        </android.support.v7.widget.RecyclerView>
    
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2017-02-05
      相关资源
      最近更新 更多