Android的 - 麻烦与轻扫手势(Android - Trouble with swipe ge

2019-06-23 10:21发布

我想实现我的应用程序滑动手势。 我做了几乎所有的代码,但它不工作。

下面是我在我的活动代码:

// 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);

当我触摸屏幕时,logcat的显示it works

在这里,他对我的类的代码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;
    }
}

该生产线Log.e("", "Here I am"); 永远不会显示。 所以,我推测onFling方法不会被调用。

为什么这是行不通的任何想法?

谢谢。

问候。

V.

Answer 1:

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

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

.... 请参阅此链接..并回答下面的评论..



Answer 2:

你需要改变一些事情,这里有一个例子。

设置你的OnTouchListener:

root.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return false;
                }
                return false;
            }
        });

SwipeGesture类

class SwipeGesture extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        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) {
//Do something

                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Do something
                return true;

            }

        } catch (Exception e) {
            Log.e("Fling", "There was an error processing the Fling event:"
                    + e.getMessage());
        }
        return true;
    }

    // Necessary for the onFling event to register
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
}

它看起来像你刷卡之间Tabs 。 利用FragmentsViewPager是多为你的用户更容易和顺畅。



文章来源: Android - Trouble with swipe gesture