简体   繁体   English

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

[英]How to put RecyclerView below CollapsingToolbarLayout and responsive to toolbar when collapsed in Android?

I am developing an Android App. 我正在开发一个Android应用程序。 In my app I am using, CollapsingtoolbarLayout together with RecyclerView. 在我的应用程序中,我正在使用,与DistributedlerView一起使用CollapsingtoolbarLayout。 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. 我想要的是我想在Recycpsing Toobar下方直接使用RecyclerView,我希望RecyclerView在折叠时与工具栏一起上升。 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. 它无法响应折叠的工具栏。

This is my XML layout: 这是我的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>

In activity, I did not configured anything excepting initializing RecyclerView and setting data to it. 在活动中,除了初始化RecyclerView和设置数据之外,我没有配置任何东西。

This is how I set RecyclerView in activity 这就是我在活动中设置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
    }

So, how can I make RecyclerView responsive to CollapsingToolbarLayout when it is collapsed? 那么,如何在折叠时使RecyclerView对CollapsingToolbarLayout做出响应?

I don't know if you have already fixed it. 我不知道你是否已经修好了。 However you are missing 但是你错过了

app:layout_behavior="@string/appbar_scrolling_view_behavior"

in your RecyclerView. 在您的RecyclerView中。

Try like this: 试试这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使 CollapsingToolbarLayout 折叠,如何使 Toolbar 和 TabLayout 仍然可见? - How to make Toolbar and TabLayout still visible even when CollapsingToolbarLayout is collapsed? 在 CollapsingToolbarLayout 中折叠时如何删除透明的工具栏填充 - How to remove transparent Toolbar padding when collapsed in CollapsingToolbarLayout 如何将CollapsingToolbarLayout的背景保留为ToolBar的背景,即使在折叠时也是如此 - How to keep the background of CollapsingToolbarLayout as the background of ToolBar even when collapsed CollapsingToolbarLayout折叠时未显示工具栏标题 - Toolbar Title not showing when CollapsingToolbarLayout collapsed 当CollapsingToolbarLayout折叠时,工具栏HOME不起作用 - Toolbar HOME not working when CollapsingToolbarLayout collapsed 如何将视图元素粘贴到android中的collapsingtoolbarlayout工具栏下面? - How to stick a view element below a collapsingtoolbarlayout toolbar in android? 展开后如何将工具栏放置在collapsingtoolbarlayout下方? - How do I place toolbar below collapsingtoolbarlayout when expanded? 工具栏下方的 Android RecyclerView - Android RecyclerView below Toolbar Android Material Design-CollapsingToolbarLayout折叠后如何更改工具栏的背景颜色 - Android Material Design - How to change background color of Toolbar after CollapsingToolbarLayout is collapsed 当CollapsingToolbarLayout折叠时,锚定图像会被工具栏重叠 - Anchor image gets overlapped by toolbar when CollapsingToolbarLayout is collapsed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM