本文介绍了如何在 CoordinatorLayout 中禁用 AppBarLayout 的滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AppBarLayout 中有具有视差效果的 MapFragment:

I have MapFragment with parallax effect inside AppBarLayout:

我想在 AppBarLayout 上禁用滚动,因为不可能在地图上移动,因为地图上的触摸事件总是作为滚动事件处理.我想通过仅滚动位于屏幕底部的 RecyclerView 来处理 AppBarLayout 的折叠.

I want to disable scrolling on AppBarLayout, because it is not possible to move across map, since touch evenys on the map are always handled as scroll events.I would like to handle collapsing of AppBarLayout by scrolling RecyclerView only, which is on the bottom of the screen.

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/white"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
        app:titleEnabled="false">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_collapseMode="parallax"
            android:fitsSystemWindows="true">

            <fragment
                android:id="@+id/map"
                android:name="com.androidmapsextensions.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="73dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <include
                android:id="@+id/search_bar"
                layout="@layout/layout_searchbar" />
        </android.support.v7.widget.Toolbar>

        <View
            android:id="@+id/toolbar_shadow"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_below="@id/search_bar"
            android:layout_gravity="bottom"
            android:background="@drawable/toolbar_dropshadow"
            android:visibility="gone" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<RecyclerView
    android:id="@+id/farm_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

感谢您的回复.

推荐答案

我不确定我是否明白,但我认为您正在寻找 DragCallback.

I'm not sure I got it, but I think you are looking for a DragCallback.

DragCallback 接口允许选择兄弟滚动视图是否应该通过滚动控制到 AppBarLayout.

The DragCallback interface allows to choose whether the sibling scrolling view should be controlled by scrolls onto the AppBarLayout.

您可以通过调用来定义一个:

You can define one by calling:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
    @Override
    public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
        return false;
    }
});

通过始终返回 false,您的滚动视图将不再受 ABL 控制.

By always returning false, your scrolling view will not be controlled by the ABL any longer.

注意:在调用之前你应该检查ViewCompat.isLaidOut(appBarLayout),否则params.getBehavior()将返回null.

Note: before calling this you should check that ViewCompat.isLaidOut(appBarLayout), otherwise params.getBehavior() will return null.

这篇关于如何在 CoordinatorLayout 中禁用 AppBarLayout 的滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:34