簡體   English   中英

如何在CoordinatorLayout中停止滾動工具欄

[英]How to stop scrolling toolbar in coordinatorlayout

我正在制作一個簡單的應用程序。 我使用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中的按鈕時。 第二個工具欄應顯示,工具欄必須固定在頂部不想滾動。

該怎么辦?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM