首页 > 解决方案 > 如何在协调器布局中拉伸内容以填充空白空间?

问题描述

我正在尝试实现如下所示的行为(在图片上),所以我需要拉伸 AppBarLayout 下面的所有内容。我已经通过实现自定义 CoordinatorLayout Behavior 以某种方式实现了它,但是该解决方案在 RecyclerView 内部的视图回收和整体性能方面存在一些问题。有没有更简单的解决方案来实现我想要的?

在此处输入图像描述

public class TestBehaviour48Margin extends CoordinatorLayout.Behavior {

    public TestBehaviour48Margin() {
    }

    public TestBehaviour48Margin(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        AppBarLayout appBarLayout = (AppBarLayout) dependency;
        val toolbar = appBarLayout.findViewById(R.id.toolbar);
        int range = appBarLayout.getTotalScrollRange();
        int totalHeight = parent.getHeight();
        int appBarHeight = appBarLayout.getHeight();
        int toolbarHeight = toolbar.getHeight();

        int initialHeight = totalHeight - appBarHeight;
        int finalHeight = totalHeight - toolbarHeight;
        int differenceHeight = finalHeight - initialHeight;

        float factor = -appBarLayout.getY() / range;

        val layoutParams = child.getLayoutParams();
        int lastHeight = layoutParams.height;
        layoutParams.height = (int) (initialHeight + (differenceHeight * factor)) + LayoutUtils.dpToPx(parent.getContext(), 48);
        if(lastHeight != layoutParams.height){
            child.setLayoutParams(layoutParams);
        }

        return true;
    }
}


   <CoordinatorLayout ... (some content)

   <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="48dp"
                android:layout_gravity="bottom"
                app:layout_behavior=".custom_views.TestBehaviour48Margin">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/custom_pink"
                    android:orientation="vertical">

                    <androidx.recyclerview.widget.RecyclerView
                        android:nestedScrollingEnabled="false"
                        android:id="@+id/rv_strength_items"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:background="@color/grey_B" />

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:background="@color/custom_blue_light" />

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:layout_gravity="bottom"
                        android:background="@color/custom_green"
                        bind:layout_anchor="@id/scroll"
                        bind:layout_anchorGravity="bottom" />

                </LinearLayout>

            </FrameLayout>


            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorSurfaceNight"
                android:visibility="@{viewmodel.views.mainLayoutVisibility}"
                app:elevation="0dp"
              app:layout_behavior=".fragments.training.main_training.view_fold.BlockableAppBarLayoutBehaviour"> 

... rest of code

标签: androidandroid-coordinatorlayoutandroid-appbarlayout

解决方案


推荐阅读