Android: GestureDetector not working (gestureDetector.onTouchEvent(event) always false) with Tabs (TabActivity, Tabwidget)
Asked Answered
H

2

11

I have implemented my TabActivity with different child activities:

intent = new Intent().setClass(this, MyChildTabActiviy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = getTabHost.newTabSpec("tag").setIndicator("indicator", getResources().getDrawable(R.drawable.icon)).setContent(intent);
getTabHost.addTab(spec);
...

So far no problems, everything works perfectly fine. I'm switching programmatically between tabs, replacing activities within tabs with ActivityGroups, etc. just as it's shown in many tutorials.

But my problem is, that when I want to check for a fling gesture my gestureDetector.onTouchEvent(event) is always returning false, thus no gesture is registrated.

This is my implementation of gestureDetector:

public class MyChildTabActiviy extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ... building views, controls, etc.
        GestureDetector gestureDetector = new GestureDetector(this, new MyGestureDetector());
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
 class MyGestureDetector extends SimpleOnGestureListener {
  @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;
   // left to right swipe and right to left swipe
   if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    //... fling logic ...
    return true;
   } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    //... fling logic ...
    return true;
   }
   return false;
  }
 }

The thing is, that this code (and also the fling detection) works perfectly fine, when I'm starting these activities (there are four basic activities, that I sometimes switch to other activities) outside of a TabActivity, e.g. as a Launcher Activity. But I can't get it to work within a TabActivity. I already tried to append the GestureDetector to the TabActivity, but it doesn't work. I tried to append the GestureDetector to specific views like some layout views or buttons, ViewFlippers, etc. but it just doesn't work. When I'm debugging, I can see that the touch event is triggered and a motion is registered, but it just isn't evaluated as a fling or any other gesture.

So my question is, are there are any limitations regarding the usage of GestureDetectors with Tabs in Android? As I said, the gestures are registrated perfectly outside a TabActivity.

I would greatly appreciate the help of someone who knows the answer. If there is a limitation, how could someone get a workaround for that problem?

Thanks in advance for answers.

Harry answered 9/12, 2010 at 1:23 Comment(1)
Possible duplicate of Fling gesture detection on grid layoutRetha
A
23

Have a look at the answer mentioned here. He's pretty much done the same thing as you, but if you look at the first comment on the highest rated answer, Cdsboy got it working by implementing OnDown and returning true. I'm not sure why that is needed, but it worked for me.

Aiguille answered 11/12, 2010 at 13:3 Comment(2)
This helped me too! I had some onFling code that was working fine in 1.5 and 1.6 but starting with 2.1 it stopped working... returning true in onDown() fixed the problem.Assist
@Object When i override the onDown() my onFling() has not been called.Effulgent
T
7

As a complement to @Abhinav 's answer (that btw helped me too), I'd like to say that I think overriding onDown() is needed because its default implementation in SimpleOnGestureListener is to return false. Being ACTION_DOWN the first one to reach the listener, it would make it discard the event, whatever it is.

Thursby answered 11/12, 2010 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.