如何将RecyclerView放在CollapsingToolbarLayout下面,并在Android中折叠时对工具栏做出响应? [英] How to put RecyclerView below CollapsingToolbarLayout and responsive to toolbar when collapsed in Android?

本文介绍了如何将RecyclerView放在CollapsingToolbarLayout下面,并在Android中折叠时对工具栏做出响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序.在我使用的应用程序中,CollapsingtoolbarLayout与RecyclerView一起使用.两者都在工作.但是我在放置它们时遇到了问题.我想要的是我希望RecyclerView位于折叠式Toobar的正下方,并且我希望RecyclerView在折叠时与工具栏一起向上移动.但是我的代码无法正常工作.

I am developing an Android App. In my app I am using, CollapsingtoolbarLayout together with RecyclerView. Both are working. But I am having a problem with positioning them. What I want is I want RecyclerView directly below Collapsing Toobar and I want RecyclerView go up together with toolbar when it is collapsed. But my code is not working as I expected.

这就是现在发生的事情.

This is what happening now.

如您所见,回收站视图的固定位置被工具栏覆盖.它对折叠的工具栏没有响应.

As you can see recycler view is having fixed position covered with toolbar. It is not responsive to collapsed toolbar.

这是我的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并为其设置数据外,我没有进行任何配置.

In activity, I did not configured anything excepting initializing RecyclerView and setting data to it.

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

This is how I set RecyclerView in activity

 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?

So, how can I make RecyclerView responsive to CollapsingToolbarLayout when it is collapsed?

推荐答案

我不知道您是否已修复它.但是你不见了

I don't know if you have already fixed it. However you are missing

app:layout_behavior="@string/appbar_scrolling_view_behavior"

在您的RecyclerView中.

in your 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"/>

这篇关于如何将RecyclerView放在CollapsingToolbarLayout下面,并在Android中折叠时对工具栏做出响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆