How to get current swipe direction in action in ViewPager

纵饮孤独 提交于 2019-12-24 12:16:07

问题


I am trying to use ViewPager for showing different views.

Below is code I am using to check whether swiping is left or right

    myPager.setOnTouchListener(new ViewPager.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX();

            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mStartDragX = x;
                break;
            case MotionEvent.ACTION_MOVE:
                if (event.getX() > mStartDragX ) {
                    Log.i(TAG,"SWIPING RIGHT");
                } else if (event.getX() < mStartDragX) {
                    Log.i(TAG,"SWIPING LEFT");
                }
                break;
            }
            return false;
        }
    });

So when I start to swipe from right to left or right to left its showing me correct log message.

But if I started to swipe from right to left and at the same time again if start to swipe from left to right, how can I identify whether currently swiping in which direction?

Can anyone please help ?

Thanks


回答1:


Then do somethink like this..

case MotionEvent.ACTION_MOVE:
    if (event.getX() > mStartDragX) {
            Log.i(TAG, "SWIPING RIGHT");
            mStartDragX = event.getX();
        } else if (event.getX() < mStartDragX) {
            Log.i(TAG, "SWIPING LEFT");
            mStartDragX = event.getX();
        }

Here i am saving the current position again to old position so whenever the swiped back it will give the correct result..



来源:https://stackoverflow.com/questions/22680978/how-to-get-current-swipe-direction-in-action-in-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!