I have a simple list activity, for each list item I have onTouchEventListener class defined.
The problem is onFling event never occures, instead of it following sequence of events:
onDown, onShowPress, onLongPress. When I do the same but assign onTouchEventListener to the ListView instance then onFling happens as well but not when I assign it to the list items :(
Any thoughts?
Because of a possible bug, onDown method is needed to return true to make sure, that other events also occur. Thus, make sure your custom onDown always returns true.
Related
I know this got asked often, but all solutions(, I found,) don't work for me.
What I have is a CardView with an OnClickListener making a Toast (#toast1).
Inside the CardView there are multiple views as also a WebView.
As mentioned elsewhere, to pass through the click through the WebView to the CardView I have done following:
Set android:clickable="false" in WebView XML
insert following under CardView.setOnClickListener(...)
WebView.setOnTouchListener( (view, event) -> true);
I also replaced the lambda with an anonymous method, to see if its just this. No change.
What happens now is:
At the border and over the other views, the clickListener is triggered and the toast appears
Over the webView the clickListener isn't triggered.
Also put a toast (#toast2) in touchLstener of WebView before returning true, and it gets triggered.
What I expect:
Click will passed through WebView
With #toast2 added: First show #toast2 then #toast1
What is a bit confusing, that in documentation of OnTouchListener, the return is following:
True if the listener has consumed the event, false otherwise.
For me that means:
true: Don't pass click to below views, as listener consumed it
false: Pass click to below views, as listener didn't consumed it
But setting to false didn't change anything.
First of all I would suggest you to get familiar with android touch handling system - you can find a really good description in this answer. To sum it up: touch event propagation starts on top level of hierarchy, but actual handling of the touch event starts on the lowest level of view hierarchy. As for solution of your problem I may suggest to sublcass the parent of your WebView and override onInterceptTouchEvent in the following way:
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
return true;
}
This will instruct this parent view to intercept all touch events that would otherwise go to its children views, thus limiting the first level of touch processing to this view.
I've got a big headache and spent a few hours to solve my problem withou any reasonable results.
I use a custom adapter (extending ArrayAdapter) to show listview items. This adapter implements OnTouchListener. There's also a background selector which color is changing when an item is touched and OnItemClickListener in ListViewActivity.
What I need is capture touch events (all of them, not only ACTION_DOWN) in the adapter. When onTouch returns false, the consecutive events (ACTION_CANCEL, ACTION_UP etc.) aren't captured. On the other hand returning true stops dispatching all other events (e.g. click) so that the ListViewActivity's onItemClick is never triggered.
I tried hard to find any working solution in SO and other resources, but with no success.
One idea was not worry about click events and background. I may set the background programatically and trigger click event the same way when the onTouch action is ACTION_UP, but view.performClick() does nothing (view is the 1st argument of onTouch method).
[EDIT]
To make it clear, I want to handle touch events in the adapter beacuse I need the textview in the item to marquee when the user touches it. Thus, in onTouch, when the action equals ACTION_DOWN, I assign true to setSelected property of the textview and, consequently, false when ACTION_CANCEL or ACTION_UP.
Any suggestions?
Thanks in advance.
I have a listView. In that listView i have implemented onTouchListener. Every things work fine, but when list is Scrolling , on tapping on the list , the list should stop(like normal android listView).No action can be done while scrolling like stopping the scroll.This happen because of the onTouchListener. I am returning false from onTouchListener. I don't know why this behavior happens?Can any one help.
try returning super.onTouchListener, or true, when you don't want to consume the event (i'm thinking you implemented gestures). returning false means you're discarding the TouchEvent, and you don't want to do that if you want to stop the scroll.
I need to differentiate between programmatically scrolling a list view and manually scrolling a list view. I am not quite sure how to do it. Any idea ?
Thanks.
I guess a way would be to do something like add onscrollchanged listener and onTouch listener to the listview. And keep a boolean like fromUser.
In the onTouch listener you can set the flag to true on MotionEvent.ActionDown. When the MotionEvent is ActionUp you can set the flag to false.
So whenever the scroll listener is fired you can check the flag and see whether its from the user or not.
I a view have for which I would like to show a ContextMenu on a longPress. I was able to get this ContextMenu to display using the recommended method of: calling activity.registerForContextmenu and overriding onCreateContextMenu(...).
However, I would like to do other things on other touch events, so my view has a TouchHandler assigned to it. When this touch handler is set, the onCreateContextMenu() never gets called (presumably because my TouchHandler is eating the longPress). So, is there anyway for me to instantiate and show a ContextMenu without the onCreateContenxtMenu() method being called?
Alternatively, I could just show my own custom dialog with my "menu" items. Is there any disadvantage to using a custom dialog instead of the ContextMenu?
One thing to try, is to return false from your OnTouchListener if you don't want the event to be consumed.
What do you return from OnTouchListener.OnTouch? Returning false means you haven't consumed the event, which should mean that other actions can be peformed on it as well.