Details ViewPager's use of PagerAdapter in Android App

  • 2021-06-28 13:55:47
  • OfStack

PageAdapter is an abstract class that inherits directly from Object and is available by importing the package android.support.v4.view.PagerAdapter.

To use PagerAdapter,

First, inherit the PagerAdapter class, overriding at least the following methods:

Each time you create an ViewPager or slide, the following four methods are called, and the methods in instantiateItem and destroyItem are implemented by themselves.


public abstract int getCount();

This method is to get the current number of form interfaces


public abstract boolean isViewFromObject(android.view.View arg0, java.lang.Object arg1);

This method is used to determine whether an interface is generated from an object


public java.lang.Object instantiateItem(android.view.View container, int position);

This method, return1 object, indicates which object the PagerAdapter adapter chose to place in the current ViewPager


public void destroyItem(android.view.ViewGroup container, int position, java.lang.Object object);

This method removes the current View from ViewGroup


public class ViewPagerAdapter extends PagerAdapter{

    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      return 0;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
      // TODO Auto-generated method stub
      return false;
    }


    @Override
    public void destroyItem(View container, int position, Object object) {
      // TODO Auto-generated method stub
      super.destroyItem(container, position, object);
    }
    
    @Override
    public Object instantiateItem(View container, int position) {
      // TODO Auto-generated method stub
      return super.instantiateItem(container, position);
    }
    
  }


ViewPager's PagerAdapter cannot solve the problem of updating data
While working on the project, I found that even if I called


galleryAdapter.notifyDataSetChanged();

However, ViewPager will not update the original data.

Later, we found a method on stackoverflow, the original link:
http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view
It took a little time to modify the code:


protected PagerAdapter galleryAdapter = new PagerAdapter() { 
   
  @Override 
  public boolean isViewFromObject(View arg0, Object arg1) { 
     return arg0 == ((View)arg1);  
  } 
   
  @Override 
  public int getCount() { 
    return size; 
  } 
   
  @Override 
  public Object instantiateItem(View container, int position) { 
    return bindGalleryAdapterItemView(container, position); 
  } 
   
  @Override 
  public void destroyItem(View container, int position, Object object) { 
     ((ViewPager) container).removeView((View) object);  
  }; 
   
    @Override  
    public void finishUpdate(View arg0) {}  
      
    @Override 
    public void restoreState(android.os.Parcelable state, ClassLoader loader) { 
     
    }; 
  
    @Override  
    public Parcelable saveState() {  
      return null;  
    }  
  
    @Override  
    public void startUpdate(View arg0) {}  
     
    @Override 
    public int getItemPosition(Object object) { 
    return POSITION_NONE; 
    } 
 
}; 

Note: POSITION_NONE is an internal constant of PagerAdapter with a value of -2.
API contains instructions:


int android.support.v4.view.PagerAdapter.POSITION_NONE = -2 [0xfffffffe]

The data is ready to be updated.Hey.


Related articles: