如何在 coordinatorlayout 中停止滚动工具栏

How to stop scrolling toolbar in coordinatorlayout

我正在制作一个简单的应用程序。我使用CoordinatorLayout 和AppbarLayout,AppbarLayout 包含两个Toolbar。

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hi"/>
        </LinearLayout>

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:visibility="gone"
        app:layout_collapseMode="pin"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sign"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hil"
            />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

   </android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="call"/>


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

public void call(View view) {
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar1);
Toolbar toolbar2=(Toolbar)findViewById(R.id.toolbar2);
toolbar.setVisibility(View.GONE);
toolbar2.setVisibility(View.VISIBLE);

}

正常情况下,First toolbar会滚动,但是当我点击NestedScrollView中的按钮时。第二个工具栏应该显示,工具栏必须固定在顶部不想滚动。

怎么办?

正如@kris Larson 所说。

工具栏作为 AppBarLayout 的子项,从 AppBarLayout 获取其 LayoutParams。这些布局参数具有在 XML.

中设置的滚动标志

因此,您从工具栏中获取 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(0);  // clear all scroll flags