Android:GestureDetector 无法与选项卡(TabActivity、Tabwidget)一起工作(gestureDetector.onTouchEvent(event) 始终为 false)

发布于 2024-10-06 17:35:29 字数 2059 浏览 0 评论 0原文

我已经用不同的子活动实现了我的 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);
...

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

但我的问题是,当我想检查 fling 手势时,我的gestureDetector.onTouchEvent(event) 总是返回 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;
  }
 }

问题是,当我在a之外开始这些活动(有四个基本活动,我有时会切换到其他活动)时,这段代码(以及fling检测)工作得非常好。 TabActivity,例如作为启动器活动。但我无法让它在 TabActivity 中工作。我已经尝试将 GestureDetector 附加到 TabActivity,但它不起作用。我尝试将 GestureDetector 附加到特定视图,例如某些布局视图或按钮、ViewFlippers 等,但它不起作用。 当我调试时,我可以看到触摸事件被触发并且运动被注册,但它只是没有被评估为猛击或任何其他手势。

所以我的问题是,在 Android 中使用带有选项卡的 GestureDetectors 是否有任何限制?正如我所说,手势是在 TabActivity 之外完美注册的。

我将非常感谢知道答案的人的帮助。 如果存在限制,人们如何才能找到解决该问题的方法?

预先感谢您的答复。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-10-13 17:35:29

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

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.

韬韬不绝 2024-10-13 17:35:29

作为对 @Abhinav 答案的补充(顺便说一句,这也对我有帮助),我想说我认为需要覆盖 onDown() ,因为它在 SimpleOnGestureListener 中的默认实现code> 是返回 false。作为 ACTION_DOWN 第一个到达侦听器的事件,它会使其丢弃该事件,无论该事件是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文