简体   繁体   English

屏幕滚动时隐藏和显示工具栏

[英]Hide and Show toolbar on scrolling of screen

I have a tabs inside a linearlayout . 我在linearlayout有一个选项卡。 The Linearlayout is at the bottom of the screen and inside a toolbar . Linearlayout位于屏幕底部和toolbar I want that when the screen has more items than it can fit and the screen scrolls ,the lower tab linearlayout should hide and when i scroll complete up, it should be visible. 我希望当屏幕上的项目超出容纳的数量并且屏幕scrolls ,较低的选项卡linearlayout应该隐藏,当我scroll时,它应该可见。 I am trying the below code, but it did not seem to be working. 我正在尝试下面的代码,但是它似乎没有用。

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

try to use 尝试使用

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

You have to use setOnScrollChangeListener method for toolbar hide and show. 您必须使用setOnScrollChangeListener方法来隐藏和显示工具栏。 Intially you have to set visiblity gone for toolbarview. 最初,您必须为工具栏视图设置可见性。 next use this method 接下来使用这种方法

 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