使用设计支持库时如何以编程方式启用/禁用工具栏滚动 [英] How to enable/disable toolbar scrolling programmatically when using design support library
问题描述
在片段中滚动recyclerView时,我使用支持设计库来显示/隐藏工具栏,如此处所述 https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout
I use support design library to show/hide toolbar when scrolling a recyclerView inside a fragment, as mention here 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>
它可以正常工作,现在我想以编程方式启用/禁用工具栏滚动,因为我使用了片段并且需要为某些片段停止该功能。
It works grate, Now I want to enable/disable toolbar scrolling programmatically because I use fragments and I need to stop this feature for some fragments.
推荐答案
工具栏
AppBarLayout
的子项,从 AppBarLayout
LayoutParams >。这些布局参数具有在XML中设置的滚动标志。
The Toolbar
, being a child of the AppBarLayout
, gets its LayoutParams
from the AppBarLayout
. These layout params have the scroll flags that are set in the XML.
因此,您将获得 AppBarLayout.LayoutParams
从工具栏
,然后调用
setScrollFlags()
将标志更改为所需的值。
So, you get the AppBarLayout.LayoutParams
from the Toolbar
, and call
setScrollFlags()
to change the flags to the value you want.
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(0); // clear all scroll flags
这篇关于使用设计支持库时如何以编程方式启用/禁用工具栏滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!