与 AppBarLayout 和折叠工具栏一起使用时,ViewPager2 的大小错误

ViewPager2 has wrong size when using with AppBarLayout and collapsing toolbar

我有这样的布局:

<?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"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fitsSystemWindows="true"
   tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/OfferingsMaterialTheme.AppBarOverlay"
    android:fitsSystemWindows="true">

    <androidx.appcompat.widget.Toolbar
      android:id="@+id/toolbar"
      app:layout_scrollFlags="scroll|enterAlways"
      android:layout_width="match_parent"
      android:layout_height="?android:attr/actionBarSize"
      android:background="?android:attr/colorPrimary"
      app:titleTextColor="@color/icons"
      app:title="@string/offeringsfragment_title"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/viewpager"
    app:layout_anchorGravity="bottom"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我在工具栏中使用 app:layout_scrollFlags="scroll|enterAlways",因此一旦用户向上滚动它就会折叠(ViewPager2 中有一个 RecyclerView)。问题是,ViewPager2 中视图的底部是不可见的。 身高好像计算错了看这个:

在红色部分,您可以看到 ViewPager2 扩展到屏幕大小。我读了很多关于它的帖子,但找不到 ViewPager2 的解决方案。其中,我尝试将它包装在 LinearLayout 中,但工具栏不再折叠。有什么想法吗?

对于有同样问题的人:ViewPager2 似乎在计算正确的高度时有错误。唯一对我有用的解决方案是 this。在这里,我们这样做:

我找到的这个问题的解决方案涉及两个部分。

  1. 将等于 AppBarLayout 高度的填充添加到 NestedScrollView 的底部。在我的例子中,因为 AppBarLayout 只包含一个 Toolbar,高度是 ?attr/actionBarSize.
    android:paddingBottom="?attr/actionBarSize"

  2. 将自定义 AppBarLayout.OnOffsetChangedListener 添加到 AppBarLayout,它会在工具栏折叠时更改 NestedScrollView 的高度。

     class ScrollingOffsetFixListener(
         private val nestedScrollView: NestedScrollView
     ): AppBarLayout.OnOffsetChangedListener {
    
     private var originalHeight = 0
     private var firstOffset = true
    
     override fun onOffsetChanged(layout: AppBarLayout?, offset: Int) {
         if(firstOffset) {
             firstOffset = false
             originalHeight = nestedScrollView.measuredHeight
         }
    
         val params = nestedScrollView.layoutParams
         params.height = originalHeight + (offset * -1)
    
         nestedScrollView.layoutParams = params
        }
     }