如何将 ViewPager2 与 CollapsibleToolbar 布局一起使用

How to use ViewPager2 with CollapsibleToolbar layout

我正在尝试使用 collapsible toolbar 布局实现一些目标,我有 2 个带有 viewPager2

的选项卡

以下是 expanded screenshot and collapsed screenshot.

情况下的两张图片

下面是我在 XML 中的代码,不确定哪里出错了...在这种情况下我的卷轴无法正常工作。我希望工具栏在折叠时保持在顶部

我尝试在 AppbarLayout 中添加 toolbar,但没有成功。

也试过下面的链接 不知何故对我不起作用 Link2

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_dark_blue">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white_cream_darkest_blue"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:windowSoftInputMode="adjustResize">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white_cream_darkest_blue"
                app:collapsedTitleTextAppearance="@color/text_color"
                app:contentScrim="@color/white_cream_darkest_blue"
                app:expandedTitleTextAppearance="@color/text_color"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:statusBarScrim="@color/white_cream_darkest_blue">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_start"
                    android:layout_marginTop="40dp"
                    android:layout_marginEnd="@dimen/margin_end"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textViewTitle"
                        style="@style/screen_title"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="30dp"
                        android:text="Mailing\nAddress"
                        android:textColor="@color/text_color" />

                    <TextView

                        android:id="@+id/textViewSubTitle"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="10dp"
                        android:fontFamily="@font/roboto_light"
                        android:text="We'll send your bill to this address."
                        android:textAlignment="center"
                        android:textColor="@color/text_color"
                        android:textSize="20sp"
                        android:visibility="gone" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="30dp" />
                </LinearLayout>

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_marginStart="@dimen/margin_start"
                    android:layout_marginEnd="@dimen/margin_end"
                    android:windowSoftInputMode="adjustResize"
                    app:contentInsetStartWithNavigation="0dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/text_service"
                    app:title="">

                    <TextView
                        android:id="@+id/menuOption"
                        style="@style/screen_sub_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_medium"
                        android:text="Mailing Address"
                        android:textColor="@color/text_color"
                        android:textSize="20sp"
                        android:visibility="gone" />


                </androidx.appcompat.widget.Toolbar>

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayoutMailingAddress"
                    style="@style/tabLayoutStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_start"
                    android:layout_marginEnd="@dimen/margin_end"
                    app:layout_constraintTop_toTopOf="parent"
                    app:tabInlineLabel="true"
                    app:tabTextAppearance="@style/MyCustomTextAppearance" />

                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/viewPagerMailingAddress"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintHeight_percent="1.2"
                    app:layout_constraintTop_toBottomOf="@+id/tabLayoutMailingAddress" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
</layout>

ViewPager2NestedScrollView 中无法正常工作,因为其内部 RecyclerView 默认启用嵌套滚动。

要修复错误行为,您需要以编程方式禁用 ViewPager2 RecyclerView 的嵌套滚动,因为它在布局中不可访问:

科特林:

viewPager.children.find { it is RecyclerView }?.let {
    (it as RecyclerView).isNestedScrollingEnabled = false
}

Java:

for (int i = 0; i < viewPager.getChildCount(); i++) {
    View child = viewPager.getChildAt(i);
    if (child instanceof RecyclerView) {
        ((RecyclerView) child).setNestedScrollingEnabled(false);
        break;
    }
}