Android lock onClick onFling

I have an Activity that implements a gesture detector to catch a user input to go to other screens. This works fine, but - I recently updated a class that comes from BaseActivity to add an onClick function, and now that the click event seems to block onFling from getting hit. OnClick is tied to the TextView area (in LinearLayout) that I have on my screen. The resultsClick method is connected to the TextView using its onClick property in the XML layout.

I tried changing the return values ​​in onSingleTapUp and onDown with no luck. I also tried adding log entries to all the functions below. None of them work when I rush into the TextView area, but they work in other areas of the screen.

Perhaps I am using the wrong search terms, but I could not find an example that appealed to this, but I am sure that this problem was solved earlier.

public class DerivedActivity extends BaseActivity { ... /** * resultsClick - The user clicked on the Results area * @param v */ public void resultsClick(View v) { try { Log.i(this.toString(), "resultsClick"); startActivity(new Intent(this, Results_TabHost.class )); } catch (Exception e) { Log.e(this.toString(), "Exception" + e.toString()); } }// end resultsClick ... } 

Here is a base class implementing GestureListener code

 public class BaseActivity extends ActivityGroup implements OnGestureListener { ... private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { Log.i(this.toString(), "onFling"); // jump right out if not a swipe/fling if (Math.abs( e1.getY() - e2.getY() ) > SWIPE_MAX_OFF_PATH) { return false; } // right to left swipe if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY ) { Log.i(this.toString(), "fling left"); rightArrowClick(null); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY ) { Log.i(this.toString(), "fling right"); leftArrowClick(null); } } catch (Exception e) { Log.e(this.toString(), "Exception" + e.toString()); } return true; }// end onFling // These next methods we are required to have - even if unused - // in order for the Gesture Handling to work @Override public boolean onTouchEvent(MotionEvent motionEvent) { return this.gestureDetector.onTouchEvent(motionEvent); } @Override public void onLongPress(MotionEvent e) { // Intentionally not handling - must be overridden by listener class } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // Intentionally not handling - must be overridden by listener class // Intentionally returning true - per code examples return true; } @Override public void onShowPress(MotionEvent e) { // Intentionally not handling - must be overridden by listener class } @Override public boolean onSingleTapUp(MotionEvent e) { // Intentionally not handling - must be overridden by listener class // Intentionally returning true - per code examples return true; } @Override public boolean onDown(MotionEvent e) { // Intentionally not handling - must be overridden by listener class // Intentionally returning true - per code examples return true; } ... } 
+6
android android-activity onclick gesture-recognition
source share
3 answers

Your onTouchEvent implementation is incorrect. You simply return the value of the result of gestureDector.

If your gesture detector does not detect any gestures, you tell the subscriber: β€œI had nothing to do here,” and the touch event will never be sent to the Activity children.

You need to call super.onTouchEvent() if your gesture detector has not processed the event.

 @Override public boolean onTouchEvent(MotionEvent motionEvent) { if(this.gestureDetector.onTouchEvent(motionEvent)) { return true; } //no gesture detected, let Activity handle touch event return super.onTouchEvent(motionEvent); } 
+3
source share

Pay attention to this function:

 @Override public boolean onDown(MotionEvent e) { // Intentionally not handling - must be overridden by listener class // Intentionally returning true - per code examples return true; } 

Change the return value to false.

+3
source share

You can simply return false when your code does nothing ... This will allow the motion event system to control everything on its own. True when you want to stop an event that will be sent to a different kind of children.

+1
source share

All Articles