如何在协调器布局中拉伸内容以填充空白空间?

问题描述

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

enter image description here

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 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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)