【问题标题】:How to prevent hide toolbar by scroll itself如何防止通过滚动本身隐藏工具栏
【发布时间】:2017-11-30 06:47:31
【问题描述】:

我对 CoordinatorLayout 和工具栏的行为有疑问。 像往常一样,我想通过滚动隐藏/显示工具栏。它工作正常。但如果我扔在工具栏本身上,它将独立于 recyclerView 或其他所有东西隐藏。 如何通过触摸工具栏本身来防止工具栏隐藏/显示行为。

这是解释我的问题的视频。 https://youtu.be/3bFcy2SF3Nk

这里的更多信息是我的布局文件中的 sn-p

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/action_bar_height"
            android:background="@color/white"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/padding"
                android:src="@drawable/logo_dark" />

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </FrameLayout>

【问题讨论】:

    标签: android material-design toolbar appbar


    【解决方案1】:
    <ScrollView 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="fill_parent"
        android:layout_height="fill_parent"
        android:fitsSystemWindows="true"
      >
        <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
            android:background="@color/white">
    
            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/action_bar_height"
                    android:background="@color/white"
                    app:contentInsetStart="0dp"
                    app:layout_scrollFlags="scroll|enterAlways|snap">
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="@dimen/padding"
                        android:src="@drawable/logo_dark" />
    
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.AppBarLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </FrameLayout>
    </ScrollView>
    

    【讨论】:

    • 感谢您的回答,但这对我不起作用。我必须使用 CoordinatorLayout 来实现工具栏和底部导航的正确行为。RecyclerView insode ScrollView 不是一个好习惯。并且在应用你示例工具栏后仍然隐藏它。
    【解决方案2】:

    好的,我看到了你的视频,我过去也遇到过同样的问题 答案是这样的: 当您单击底部栏中的图标以加载新片段时,请执行以下操作:

    禁用:

      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
    

    当点击底部栏中的主页按钮时,启用滚动工具栏:

    启用:

    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
        | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    

    【讨论】:

    • 谢谢你的回答,它可能可以工作,但不是那么清楚的解决方案。禁用启用后对我来说不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多