【问题标题】:Hiding toolbar & tablayout tabs on scrolling up向上滚动时隐藏工具栏和选项卡布局选项卡
【发布时间】:2017-08-22 21:25:01
【问题描述】:

我尝试克隆 Google Playstore 用户界面。 所以,我有一个 NavigationView、CoordinatorLayout -> AppBarLayout -> 工具栏、TabLayout、ViewPager。一个选项卡有一个布局,其中在 1 个垂直 RecyclerView 中有 n 个水平 RecyclerView。

我的问题:

  1. 如果垂直列表中水平列表项不可见的区域(所附屏幕截图中的灰色背景)被触摸以滚动,则工具栏会在向上滚动时隐藏。如果我通过触摸内部列表向上滚动,则会发生滚动,但工具栏不会隐藏。

  1. 如何在向上滚动时隐藏选项卡标题。

我的布局文件:

activity_navigation_drawer.xml: 主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/layout_main_content_nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header_navigation_drawer"
        app:menu="@menu/menu_navigation_drawer"/>

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

layout_main_content_nav_drawer.xml: 包含在上述布局中

<?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"
    android:fitsSystemWindows="true"
    tools:context="com.aishwarya.materialdesign1.NavigationDrawerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

    <include layout="@layout/layout_content_nav_drawer"/>

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

layout_content_nav_drawer.xml: 包含在上述布局中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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_navigation_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.aishwarya.materialdesign1.NavigationDrawerActivity"
    tools:showIn="@layout/layout_main_content_nav_drawer">

    <FrameLayout
        android:id="@+id/fl_main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

fragment_home_screen.xml: 包含 TabLayout 和 ViewPager 的片段。我从我的主要活动中打开它以显示内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tl_sliding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/color_primary"
        app:tabTextColor="@android:color/black"
        app:tabIndicatorColor="@color/color_primary"
        app:tabIndicatorHeight="2dp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</LinearLayout>

【问题讨论】:

  • 谷歌的投掷行为,或者你可以试试 layout_scrollFlags
  • 好吧,如果你仔细阅读我的代码,我使用了 layout_scrollFlags。问题是我想要的输出只有当我在滚动时触摸主列表的灰色背景区域时才会出现,而不是当我触摸内部列表时,即主列表的项目。所以我觉得有一些与嵌套的recyclerviews有关。
  • 在子视图上尝试 nestedScrollingEnabled(false)。
  • @SouravGanguly 非常感谢! Here 是对其他人有用的链接。
  • 我的第二个问题呢?

标签: android material-design android-toolbar android-tablayout


【解决方案1】:

你可以轻松调用

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

【讨论】: