开发者

Android - Drag screen navigation

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(getA开发者_StackOverflow社区pplicationContext(), nextScreen.class);
                startActivity(lIntent);
                finish();
            }
            break;
        }
    }
}

Please specify the right way of achieving it.

Thanks in advance


You should be using a GestureListener


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜