安卓:GestureDetector不工作(gestureDetector.onTouchEvent(事件)始终为false)与标签(TabActivity,Tabwidget) [英] Android: GestureDetector not working (gestureDetector.onTouchEvent(event) always false) with Tabs (TabActivity, Tabwidget)

查看:185
本文介绍了安卓:GestureDetector不工作(gestureDetector.onTouchEvent(事件)始终为false)与标签(TabActivity,Tabwidget)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了我的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);
...

到目前为止没有任何问题,一切工作完全正常。我的选项卡之间进行切换编程,卡口与ActivityGroups等,只是因为它显示在许多教程中更换活动。

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.

但我的问题是,当我要检查一扔手势我gestureDetector.onTouchEvent(事件)总是返回false,因此没有手势注册资格。

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.

这是我实现gestureDetector的:

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;
  }
 }

的事情是,这个code(也是一扔检测)工作完全正常,当我开始这些活动(有四种基本的活动,我有时切换到其他活动)一TabActivity外, 例如作为一个启动活动。但我无法得到它的一个TabActivity内工作。我已经尝试过追加GestureDetector到TabActivity,但它不工作。我试图追加GestureDetector像一些布局视图或按钮,ViewFlippers等具体意见,但它只是不工作。 当我调试,我可以看到,触摸事件被触发和运动被注册,但它只是不评价为一扔,或任何其他的手势。

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.

所以我的问题是,有关于GestureDetectors的使用与标签Android中的任何限制?正如我所说的,举手投足间都是一个TabActivity外界完全配准。

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.

我将不胜AP preciate人谁知道答案的帮助。 如果有一个限制,怎么会有人得到一个解决方法的问题?

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?

在此先感谢答案。

推荐答案

有一个在该答复中提到这里。他是pretty的多少做同样的事情,你,但如果你在收视率最高的答案第一个注释,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.

这篇关于安卓:GestureDetector不工作(gestureDetector.onTouchEvent(事件)始终为false)与标签(TabActivity,Tabwidget)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆