viewpager setonpagechangelistener已弃用

今天我试图设置ViewPager.setOnPageChangeListener ,我发现它已被弃用。

find自己的答案只是想分享它.ie,

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

你可以在Google开发者网站find这个。

使用ViewPager.addOnPageChangeListener而不是ViewPager.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) { } }); 

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