【问题标题】:Android - Pass view in Data Binding AdapterAndroid - 在数据绑定适配器中传递视图
【发布时间】:2020-08-22 19:52:04
【问题描述】:

我有一个顶部带有复选框的图像视图,类似于您在多选图像库中的视图。

<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 确认的一种方式。

问题:我想到的一个想法是通过适配器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)
    }
}

编辑:目前我已经在我的RecyclerView.ViewHolderonLongClick() 方法中添加了监听器,它实现了OnLongClickListener。当长按仅针对单个视图持有者发生时,该方法的问题出现在为图像实现拖动选择时,这意味着侦听器未安装在其他视图持有者上。

【问题讨论】:

    标签: android data-binding


    【解决方案1】:

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

    <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}" 传递。

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 2020-04-24
      • 1970-01-01
      • 2020-09-27
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多