簡體   English   中英

屏幕滾動時隱藏和顯示工具欄

[英]Hide and Show toolbar on scrolling of screen

我在linearlayout有一個選項卡。 Linearlayout位於屏幕底部和toolbar 我希望當屏幕上的項目超出容納的數量並且屏幕scrolls ,較低的選項卡linearlayout應該隱藏,當我scroll時,它應該可見。 我正在嘗試下面的代碼,但是它似乎沒有用。

 <android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?android:actionBarSize"
    android:layout_gravity="bottom"
    android:background="@android:color/transparent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_scrollFlags="scroll|enterAlways">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="visible"
        app:layout_behavior="com.imi.utils.ScrollingToolbarBehavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <include
            android:id="@+id/bottomBarLayout"
            layout="@layout/activity_custom_bottom_navigation" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

嘗試使用

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

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

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

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

        <YourCustomtLinearLayout
        ...
        app:layout_behavior="com.imi.utils.ScrollingToolbarBehavior" 
        />

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

您必須使用setOnScrollChangeListener方法來隱藏和顯示工具欄。 最初,您必須為工具欄視圖設置可見性。 接下來使用這種方法

 appBarLayout.setVisibility(View.GONE);
            scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                if (scrollY > oldScrollY) {
                    appBarLayout.setVisibility(View.VISIBLE);
                }
                if (scrollY == 0) {
                    appBarLayout.setVisibility(View.GONE);
                }
            }
        });

暫無
暫無

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

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