繁体   English   中英

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

[英]How to use ViewPager2 with CollapsibleToolbar layout

我正在尝试通过collapsible toolbar布局实现一些目标,并且我有 2 个带有viewPager2的选项卡

以下是展开屏幕截图折叠屏幕截图示例的两个图像。

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

我尝试在AppbarLayout中添加toolbar ,但这没有用。

还尝试了以下链接,但不知何故对我不起作用Link1 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的嵌套滚动,因为它在布局中不可访问:

Kotlin:

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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM