viewpager setonpagechangelistener deprecated

后端 未结 6 1642
终归单人心
终归单人心 2020-12-02 07:13

Today I\'m trying to set ViewPager.setOnPageChangeListener and I\'ve found out that it\'s deprecated.

相关标签:
6条回答
  • 2020-12-02 07:45

    this worked for me:

    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) {
        }
    });
    
    0 讨论(0)
  • 2020-12-02 07:48

    Just in case someone needs the solution in 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.
            }
    
        })
    
    0 讨论(0)
  • 2020-12-02 07:50

    ViewPager.setOnPageChangeListener is deprecated now. Need to use ViewPager.addOnPageChangeListener instead.

    for example,

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

    You can find this Here.

    0 讨论(0)
  • 2020-12-02 07:51
    1. Create an interface if need be;

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

      }

    2. Implement Interface in every fragment in viewpager as shown

     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. List this is how u user the addOnPageListener

      enter 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. Implement your calls in the onPauseFragment() or onResumeFragment()

    0 讨论(0)
  • 2020-12-02 08:00

    You can achieve via SimpleOnPageChangeListener Below is the Code:

    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);
    
    0 讨论(0)
  • 2020-12-02 08:10

    Use ViewPager.addOnPageChangeListener instead of ViewPager.setOnPageChangeListener.

    0 讨论(0)
提交回复
热议问题