[英]Android - Pass view in Data Binding Adapter
I've a image view with a checkbox on top of it, something similar to what you'd have on a multi-selection image gallery.我有一个顶部带有复选框的图像视图,类似于您在多选图像库中的图像视图。
<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>
Goal: I'm trying to depress the image (add some view padding) when long pressed as a means of UX acknowledgment.目标:我试图在长按时压低图像(添加一些视图填充)作为 UX 确认的一种方式。
Issue: One of the ideas that came across my mind was to do a data binding on checkbox via an adapter app:depressViewOnSelect
and pass in the ImageView
from the above layout.问题:我想到的一个想法是通过适配器
app:depressViewOnSelect
并从上述布局中ImageView
。 What I fail to understand is what should I write in XML if I were to pass in the view
parameter?我不明白的是,如果我要传入
view
参数,我应该在 XML 中写什么? Is it possible at all with data binding?数据绑定有可能吗?
@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)
}
}
Edit: Currently I've added the listener within onLongClick()
method of my RecyclerView.ViewHolder
, which implements the OnLongClickListener
.编辑:目前我已经在我的
RecyclerView.ViewHolder
的onLongClick()
方法中添加了监听器,它实现了OnLongClickListener
。 The problem with that approach comes when implementing a drag-select for images when the long press happens only for a single view holder, which means the listener is not installed on other view holders.当长按仅针对单个视图持有者发生时,该方法的问题出现在实现图像的拖动选择时,这意味着侦听器未安装在其他视图持有者上。
I added a view
variable to access it from layout and bound it as binding.view = imageView
within ViewHolder
implementation.我添加了一个
view
变量以从布局中访问它,并将其绑定为在ViewHolder
实现中的 binding.view binding.view = imageView
。
<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>
Finally, pass the view as any other data binding parameter app:depressViewOnSelect="@{view}"
in the XML.最后,将视图作为 XML 中的任何其他数据绑定参数
app:depressViewOnSelect="@{view}"
传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.