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

时间:2018-11-22 10:45:32

标签: android scroll toolbar android-coordinatorlayout android-appbarlayout

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

<?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);

}

通常,第一个工具栏将滚动,但是当我单击NestedScrollView中的按钮时。第二个工具栏应显示,工具栏必须固定在顶部不想滚动。

该怎么办?

1 个答案:

答案 0 :(得分:1)

如@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