如何禁止在CoordinatorLayout中滚动AppBarLayout
2021-02-24 17:03:07  By: shinyuu

我有AppBarLayout中具有视差效果的MapFragment,我想在AppBarLayout上禁用滚动,因为不可能在地图上移动,因为地图上的触摸偶数总是作为滚动事件处理.

我想通过仅滚动RecyclerView(位于屏幕底部)来处理AppBarLayout的折叠。


xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@ id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/public_white"
    android:fitsSystemWindows="false">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@ id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/public_white"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@ id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/public_background"
            android:paddingBottom="@dimen/public_dim20"
            app:contentScrim="#000000"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
            app:titleEnabled="false">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/public_white"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@ id/player_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <!--<View-->
                    <!--    android:id="@ id/video_player"-->
                    <!--    android:layout_width="match_parent"-->
                    <!--    android:layout_height="@dimen/public_height_210dp" />-->

                    <com.aliyun.player.alivcplayerexpand.widget.AliyunVodPlayerView
                        android:id="@ id/video_player"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/public_dim420" />

                    <!--<RelativeLayout-->
                    <!--    android:id="@ id/back_layout"-->
                    <!--    android:layout_width="wrap_content"-->
                    <!--    android:layout_height="@dimen/public_height_50dp"-->
                    <!--    android:gravity="left">-->

                    <!--    <ImageView-->
                    <!--        android:layout_width="wrap_content"-->
                    <!--        android:layout_height="wrap_content"-->
                    <!--        android:layout_centerVertical="true"-->
                    <!--        android:layout_marginLeft="@dimen/public_margin_10dp"-->
                    <!--        android:layout_marginRight="@dimen/public_margin_10dp"-->
                    <!--        android:src="@mipmap/public_ic_arrow_back_white_24dp" />-->

                    <!--</RelativeLayout>-->

                </RelativeLayout>

                <TextView
                    android:id="@ id/tv_coure_name_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/public_dim32"
                    android:textColor="@color/public_main_text_color" />

                <TextView
                    android:id="@ id/tv_introduction_name_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingStart="@dimen/public_dim32"
                    android:paddingEnd="@dimen/public_dim32"
                    android:paddingBottom="@dimen/public_dim32"
                    android:textColor="@color/public_main_text_color" />

            </LinearLayout>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="exitUntilCollapsed|enterAlways|enterAlwaysCollapsed">

            <net.lucode.hackware.magicindicator.MagicIndicator
                android:id="@ id/magic_indicator"
                android:layout_width="match_parent"
                android:layout_height="@dimen/public_dim100" />

        </LinearLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@ id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/public_white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>


DragCallback界面允许选择是否通过滚动到AppBarLayout来控制兄弟滚动视图,您可以通过调用来定义一个:

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控制,注意:在调用它之前,应该检查ViewCompat.isLaidOut(appBarLayout),否则params.getBehavior()将返回null,


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+