如何使用数据绑定直接在 xml 布局文件上设置 onTouchListener?
How can I set the onTouchListener directly on the xml layout file using data binding?
我已经成功地直接添加了 onClick
个监听器
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{action::onItemClicked}" />
和 onRefreshListener
听众
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:onRefreshListener="@{action::onRefresh}">
但我不明白为什么我无法执行以下操作
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onTouchListener="@{action::onItemTouched}" />
显示以下错误:
Listener class android.view.View.OnTouchListener with method onTouch did not match signature of any method action::onItemTouched
如果我使用 android:
而不是 app:
或 onTouch
而不是 onTouchListener
,则会出现另一个错误。
然而 onItemTouched
的方法签名在 View.java
源文件中定义:
public void onItemTouched(View v, MotionEvent event) {
// no dice
}
据我所知,TextView
是一个 View
,因此它应该有效:https://android.googlesource.com/platform/frameworks/base/+/a175a5b/core/java/android/view/View.java#14494
那么我做错了什么,为什么这行不通?它应该是不同的属性名称吗?
请不要 @BindingAdapter
或类似的建议,我已经知道该怎么做了。我的 objective 是为了让模型保持整洁,并将方法直接附加到视图上,就像我对点击和滑动刷新所做的那样。
成功了,唯一的问题是方法是 void
,但是 onTouch
签名需要 boolean
return。最后应该是这样的:
布局文件示例
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onTouch="@{action::onItemTouched}" />
或
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onTouchListener="@{action::onItemTouched}" />
动作class方法
public boolean onItemTouched(View v, MotionEvent event) {
// now it works, do your magic
return true; // or return false, depending on what you want to do
}
现在唯一担心的是Android工作室抱怨说android:onTouch
是一个未知的属性,我希望它以后不会因为一些奇怪的原因变成non-public。出于这个原因,我将在布局中使用以下方法来消除警告消息
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onTouch="@{action::onItemTouched}" />
// app:onTouchListener works as well
我已经成功地直接添加了 onClick
个监听器
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{action::onItemClicked}" />
和 onRefreshListener
听众
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:onRefreshListener="@{action::onRefresh}">
但我不明白为什么我无法执行以下操作
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onTouchListener="@{action::onItemTouched}" />
显示以下错误:
Listener class android.view.View.OnTouchListener with method onTouch did not match signature of any method action::onItemTouched
如果我使用 android:
而不是 app:
或 onTouch
而不是 onTouchListener
,则会出现另一个错误。
然而 onItemTouched
的方法签名在 View.java
源文件中定义:
public void onItemTouched(View v, MotionEvent event) {
// no dice
}
据我所知,TextView
是一个 View
,因此它应该有效:https://android.googlesource.com/platform/frameworks/base/+/a175a5b/core/java/android/view/View.java#14494
那么我做错了什么,为什么这行不通?它应该是不同的属性名称吗?
请不要 @BindingAdapter
或类似的建议,我已经知道该怎么做了。我的 objective 是为了让模型保持整洁,并将方法直接附加到视图上,就像我对点击和滑动刷新所做的那样。
成功了,唯一的问题是方法是 void
,但是 onTouch
签名需要 boolean
return。最后应该是这样的:
布局文件示例
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onTouch="@{action::onItemTouched}" />
或
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onTouchListener="@{action::onItemTouched}" />
动作class方法
public boolean onItemTouched(View v, MotionEvent event) {
// now it works, do your magic
return true; // or return false, depending on what you want to do
}
现在唯一担心的是Android工作室抱怨说android:onTouch
是一个未知的属性,我希望它以后不会因为一些奇怪的原因变成non-public。出于这个原因,我将在布局中使用以下方法来消除警告消息
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:onTouch="@{action::onItemTouched}" />
// app:onTouchListener works as well