今天我要设置ViewPager。setOnPageChangeListener,我发现它已弃用。


当前回答

: viewpage。setOnPageChangeListener现在已弃用。需要使用ViewPager。addOnPageChangeListener代替。

例如,

ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

你可以在这里找到这个。

其他回答

: viewpage。setOnPageChangeListener现在已弃用。需要使用ViewPager。addOnPageChangeListener代替。

例如,

ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

你可以在这里找到这个。

你可以通过SimpleOnPageChangeListener来实现 守则如下:

private Fragment page = null;
/** Defining a listener for pageChange */
        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager_selectPhotos + ":" + position);

                System.out.println(page);
                switch (position) {
                case 0:

                    break;
                case 1:

                    break;

                default:
                    break;
                }
            }
        };
        /** Setting the pageChange listener to the viewPager */
        _pager.setOnPageChangeListener(pageChangeListener);

使用:viewpage。addOnPageChangeListener而不是viewpage . setonpagechangelistener。

这招对我很管用:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {       
        conterchange.setText(""+(1+position));
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

以防有人需要kotlin中的解决方案:

viewPager.addOnPageChangeListener (object: ViewPager.OnPageChangeListener {
        override fun onPageScrollStateChanged(state: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onPageSelected(position: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    })