CodingTT
CodingTT

Reputation: 1145

Android coordinatorlayout add layout below RecyclerView

To be able to support the bottom sheet behavior I have to add my entry_field layout below the RecyclerView. The layout_anchor will not work correctly. In this case, the entry_field will block the RecyclerView.

I want the RecyclerView to fill the reset of the spaces and resize with the entry_field

If the entry_field height changes, the RecyclerView should always fit the reset of the space, not blocked by the entry_field

The entry_field MUST to be the direct child of the CoordinatorLayout

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background_gray_lightest">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/messaging_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:overScrollMode="ifContentScrolls"
    android:paddingTop="8dp"
    android:paddingBottom="10dp"
    android:scrollbars="vertical"
    android:layout_gravity="top"
    tools:listitem="@layout/item_message" />

<RelativeLayout
    android:id="@+id/entry_field"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_anchor="@id/messaging_recycler_view"
    app:layout_anchorGravity="bottom">

    <com.abc.newmessage.NewMessageLayout
        android:id="@+id/include_message_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </com.abc.newmessage.NewMessageLayout>
</RelativeLayout>

Upvotes: 0

Views: 340

Answers (1)

Bartek Lipinski
Bartek Lipinski

Reputation: 31458

To accomplish that I think you will have to create your own CoordinatorLayout.Behavior.

Something like (writing this off the top of my head, so you might need to correct that):

class FillBehavior : CoordinatorLayout.Behavior<View> {

    @Suppress("unused")
    constructor() : super()

    @Suppress("unused")
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    private var lastSetHeight = -1

    override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
        return dependency.id == R.id.entry_field
    }

    override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
        val leftVerticalSpace = parent.height - dependency.height
        if (lastSetHeight != leftVerticalSpace) {
            lastSetHeight = leftVerticalSpace;
            child.layoutParams.height = lastSetHeight
            child.requestLayout()
            return true
        } else {
            //nothing to do, available space hasn't changed
            return false
        }
    }
}

and then in your layout:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/messaging_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:overScrollMode="ifContentScrolls"
    android:paddingTop="8dp"
    android:paddingBottom="10dp"
    android:scrollbars="vertical"
    android:layout_gravity="top"
    tools:listitem="@layout/item_message"         
    app:layout_behavior="your.package.FillBehavior"/>

Upvotes: 1

Related Questions