如何将RecyclerView放在CollapsingToolbarLayout下面并在Android中折叠时响应工具栏?

时间:2016-05-29 08:39:30

标签: android android-recyclerview android-coordinatorlayout android-collapsingtoolbarlayout

我正在开发Android应用程序。在我的应用程序中,我正在使用,与DistributedlerView一起使用CollapsingtoolbarLayout。两者都有效。但我在定位它们时遇到了问题。我想要的是我想在Recycpsing Toobar下方直接使用RecyclerView,我希望RecyclerView在折叠时与工具栏一起上升。但我的代码没有像我预期的那样工作。

这就是现在发生的事情。

enter image description here

正如您所见,回收站视图的工具栏上有固定位置。它对折叠的工具栏没有响应。

这是我的XML布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/dc_rv_destination"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <android.support.design.widget.AppBarLayout
        android:id="@+id/htab_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/htab_collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/main_activity_parallax_initial_height"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/htab_header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/apple"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
                <TextView
                    android:layout_centerInParent="true"
                    android:text="HELLO"
                    android:textSize="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/main_activity_toolbar_height"
                android:gravity="top"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleMarginTop="13dp" />
          <!--  <android.support.design.widget.TabLayout
                android:id="@+id/htab_tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                app:tabIndicatorColor="@android:color/white" />-->
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

在活动中,我没有配置除初始化RecyclerView和设置数据之外的任何内容。

这就是我在活动中设置RecyclerView的方式

 private void setDestinationRecyclerView()
    {
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getBaseContext());
        rcDestinations.setLayoutManager(mLayoutManager);
        rcDestinations.setItemAnimator(new DefaultItemAnimator());
        regionsList = new ArrayList<Region>();
        destinationsAdapter = new DestinationsAdapter(regionsList,getBaseContext());
        rcDestinations.setAdapter(destinationsAdapter);
        //add items and notify data changed
    }

那么,如何在折叠时使RecyclerView对CollapsingToolbarLayout做出响应?

1 个答案:

答案 0 :(得分:13)

我不知道你是否已经修好它。但是你错过了

app:layout_behavior="@string/appbar_scrolling_view_behavior"
在您的RecyclerView中

试试这样:

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