Как разместить элемент и подогнать под него фон в кастомном фрейм лаяуте?

Всем привет! Мне необходимо реализовать кастомный лаяут для свайпа. Я думаю взять за основу FrameLayout и установить в него две вью программно (вью фона и вью контента, которую, собственно будет свайпать).

Условный фон:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/blue"
    app:cardCornerRadius="12dp">

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

        <ImageView
            android:id="@+id/sb_iv_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:src="@drawable/ic_share"
            app:layout_constraintBottom_toTopOf="@id/sb_tv_share"
            app:layout_constraintEnd_toEndOf="@id/sb_tv_share"
            app:layout_constraintStart_toStartOf="@id/sb_tv_share"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/sb_tv_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="12dp"
            android:text="Поделиться"
            android:textColor="#fff"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/sb_iv_share" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Вью контента может быть любой. Задача в том, чтобы вью фона растягивалась под размеры передней. В итоге должно получиться так, что я навешу свайп листенер на передний план и, когда он будет смещаться влево или вправо, то будет становиться видным задний фон.

Можно ли получить такое поведение? Подскажите, пожалуйста, в каких коллбэках?

Пока, что у меня есть:

class SwipeItemLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val backgroundView = ShareBackgroundBinding.inflate(
        LayoutInflater.from(context)
    ).root

    private lateinit var contentView: View

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        // Должны мы установить размеры здесь?
        super.onLayout(changed, left, top, right, bottom)
    }

    companion object {
        fun configure(contentView: View): SwipeItemLayout {
            return SwipeItemLayout(contentView.context)
                .apply {
                    this.contentView = contentView
                    addView(this.contentView)
                    addView(
                        ShareBackgroundBinding.inflate(
                            LayoutInflater.from(context)
                        ).root
                    ) // Может, здесь установить какие-то размеры?
                }
        }
    }

}

P.S: Прошу не писать про ItemTouchHelper, мне необходим именно лаяут


Ответы (0 шт):