viewpager 左右滑动监听,左右滑动判断

 viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            private int position;
            private int oldPositon;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                this.position = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == 1) {
                    oldPositon = position;
                }
                if (state == 0) {
                   
                    if (position == oldPositon) {
                        if (position == 0) {
                            //滑动到第一页,继续向右滑
                        } else if (position == viewPager.getAdapter().getCount() - 1) {
                            //滑动到最后一页,继续向左滑
                        } else {
                            //滑动到一半时停止滑动,当前停留在第position页
                        }
                    } else {
                        if (position < oldPositon) {
                            //从左向右
                        } else {
                            //从右向左
                        }
                    }
                }
            }

        });

 

你可能感兴趣的:(viewpager 左右滑动监听,左右滑动判断)