Android - 拖屏导航

标签 android android-widget android-layout drag

我必须在 Android 中进行拖动导航。我为拖屏导航添加了代码。这不像 Android 默认那样流畅。以下代码提供了从左到右和从右到左的拖动导航,但问题是当您点击右侧然后点击左侧然后屏幕导航时。实现它的正确方法是什么。我是否需要使用相同的代码来计算 X 值?
代码下方。

// On Drag Navigation
public boolean onTouch(View arg0, MotionEvent arg1) {

    // Get the action that was done on this touch event
    switch (arg1.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        {
            // store the X value when the user's finger was pressed down
            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP:
        {
            // Get the X value when the user released his/her finger
            float currentX = arg1.getX();            

            // Going Backward: pushing stuff to the right
            if (downXValue < currentX)
            {
                Intent lIntent = new Intent(getApplicationContext(), previousScreen.class);
                startActivity(lIntent);
                finish();
            }

            // Going Forward: pushing stuff to the left
            if (downXValue > currentX)
            {
                Intent lIntent = new Intent(getApplicationContext(), nextScreen.class);
                startActivity(lIntent);
                finish();
            }
            break;
        }
    }
}

请指定实现它的正确方法。

提前致谢

最佳答案

你应该使用 GestureListener

关于Android - 拖屏导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4367934/

相关文章:

java - Android 上下文问题 - 广播接收器(警报管理器)中应使用什么上下文?

java - 为什么 setOnChangeListener 不能正常工作?

android - 没有字典提示的 AutoCompleteTextView

android - 测试主屏幕小部件的最佳方法

java - 在启动Intent之前更改布局内容,例如文本和图像

android - 如何在嵌套 View 中水平划分 View

带有后退按钮的 Android Webview,否则

android - 如何设置适合任何屏幕高度的布局?

android - 如何在 Android 中拖放 EditText 的内容?

android - 数字键盘输入连接