我想在主活动中创建几个片段,每个片段都有一个RecyclerView,以便我可以在它们之间滑动.同样在这个活动中,我在所有这些东西的顶部有一个正在折叠的工具栏.

  1. 我try 直接在ViewPager中使用RecyclerView,但不幸的是工具栏没有崩溃(我认为它需要NestedScrollView)
  2. 我在ViewPager之外添加了NestedScrollView,这给了我一个向任何RecyclerViews java.lang.IndexOutOfBoundsException添加新项目的例外,我发现这是调用runOnUiThread()导致的错误,但如果我使用UI线程加载数据,速度会太慢
  3. 我try 在每个片段中使用NestedScrollView(包装RecyclerViews),但它给出了相同的异常

有没有办法在没有NestedScrollView的情况下折叠工具栏?

推荐答案

看看这layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@color/colorPrimary">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:id="@+id/lnr_expandView"
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:minHeight="130dp"
                android:orientation="horizontal"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7">

            </LinearLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                app:collapseIcon="@drawable/ic_back"
                app:contentInsetStart="30dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/googlesans_medium"
                    android:padding="@dimen/pad_15"
                    android:text="@string/app_name"
                    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>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.viewpager.widget.ViewPager
            android:id="@id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

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

第二个父-子布局必须包含此属性才能产生折叠效果;

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

Android相关问答推荐

Jetpack编写日期 Select 器

如何在点击按钮时将字符串插入到文本字段中的光标位置?

Android Gradle/Groovy,如何将文件复制到APK

在模块中找到重复的类com.google.Firebase.auth.ktx.AuthKt||Android Studio

如何删除Jetpack Compose中的Textfield底线

StateFlow和LazyColumn重组

我无法连接到信号机

需要 java 17 而不是 java 11:Android CI-CD GitHub Actions

Jetpack Compose 中的用户在线指示器

Android WebView 不会在滚动端加载新内容

我的自定义小吃店不适合我的全宽屏幕尺寸

状态更改后 colored颜色 未正确更改

如何添加到先前预填充的 Room 数据库?

如何像 XML 一样在 Compose Android Studio 中折叠/展开小部件代码区域/区域

Compose `Icons.Outlined.Star` 没有概述

TextField 溢出和软包装不适用于 Compose 约束布局

我应该使用 Bluetooth Classic 还是 Bluetooth LE 与我的移动应用程序通信?

android 13 版本是否会影响 android 12 目标应用程序

如何在 Jetpack Compose 中禁用 TabRow 或 Tab 中的涟漪效应?

在 Jetpack Compose 中 Select 要省略的文本