Android - 拖屏导航

发布于 2024-10-06 03:27:57 字数 1343 浏览 0 评论 0原文

我必须在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;
        }
    }
}

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

提前致谢

I have to do the drag navigation in Android. I have added a code for the drag screen navigation. Which is not as smooth as Android default. The following code gives the drag navigation from left to right and right to left but the problem is when you tap at right and then to left then too the screen navigates. What is the right way to achieve it. Do I need to work with same code with calculating the X values?
Below the code.

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

Please specify the right way of achieving it.

Thanks in advance

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

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

发布评论

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

评论(2

熟人话多 2024-10-13 03:27:57

您应该使用 GestureListener

You should be using a GestureListener

π浅易 2024-10-13 03:27:57

您可能不想为此使用单独的活动。此模式用于在同一活动中进行导航。

根据您选择的思考方式,可以考虑两种或三种状态。第一种是当用户将手指放在屏幕上并来回平移时的“拖动”状态。第二个和第三个是用户放手时发生的情况。如果放手时的速度低于某个阈值,(系统使用 ViewConfiguration 中的最小滑动速度)动画到最近的屏幕。如果速度超过该阈值,则朝该方向跳至下一个屏幕。

看一下 VelocityTracker、Scroller 和 Interpolator 类。他们所有人都可以在这里为您提供帮助。

You probably don't want to use separate activities for this. This pattern is used to navigate within the same activity.

There are either two or three states to consider depending on how you choose to think about it. The first is the 'dragging' state when the user has a finger in the screen and is panning back and forth. The second and third are what happens when the user lets go. If the velocity when letting go is under a certain threshold, (the system uses the minimum fling velocity from ViewConfiguration) animate to the closest screen. If the velocity is over that threshold, fling to the next screen in that direction.

Take a look at the VelocityTracker, Scroller, and Interpolator classes. All of them can help you here.

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