不推荐使用viewpager setonpagechangelistener

时间:2015-06-16 12:39:39

标签: android android-viewpager

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

6 个答案:

答案 0 :(得分:554)

我自己找到的答案就是想分享它.i.e,

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 Developer site.

找到

答案 1 :(得分:40)

这对我有用:

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

答案 2 :(得分:39)

使用 ViewPager.addOnPageChangeListener 代替 ViewPager.setOnPageChangeListener

答案 3 :(得分:7)

您可以通过@Override public void run() { try { wscript.setBid(0.30); wscript.setServiceMode(WebScript.ServiceMode.ON); for(;!ReadyToReturn || !Thread.currentThread().isInterrupted();) { Positioning(demandedPosition, maximumBid); if(ReadyToReturn || Thread.currentThread().isInterrupted()) break; Thread.sleep(10000); } wscript.setServiceMode(ServiceMode.OFF); Log("Algorithm has been canceled!"); } catch (InterruptedException e) { wscript.setServiceMode(ServiceMode.OFF); Log("Algorithm has been canceled!"); return; } } 来实现 以下是代码:

SimpleOnPageChangeListener

答案 4 :(得分:7)

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

    })

答案 5 :(得分: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()中实现您的通话