【问题标题】:How to scroll and collapse a LinearLayout with Toolbar in Coordinator Layout如何在 Coordinator Layout 中使用工具栏滚动和折叠 LinearLayout
【发布时间】:2017-01-07 14:55:16
【问题描述】:

我正在尝试在我的应用程序中使用协调器布局来完成滚动行为。

我有以下活动的布局结构。

<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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.Dark.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.Dark.PopupOverlay"/>
    </android.support.design.widget.AppBarLayout>

    <!-- The layout to be collapse with toolbar -->
    <LinearLayout
        android:id="@+id/ll_collapse_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

当我向上滚动回收站视图项时,工具栏会折叠并向上移出窗口,而当我向下滚动时,工具栏又会返回。

当我滚动 Recycler View 时,是否可以折叠带有工具栏的 LinearLayout?

【问题讨论】:

    标签: android android-layout android-recyclerview toolbar android-collapsingtoolbarlayout


    【解决方案1】:
    <?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="in.test.ProfileActivity">
        <android.support.design.widget.AppBarLayout
                    android:id="@+id/app_bar"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/app_bar_height"
                    android:fitsSystemWindows="true"
                    android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.design.widget.CollapsingToolbarLayout
                        android:id="@+id/toolbar_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fitsSystemWindows="true"
                        app:contentScrim="?attr/colorPrimary"
                        app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <LinearLayout
                    android:id="@+id/ll_collapse_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
             app:layout_collapseMode="parallax"
                    android:orientation="horizontal"></LinearLayout>
                <android.support.v7.widget.Toolbar
                            android:id="@+id/toolbar"
                            android:layout_width="match_parent"
                            android:layout_height="?attr/actionBarSize"
                            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/rv_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 你试过线性布局的布局高度匹配父级吗?并在其中添加一些内容???
    • 是的,它确实做到了...您到底想在线性布局中放置什么。所以我可以给你一个样本
    • 我破解了。我不需要将 Linearlayout 移动到 appbarlayout 的折叠工具栏就可以完成工作。感谢您的尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2015-12-28
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    相关资源
    最近更新 更多