【问题标题】:How to make TabLayout scrollable in Fragment?如何使 TabLayout 在 Fragment 中可滚动?
【发布时间】:2017-02-24 22:53:49
【问题描述】:

我在 Activity 中有 DrawerLayout,并且从 NavigationView 打开了不同的片段。 其中一个片段包含 TabLayout。 当页面滚动时,工具栏向上滚动(隐藏), 虽然我还需要隐藏 TabLayout。有很多示例是通过 Activity 实现的,而我在 Fragment 中有 TabLayout,需要像在 Play Store 中一样滚动 Toolbar 和 TabLayout 滚动。 这是布局activity_main.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/main_screen"
    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/navigation_header"
    app:menu="@menu/navigation_menu" />

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

main_screen.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"
tools:context=".view.activity.MainActivity">

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFF00"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#0000FF"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

main_content.xml 我在其中添加/替换片段

   <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>

这个 xml 是其中一个片段的内容之一,它包含我希望可滚动的 TabLayout

<?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/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabMode="scrollable" />

<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" />
</LinearLayout>

【问题讨论】:

  • 如果您正在寻找它,请告诉我。
  • 应该在 AppBarLayout 之后

标签: android android-tablayout android-coordinatorlayout


【解决方案1】:

很明显!

不幸的是,您还没有尝试过任何东西,或者只是想查看官方示例等。

无论如何,这应该很简单,因为您只需要从最后一个Layout 中删除TabLayout 并将其替换为Toolbar 并将其余部分留在那里。

最后一件事是添加:

app:layout_scrollFlags="scroll|enterAlways"

用于使TabLayout 可滚动或隐藏。

所以;

<?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"
    tools:context=".view.activity.MainActivity">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFF00"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#0000FF"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabMode="scrollable" />

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

【讨论】:

  • 你的意思是 TabLayout 应该移动到 main_screen,它是 activity_main 的一部分,在这种情况下,如果从导航菜单打开不同的片段并且只有一个片段需要 TabLayout 我需要为不同的片段显示/隐藏它?
  • 在你的问题中你说; 其中一个片段包含 TabLayout。当页面滚动时,工具栏会向上滚动(变为隐藏,因此您只需要找到该视图并在工具栏下添加此 TabLayout。请先检查我的答案,如果它不起作用,请告诉我们!
【解决方案2】:

只需将 app:tabMode="scrollable" 放入 xml 中即可。

这样

<com.google.android.material.tabs.TabLayout
        android:id="@+id/home_tab_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:tabMode="scrollable"
        app:tabGravity="center"
        app:tabIndicatorGravity="stretch"
        app:tabSelectedTextColor="@color/white"
        app:tabIndicatorAnimationMode="elastic"
        app:tabIndicator="@drawable/tab_indicator"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:tabIndicatorColor="@color/light_green"

        />

【讨论】:

    猜你喜欢
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多