如何在使用设计支持库时通过编程启用/禁用工具栏滚动

41

我使用 support design 库来在一个 fragment 中滚动 recyclerView 时显示/隐藏工具栏,如此处所述 https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.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:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_scrollFlags="scroll|enterAlways"/>

        </android.support.design.widget.AppBarLayout>


        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

它运行得很好,现在我想通过编程启用/禁用工具栏滚动,因为我使用的是片段,并且我需要为某些片段停止此功能。

2个回答

73

ToolbarAppBarLayout的子元素,其LayoutParamsAppBarLayout获取。这些布局参数具有在XML中设置的滚动标志。

因此,您需要从Toolbar获取AppBarLayout.LayoutParams,并调用setScrollFlags()将标志更改为所需值。

    Toolbar toolbar = findViewById(R.id.toolbar);  // or however you need to do it for your code
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);  // clear all scroll flags

4
要在运行时添加滚动功能,请使用常量 从这里 中的相同方法。 - Mauker
我想要相同的行为。它起作用了,但我遇到的问题是,当我将标志设置为0时,嵌套的滚动视图的高度会减小,并在内容底部创建一个巨大的空白空间。如果有人知道,请帮忙解决。 - Vineet Ashtekar
@VineetAshtekar 你可以尝试同时从NestedScrollView中移除appbar_scrolling_view_behavior。由于工具栏不再滚动,因此没有必要将NestedScrollView布局得像它可以滚动一样。我不确定如何以编程方式实现这一点;我正在研究中。 - kris larson
由于某种原因,当我使用RecyclerView时,这个功能对我来说无法正常工作。 - Mauker
1
在支持版本25.3.1之后,需要调用appBarLayout.requestLayout()才能生效。 - Chaos
显示剩余2条评论

12

在任何你想禁用任务栏滚动的地方,删除app:layout_scrollFlags="scroll|enterAlways"


8
我愿意以编程的方式来实现。 - Mohamad Shaker
如果您想在xml中禁用它,可以像这样设置: app:layout_scrollFlags="enterAlwaysCollapsed" - stevyhacker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接