gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:01:42 26 4
gpt4 key购买 nike

我有这个布局:

<?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 中 View 的底部不可见。 高度似乎计算错误。 看到这个:
enter image description here
在红色部分,您可以看到 ViewPager2 扩展到屏幕大小。我阅读了很多关于它的帖子,但找不到 ViewPager2 的解决方案。其中,我试图将它包装在一个 LinearLayout 中,但随后工具栏不再折叠。有任何想法吗?

最佳答案

对于任何有同样问题的人:ViewPager2 似乎在计算正确高度时有一个错误。唯一对我有用的解决方案是 this .在这里,我们这样做:
我发现这个问题的解决方案包括 2 个部分。

  • 添加等于 AppBarLayout 高度的填充到 NestedScrollView 的底部.就我而言,因为 AppBarLayout 只包含一个 Toolbar , 高度为 ?attr/actionBarSize .android:paddingBottom="?attr/actionBarSize"
  • 添加自定义 AppBarLayout.OnOffsetChangedListenerAppBarLayout这改变了 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
    }
    }
  • 关于android - 与 AppBarLayout 和折叠工具栏一起使用时,ViewPager2 的大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62955052/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com