title: android:How can I call setAdapter for ViewPager inside the method onPageSelected?
body: <p>My adapter for the ViewPager needs to be reset according to the current active page.I want to do it using OnPageChangeListener.
  A part of the code of my adapter is like:</p>
  
  <pre><code>    class CustomPagerAdapter extends PagerAdapter {
              static int currpage = 0; // current selected page
              ......
              @Override
              public Object instantiateItem(ViewGroup collection, int position) {     
                  LayoutInflater inflater = LayoutInflater.from(mContext);
                  View layout = inflater.inflate(R.layout.layout_home_viewpager, collection, false);
  
                 // some code that changes layout dynamically    
                 // according to variable position and currpage
                 ......
  
                 collection.addView(layout);
                 return layout;
              }
      }
  </code></pre>
  
  <p>And here is the code for my viewpager(inside OnCreateView of a fragment):</p>
  
  <pre><code>    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
          }
  
          @Override
          public void onPageSelected(int position) {
              CustomPagerAdapter.currpage = position;
          }
  
          @Override
          public void onPageScrollStateChanged(int state) {
  
          }
      };
      viewPager.addOnPageChangeListener(onPageChangeListener);
      viewPager.setAdapter(customPagerAdapter);
  </code></pre>
  
  <p>The following code will only be called once(When creating view of the fragment):</p>
  
  <pre><code>     viewPager.setAdapter(customPagerAdapter);
  </code></pre>
  
  <p>I intend to call setadapter whenever the active is changed.But if I change the code to this :</p>
  
  <pre><code>    @Override
      public void onPageSelected(int position) {
          CustomPagerAdapter.currpage = position; // flag for setAdapter
          viewPager.setAdapter(customPagerAdapter);
      }
  </code></pre>
  
  <p>the viewpager will be unable to slide.</p>
  
  <p>Since the only way to detect page changing is register a listener to viewPager and we can't call setAdapter inside the method of our listener, What will be the best way to reset adapter for viewpager when the selected page is changed ?</p>