我有一个文本视图,需要为onLongClick创建一个侦听器。现在,对于相应的视图模型,它有一个函数sendLogs(),它处理onClick的逻辑。如果我将onClick更改为onLongClick函数,就永远不会得到调用。有什么办法让它在onLongClick上工作吗?
onClick直接链接到我的模型类函数,而不是onLongClick。所以我认为模型类绑定是正确的,但是我可能需要一些额外的工作。
<data>
<import type="android.view.View" />
<variable
type="com.aaa.bbb.viewmodel.SystemSettingsViewModel"
name="systemSettings"
</variable>
</data>
<TextView
android:gravity="end"
android:id="@+id/tv_logging"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_width="wrap_content"
android:onClick="@{() -> systemSettings.sendLogs()}"
android:text="@string/enable_logs"
android:textAlignment="viewEnd" />
发布于 2017-10-13 01:03:10
我成功地把它做对了。我怀疑这件事是否有适当的记录。
在xml中
android:onLongClick="@{(view) -> presenter.onLongClickOnHeading(view)}"
在演示者视图模型类中
public boolean onLongClickOnHeading(View v) {
//logic goes here
return false;
}
注意:此方法签名应完全采用此格式。否则,等待错误将在运行时抛出。
发布于 2020-07-26 22:05:48
这是完整的代码。
长时间单击没有这样的属性。所以我们必须创建一个绑定适配器。
BindingUtils.kt
object BindingUtils {
private const val ON_LONG_CLICK = "android:onLongClick"
@JvmStatic
@BindingAdapter(ON_LONG_CLICK)
fun setOnLongClickListener(
view: View,
func : () -> Unit
) {
view.setOnLongClickListener {
func()
return@setOnLongClickListener true
}
}
}
布局
<androidx.constraintlayout.widget.ConstraintLayout
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:onLongClick="@{() -> vm.onLongClick()}"/>
发布于 2018-02-11 17:39:48
要使其工作,括号中的部分必须与接口View.OnLongClickListener中的方法签名相匹配,如下所示:
boolean onLongClick(View view);
所以我就是这么让它起作用的:
<View
...
android:onLongClick="@{(view) -> listener.onLongClick(view, viewmodel)}"/>
...
https://stackoverflow.com/questions/46680862
复制