首页 > 解决方案 > 如何使用内部片段更改 CollapsingToolbarLayout 的高度?

问题描述

我有一个带有 CollapsingToolbarLayout 和内部片段的活动。CollapsingToolbarLayout 具有wrap_content高度。片段高度也设置为wrap_content。加载所有数据后,片段高度会发生变化,但 CollapsingToolbarLayout 的高度仍与以前一样。正因为如此,片段的一部分被剪切了,而其中的一部分对用户来说是不可见的。

我的想法是,在片段中我有观察者,当数据加载时,我通过监听器将测量的高度发送到活动。在那里我试图为片段设置 LayoutParams 并在片段和 CollapsingToolbarLayout 上使用invalidate()requestLayout()方法,但它不起作用。

你知道如何解决这个问题吗?

 <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

                <include layout="@layout/include_toolbar"/>

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

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

        <FrameLayout
            android:id="@+id/other_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

片段.xml

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

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:contraints=".." />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/comments_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contraints=".."/>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-layoutandroid-fragmentsandroid-collapsingtoolbarlayout

解决方案


推荐阅读