I changed my project to use Android Data Binding.
Everthing works fine except the onTouchListener
on my custom SectorImageView
which inherit AppCompatImageView
. It has to be an OnTouch
event instead of a ClickListener
to handle x and y coordinates of the click.
As i implemented the Android Data Binding none of my listeners worked so i decided to use the XML approach.
Question
Is there any way to handle onTouchListener
by simply adding an OnTouchListener to the class without using XML approach? Why the listener don't get recognized anymore when i implement the Android Data Binding?
Here is some code i tried to work with:
Java code
@BindingAdapter("app:onTouchListener")
public boolean onTouch(View view, MotionEvent event) {
// do something
}
XML layout
<x.y.z.SectorImageView
android:id="@+id/image_sector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
app:onTouchListener="@{(view, event) -> onTouch(view, event)}"
app:srcCompat="@drawable/map" />
Error
****/ data binding error ****msg:Cannot find method onTouch on ViewDataBinding
Try this:
In .java
file add like:
@BindingAdapter("touchListener")
public void setTouchListener(View self, boolean value){
self.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
// Check if PRESSED
if (event.getAction() == MotionEvent.ACTION_DOWN){
// code stuff
}
// Check if RELEASED
else if (event.getAction() == MotionEvent.ACTION_UP) {
// code stuff
}
return false;
}
});
}
In .xml
file add like:
<x.y.z.SectorImageView app:touchListener="@{true}"}"/>