android - Android 滚动工具栏

标签 android scroll toolbar

在 TabLayout 中,我有 2 个选项卡,第一个是带有 recyclerView 的 FrameLayout,第二个是带有 LinerLayout 的 ScrollView。当我在任何选项卡中滚动时,我需要隐藏工具栏。当我在第一个选项卡工具栏中滚动 RecyclerView 时也会滚动,但是当我在第二个选项卡中滚动时它不会。不明白为什么。

我有这个 main_acrivity.xml

<FrameLayout 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:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            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="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:tabIndicatorColor="@android:color/background_light"
                app:tabSelectedTextColor="@android:color/background_light"
                app:tabTextColor="@android:color/background_light"/>

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



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

</FrameLayout>

最佳答案

新的滚动行为不适用于常规 android.widget.ScrollView,因为它不支持嵌套滚动。

滚动行为依赖于支持嵌套滚动的 View ,它需要将滚动事件向上传播到 View 树,以便工具栏知道何时向上滑动和隐藏。

它与 RecyclerView 一起工作的原因是它默认支持嵌套滚动。

您需要的是 NestedScrollView :

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

因此,在具有 ScrollView 的布局中,将其替换为 android.support.v4.widget.NestedScrollView, ScrollView 行为将按预期工作:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- your content here...... -->

</android.support.v4.widget.NestedScrollView>

关于android - Android 滚动工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36506969/

相关文章:

java - Android 如何正确添加多个按钮监听器

java - 使用 JavaMail 发送邮件到 @hotmail.com/@live.com

jquery - 从另一个页面链接到固定标题下的 anchor

ios - 具有 "More"按钮样式的 UIBarButtonItem

Android:拖动多个 View

android - 获取 Firebase Android 评分的平均值

java - 创建一个简单的自定义 ScrollView

css - 棘手的 CSS 滚动长度

wpf - 无法隐藏工具栏夹点

java - 无法更改工具栏的颜色