【问题标题】:Do not collapse AppBarLayout when scrolling down向下滚动时不要折叠 AppBarLayout
【发布时间】:2018-07-07 15:08:51
【问题描述】:

我有一个这样布局的片段

    <?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.design.widget.AppBarLayout
        android:background="?attr/cropperBckColor"
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/cropper_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolBarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="@android:color/transparent"
            app:contentScrim="@color/transparent">

            <com.paralect.mediapicker.custom_views.cropper.ImageCropperView
                android:id="@+id/image"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.0"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <android.support.v7.widget.Toolbar
                app:layout_collapseMode="pin"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="@dimen/my_tab_height">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <android.support.v7.widget.AppCompatImageView
                        android:layout_alignParentBottom="true"
                        style="@style/Icon"
                        android:src="@drawable/ic_rezoom"
                        android:id="@+id/rezoom"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true" />

                    <android.support.v7.widget.AppCompatImageView
                        android:layout_alignParentBottom="true"
                        style="@style/Icon"
                        android:src="@drawable/ic_multiple_disabled"
                        android:id="@+id/multiSelect"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentRight="true" />
                </RelativeLayout>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

协调器布局按预期工作,但我想更改 AppBarLayout 的行为,即使其在向下滚动回收站视图时折叠,但在滚动 AppBarLayout 时折叠。

我该怎么做?

提前致谢

【问题讨论】:

标签: android android-coordinatorlayout android-appbarlayout


【解决方案1】:

根据android documentation for RecyclerView,实现 RecyclerView.OnScrollListener 包含方法 OnScroll(...),其中包含 delta_x 和 delta_y。使用 delta_y 值可用于检测 y 方向的位移变化。因此,

if( dy &gt; 0 ) { you're scrolling in the positive vertical direction }

else if( dy &lt; 0 ) { you're scrolling in the negative vertical direction }

else { dy == 0, you're not scrolling or the visible item changed }

知道这个逻辑,if(dyandroid documentation for AppBarLayout使用setScrollFlags(int)来实现。

【讨论】:

  • 按照这种方式可以确保您以编程方式控制您的应用程序,而不是在 xml 文件中设置一个固定值。
【解决方案2】:

在您的 Activity 类中进行以下更改

toolbar.setCollapsible(false);

在你的XML文件中设置

app:layout_scrollFlags="snap"

用于 CollapsingToolbarLayout

此更改将避免 AppBarLayout 在向下滚动时折叠。

希望这行得通。

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多