android - 使用 ViewPager 的 CollapsingToolbarLayout 中的 fragment 不会向下滑动

标签 android layout androiddesignsupport

我有一个带有 CollapsingToolbarLayout 和 TabLayout 的 Activity 。当我左右滑动时,它会在 fragment 之间完美移动。但是,当我尝试向下滚动(屏幕截图中的红色箭头)时,它会忽略它。我尝试将 ScrollView 添加到 fragment 中,但并没有什么不同。有什么想法吗?

顺便说一句 - 不知何故,在第二个 fragment RecycleView 上,向下滑动有效。这在右侧屏幕截图中可见:

screenshot

MainActivity 的 XML:

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/root_coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="122dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/rsz_bg_cover"
                    app:layout_collapseMode="parallax" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/app_bar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:layout_collapseMode="pin" />

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tab_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorAccent"
                    app:layout_collapseMode="pin"
                    app:tabIndicatorColor="@color/colorPrimary"
                    app:tabSelectedTextColor="@android:color/white"
                    app:tabTextColor="#EEE" />
            </android.support.design.widget.CollapsingToolbarLayout>

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

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


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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>

最佳答案

试试这个布局。我在 AppBarLayout 中添加了 TabLayout,我相信它应该可以像您想要的那样工作。但是如果你愿意,你可以保留两个 CollapsingToolbarLayout 来实现你想要的行为。并确保 fitsSystemWindows 在所有布局中都应该是相同的 truefalse 否则您可能看不到预期的行为。

<?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"
    android:id="@+id/root_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="122dp"
                android:scaleType="centerCrop"
                android:src="@drawable/rsz_bg_cover"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_collapseMode="pin" />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            app:layout_collapseMode="pin"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="#EEE" />
    </android.support.design.widget.AppBarLayout>

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

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

并且在Fragment布局中,使用NestedScrollView并在其中添加layout_behavior

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

        // Your Layout

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

关于android - 使用 ViewPager 的 CollapsingToolbarLayout 中的 fragment 不会向下滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37520497/

相关文章:

android - 在android中检查用户是否连接到网络

android - 我所有的xml文件在Android Studio Project中都出错

android - 从 Android 生成 PDF

android - 动态设置时折叠工具栏的标题不会改变?

android - BottomSheetBehavior : The view is not associated with BottomSheetBehavior

android - 如何在 CalendarView 中仅显示日期栏?

html - CSS根据另一个div的动态高度定位一个div

wpf - 带有 "UniformToFill"的 WPF Viewbox 内的可滚动内容

html - 表格单元格内的可滚动 DIV

android - 使用 ScrollView(和 NestedScrollView)时 CollapsingToolbarLayout 不折叠