Custom view with buttons implements OnGestureListener

I’ve been racking my brains googled from google for the last couple of hours, and not one of the examples in StackOverflow or other places worked for me, so here it is ...

I have a custom view that extends LinearLayout and implements GestureDetector.OnGestureListener .

In my custom layout, I have 3 buttons, each of which has a click listener. I want to be able to Fling throughout the view in order to accomplish something, but also to be able to click on buttons.

My onFling function works fine if I go inside, but not on one of the buttons. If I go through one of the buttons, in most cases it clicks or does nothing.

Here is the relevant part of my code:

 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 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) { viewFlipper.setInAnimation(slideLeftIn); viewFlipper.setOutAnimation(slideLeftOut); viewFlipper.showNext(); return true; } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { viewFlipper.setInAnimation(slideRightIn); viewFlipper.setOutAnimation(slideRightOut); viewFlipper.showPrevious(); return true; } return false; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return true; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return gestureDetector.onTouchEvent(event); } return false; } 

tried any combination return true; return false; I would have thought ... I would have thanked for any help! :)

Thanks!

+7
source share
3 answers

Here is what finally solved it for me ... (only modified code from my question). Not sure if this is the best solution, but it works:

 @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onTouchEvent(MotionEvent e) { return gestureDetector.onTouchEvent(e); } @Override public boolean onInterceptTouchEvent(MotionEvent e) { return onTouchEvent(e); } 

This will allow the fling gesture even above the buttons, and also allows you to simply click on the BUT buttons, if you have a different view with the buttons behind this view, sometimes clicks will go back and perform onClick on the buttons behind the view. The solution for this is to add "android:clickable="true" to the layout of the root of the foreground view (LinearLayout, FrameLayout or something else).

+3
source

Here is an answer that may be relevant to the situation that you are describing https://stackoverflow.com/a/318618/

+1
source

Perhaps you can set the overlay using SurfaceView , in which you will handle all the pop-ups: http://developer.android.com/reference/android/view/SurfaceView.html

Using surfaceView on Top, your buttons will still work, and your hatches will be able to navigate the buttons without any problems.

0
source

All Articles