How can I stretch content inside Coordinator layout to fill empty space?

Viewed 268

I am trying to implement behaviour which is shown below (on pictures), so I need to stretch all content below AppBarLayout. I've already achieved it somehow by implementing custom CoordinatorLayout Behaviour but that solution have some issues with views recycling inside RecyclerView and overall performance. Is there easier solution to achieve what i want?

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 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
0 Answers
Related