viewpager setonpagechangelistener已弃用


263

今天,我正在尝试设置ViewPager.setOnPageChangeListener,但发现它已过时。


15
顺便说一句,不建议使用此方法。改用addOnPageChangeListener(OnPageChangeListener)和removeOnPageChangeListener(OnPageChangeListener)。,来自文档
Blackbelt 2015年

Answers:


586

ViewPager.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) {

        }
    });

您可以在这里找到


2
看来Eclipse甚至找不到方法addOnPageChangeListener,请问这是从哪个支持库中获得的?
霓虹灯Warge 2015年

7
我认为在这里值得一提的是clearOnPageChangeListeners(),因此在使用addOnPageChangeListener()时不会以多个侦听器结尾。
Clocksmith

2
令人困惑。如果他们保持相同的语法,并简单地用更新的版本替换此调用或其他内容,那将是很好的。
杰伊·斯奈德

41

这为我工作:

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) {
    }
});

40

使用ViewPager.addOnPageChangeListener而不是ViewPager.setOnPageChangeListener


10

万一有人需要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.
        }

    })

8

您可以通过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);

问题是不推荐使用的 setOnPageChangeListener函数的替代方法。
小学

0
  1. 根据需要创建一个接口;

    public interface FragmentLifecycle {
    public void onPauseFragment();
    public void onResumeFragment();

    }

  2. 如图所示,在viewpager的每个片段中实现接口

 public class FragmentCast extends Fragment implements FragmentLifecycle {
  @Override
    public void onPauseFragment() {
        Log.i(TAG, "onPauseFragment()");
        Toast.makeText(getActivity(), "onPauseFragment():" + TAG, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResumeFragment() {
        Log.i(TAG, "onResumeFragment()");
        Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show();
    }
  1. 列出这是您如何使用addOnPageListener

    输入viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){int currentPosition = 0;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
        }
    
        @Override
        public void onPageSelected(int position) {
    
    
            FragmentLifecycle   fragmentLifecycleToShow =(FragmentLifecycle) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem());
            fragmentLifecycleToShow.onResumeFragment();
    
            FragmentLifecycle   fragmentLifecycleToHide =(FragmentLifecycle) viewPager.getAdapter().instantiateItem(viewPager, currentPosition);
            fragmentLifecycleToHide.onPauseFragment();
    
            currentPosition = position;
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
    
        }
    });
  2. 在onPauseFragment()或onResumeFragment()中实现您的调用

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.