Android:GestureDetector不工作(gestureDetector.onTouchEvent(event)总是false)带Tabs(TabActivity,Tabwidget)

ein*_*eee 10 tabs android gestures swipe

我已经使用不同的子活动实现了我的TabActivity:

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);
...
Run Code Online (Sandbox Code Playgroud)

到目前为止没有问题,一切都很好.我在选项卡之间以编程方式切换,用ActivityGroups替换选项卡中的活动等,正如许多教程中所示.

但我的问题是,当我想检查一个投掷手势时,我的gestureDetector.onTouchEvent(事件)总是返回false,因此没有注册任何手势.

这是我对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;
  }
 }
Run Code Online (Sandbox Code Playgroud)

问题是,当我在TabActivity之外开始这些活动(有四个基本活动,我有时会切换到其他活动)时,这个代码(以及fling检测)工作得很好,例如作为启动器活动.但我不能让它在TabActivity中工作.我已经尝试将GestureDetector附加到TabActivity,但它不起作用.我试图将GestureDetector附加到特定的视图,如一些布局视图或按钮,ViewFlippers等,但它只是不起作用.当我正在调试时,我可以看到触发事件被触发并且注册了一个动作,但它不会被评估为投掷或任何其他手势.

所以我的问题是,在Android中使用带标签的GestureDetectors有任何限制吗?正如我所说,手势完全在TabActivity之外注册.

我非常感谢知道答案的人的帮助.如果存在限制,有人可以为该问题找到解决方法吗?

提前感谢您的回答.

Abh*_*nda 22

看看这里提到的答案.他几乎和你做了同样的事情,但如果你看看评分最高的答案的第一个评论,Cdsboy通过实现OnDown并返回true来实现它.我不确定为什么需要它,但它对我有用.


big*_*nes 7

作为@Abhinav答案的补充(对btw也有帮助),我想说我认为onDown()需要覆盖是因为它的默认实现SimpleOnGestureListener是返回false.作为ACTION_DOWN第一个接触听众的人,无论是什么,都会让它丢弃.