Android-数据绑定适配器中的传递视图

问题描述

我有一个图像视图,它上面有一个复选框,类似于您在多选图像库中的图像。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="entity"
            type="a.l.s.v.entity.Image" />
        <variable
            name="viewmodel"
            type="a.l.s.v.image.Imageviewmodel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout>
        <ImageView
            android:id="@+id/image"
            ...
            app:loadImage="@{entity.uri}"/>
        <CheckBox
            android:id="@+id/imageCheckBox"
            ...
            app:visibleIf="@{viewmodel.showCheckBox}"
            app:depressViewOnSelect="what goes here?"/> <!-- Can we pass views here? -->

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

目标:我正试图长按此按钮以降低图像(添加一些视图填充),以作为UX确认的方式。

enter image description here

问题:我想到的一个想法是通过适配器app:depressViewOnSelect在复选框上进行数据绑定,然后从上述布局中传入ImageView 。我不明白的是,如果要传递view参数,应该用XML写什么?数据绑定有可能吗?

@JvmStatic
@BindingAdapter("app:depressViewOnSelect")
fun depressViewOnSelect(checkBox: CheckBox,view: View) {
    checkBox.setonCheckedchangelistener { _,isChecked ->
        if(isChecked)
            view.setPadding(dpToPx(view.context,padding))
        else
            view.setPadding(0)
    }
}

编辑:目前,我已经在实现onLongClick()的{​​{1}}的{​​{1}}方法添加了侦听器。这种方法的问题在于,当长按仅发生在单个视图持有者上时对图像实施拖动选择时,这意味着侦听器未安装在其他视图持有者上。

解决方法

我添加了一个view变量以从布局中访问它,并将其绑定为binding.view = imageView实现中的ViewHolder

<data>
    <variable
        name="entity"
        type="app.lanet.sails.home.image.model.Image" />
    <variable
        name="view"
        type="android.widget.ImageView" />
    <variable
        name="counter"
        type="app.lanet.sails.home.common.media.selection.SelectionCounter" />
</data>

最后,将视图作为XML中任何其他数据绑定参数app:depressViewOnSelect="@{view}"传递。