Android SimpleOnPageChangeListener - Determine swipe direction

可紊 提交于 2019-12-05 07:12:49

Use the ViewPager.SimpleOnPageChangeListener and keep a instance var with the current tab pos, that way you can work out which way it's been swiped.

private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(final int position) {
            onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
            mCurrentTabPosition = position;
        }
    };

 protected void onTabChanged(final PagerAdapter adapter, final int oldPosition, final int newPosition) {
        //Calc if swipe was left to right, or right to left
         if (oldPosition>newPosition){
           // left to right
         }
         else{
           //right to left 
         }

    }
        int prevPage;

        @Override
        public void onPageSelected(int position) {
            // went back a page
            if (position-1 == prevPage) {
                Log.i("Swipped", "Swipped Left");
            }
            // went up a page
            if (position+1 == prevPage) {
                Log.i("Swipped", "Swipped Right");
            }
            prevPage = position;


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