Ad
Data Binding OnTouch In A Layout
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
Ad
Answer
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}"}"/>
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad