I'm trying to completely disable SwipeRefreshLayout when inside the onLongClick of an item of the ListView.
I'm currently using setEnabled(false) but sometimes the swipe gesture get recognized anyways.
What could i try to stop this behaviour?
I think doing this in onLongClick is too late to lock the SwipeRefreshLayout as the user-interaction is already happening.
You could e.g. look into somehow slightly increasing the touch slop value of your ListView to suppress accidental swipes.
Look over here for discussion: Android ACTION_MOVE Threshold
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Your condition.
return true;
}
});
Related
I have a parent, custom RelativeLayout which inflates two ImageViews: one content image, and a close button in the top right corner.
The issue: I cannot have both the gestures/scaling of the content image functioning AND the close button functioning.
The content image is a custom ImageView subclass (a modification of https://github.com/MikeOrtiz/TouchImageView), which has it's own OnTouchListener to allow for pinch-zooming, and responding to usual gestures.
This OnTouchListener contains the code:
private class PrivateOnTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
//....
return true;
Now, if that return is true, the scaling/gestures for the content image works, but the close button onClick is never called, whereas if it's false then the scaling/getures don't work and the onClick can be called.
I don't understand why a return function called AFTER the event is passed to the gesture detector affects (?consumes the event) whether or not the gesture detector works.
Is there a simple way of ensuring the functionality of both child ImageViews, where that return function is false but both detectors still work?
What I've tried:
Ensuring all methods in the gestureDetector return false, so that the event isn't consumed. (The scaleDetector isn't custom, so I haven't done the same there; if you think that's where the problem is let me know)
An onInterceptTouch method in the parent RelativeLayout, but I'm not sure if I implemented that correctly
(Reading around event handling to understand how it works)
I am overriding dispatchTouchEvent() method to detect each and every touch in an activity.
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
System.out.println("android test "+event.toString());
return super.dispatchTouchEvent(event);
}
But this method is catching only ACTION_UP,ACTION_DOWN and ACTION_MOVE events,not ACTION_CANCEL event. What may be the reason for this ?
#pskink's answer is correct. i.e. ACTION_CANCEL is a system event.
"ACTION_CANCEL occurs when the parent takes possession of the motion, for example when the user has dragged enough across a list view that it will start scrolling instead of letting you press the buttons inside of i.
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
I have OnTouchListener and OnItemClickListener attached to ListView and for some reason OnTouchListener is getting fired but it stops OnItemClickListener from firing ? Any suggesstion on how to go about this ?
Your OnTouchListener has consumed the touch event.
From here:
check on the return value ...
If you don't wanna consume the event on OnTouchListener , just return false on onTouch(View v, MotionEvent event) as suggested by the above link.
I have a customized GridView populated by a customized BaseAdapter. The selection mode of the GridView is MultiChoiceModal. I want to control which items can be activated when long clicked while still ensuring they respond to (short) click events. BaseAdapter has a method called isEnabled, the perfect solution would be if it had a method isActivatable that behaved analogously. The next best solution would be to intercept long clicks and pass them along to the default handler only when acceptable.
Here are some things that don't work:
Overriding isEnabled in the adapter. This is overkill. In that case, the items cease responding to click events.
Calling setLongClickable for the parent view of each item in the adapter's getView method. This is fraught with problems even if it worked. Needless to say it doesn't. Likely a byproduct of the selection mode.
Setting a custom onLongClickListener for the parent view of each item in the adapter's getView method. Android Studio suggests against this when using any AdapterView. It suggests overriding onItemLongClick instead.
Overriding onItemLongClick in the GridView. Evidently, that is also handled for you when in this selection mode.
Setting a custom onItemLongClickListener in the GridView.
While the hive works on this, I am going to try aborting the action mode's creation/blocking activation of prohibited items in the onItemCheckedStateChanged method of my AbsListView.MultiChoiceModeListener. Clearly I'm running low on ideas.
I have found a simple solution:
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
or
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
where view is the item where you want to prevent a choice mode activation after a long click.
I have a ViewPager that can contain two types of views. One of the view types has buttons, one of the view types does not. The buttonless view type does have to trap touches in onTouchEvent in order to pan and zoom an image. But I want to let clicks bubble to the ViewPager (that has a clickListener attached to it)
I have to return true in the view's onTouchEvent ACTION_DOWN or else it won't see future events.
I can't use onInterceptTouchEvent() in the ViewPager to capture clicks because one of the views does have buttons and those buttons need their clicks
So how can my view trap swipes, and let clicks bubble up?
proper way is probably to intercept only the events you want to intercept, by returning true in onInterceptTouchEvent only when needed.
but if you want to go with the dark side, there's a dirty alternative, that probably will lure you and you'll probably regret later: intercept all the events, then if needed pass them down.
e.g. have inside your views something like
public boolean canInterceptTouch(MotionEvent ev) {
// return true if you are interested in this touch event, e.g. it falls into
// a clickable area
}
and something that handle the touch event like
public void interceptTouch(MotionEvent ev) {
// here you react to the event
}
inside your viewpager you have
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
thien the view pager will handle the events in a dirty way like this:
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (yourView.canInterceptTouch(ev))) {
yourView.interceptTouch(ev);
return true;
} else {
return doSomethingElse();
}
}
note: although I've done something similar, with decent result, I do NOT reccomend it as a solution unless you've very simple logic in the handling of the motion events, otherwise it becomes a mess. Your future self will be happier if you spend some time now to do it properly.
EDIT: code has not been tested, just to give you an idea of what you need. Sorry for any typo.
Try using this ViewGroup.onInterceptTouchEvent(MotionEvent).This allows a ViewGroup to watch events as they are dispatched to child Views`