Android的 - 麻烦与轻扫手势 [英] Android - Trouble with swipe gesture

查看:185
本文介绍了Android的 - 麻烦与轻扫手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的应用程序轻扫手势。我做了几乎所有code,但它不能正常工作。

下面是code我有我的活动:

  //刷卡器
gestureDetector =新GestureDetector(新SwipeGesture(本));
gestureListener =新OnTouchListener(){
    公共布尔onTouch(视图V,MotionEvent事件)
    {
        Log.e(,它的工作原理);
        返回gestureDetector.onTouchEvent(事件);
    }
};

的LinearLayout根=(的LinearLayout)findViewById(R.id.rules_root);
root.setOnTouchListener(gestureListener);
 

当我触摸屏,显示的logcat 它的工作原理

在这里,他的类 SwipeGesture 我的code:

 公共类SwipeGesture扩展SimpleOnGestureListener
{
    私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
    私有静态最终诠释SWIPE_MAX_OFF_PATH = 250;
    私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;

    私人活动的活动;

    公共SwipeGesture(活动活动)
    {
        超();
        this.activity =活动;
    }

    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮动velocityY)
    {
        Log.e(,我在这里);
        尝试
        {
            如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)返回false;
            如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY)
            {
                如果(((TabActivity)activity.getParent())。getTabHost()!= NULL)
                {
                    TabHost第=((TabActivity)activity.getParent())getTabHost()。
                    th.setCurrentTab(th.getCurrentTab() -  1);
                }
                其他
                {
                    activity.finish();
                }
                Log.e(,向左滑动);
            }
            否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY)
            {
                如果(((TabActivity)activity.getParent())。getTabHost()!= NULL)
                {
                    TabHost第=((TabActivity)activity.getParent())getTabHost()。
                    th.setCurrentTab(th.getCurrentTab()+ 1);
                }
                Log.e(,向右滑动);
            }
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
        返回false;
    }
}
 

Log.e(,我在这里); 永远不会显示。所以我presume的onFling方法不会被调用。

为什么这不起作用任何想法?

感谢。

问候。

解决方案

在你SimpleOnGestureListener,覆盖onDown为你的手势进行注册。它可以只返回true,但它必须是这样定义的。

  @覆盖
    公共布尔onDown(MotionEvent E){
            返回true;
    }
 

.... 请参阅此链接..和下面的答案评论..

I'm trying to implement a swipe gesture in my app. I've made almost all code but it doesn't work.

Here is the code I have in my Activity:

// Swipe detector
gestureDetector = new GestureDetector(new SwipeGesture(this));
gestureListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event)
    {
        Log.e("", "It works");
        return gestureDetector.onTouchEvent(event);
    }
};

LinearLayout root = (LinearLayout) findViewById(R.id.rules_root);
root.setOnTouchListener(gestureListener);

When I touch the screen, the logcat displays it works.

Here he my the code of the class SwipeGesture:

public class SwipeGesture extends SimpleOnGestureListener
{
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private Activity activity;

    public SwipeGesture(Activity activity)
    {
        super();
        this.activity = activity;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        Log.e("", "Here I am");
        try
        {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false;
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
            {
                if ( ((TabActivity) activity.getParent()).getTabHost() != null )
                {
                    TabHost th = ((TabActivity) activity.getParent()).getTabHost();
                    th.setCurrentTab(th.getCurrentTab() - 1);
                }
                else
                {
                    activity.finish();
                }
                Log.e("", "Swipe left");
            }
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
            {
                if ( ((TabActivity) activity.getParent()).getTabHost() != null )
                {
                    TabHost th = ((TabActivity) activity.getParent()).getTabHost();
                    th.setCurrentTab(th.getCurrentTab() + 1);
                }
                Log.e("", "Swipe right");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
}

The line Log.e("", "Here I am"); is never displayed. So I presume the onFling method is never called.

Any ideas of why this doesn't work?

Thanks.

Regards.

V.

解决方案

In your SimpleOnGestureListener, override onDown for your gestures to register. It can just return true but it has to be defined like this..

@Override
    public boolean onDown(MotionEvent e) {
            return true;
    }

....See this link.. and the comment below the answer..

这篇关于Android的 - 麻烦与轻扫手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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