前言
ViewPager默认情况的加载,会默认预加载一个的布局到ViewPager中,这就时ViewPager的预加载。setOffscreenPageLimit可以通过这个方法去设置预加载布局的个数。
public void setOffscreenPageLimit(int limit) {
if (limit < 1) {
Log.w("ViewPager", "Requested offscreen page limit " + limit + " too small; defaulting to " + 1);
limit = 1;
}
if (limit != this.mOffscreenPageLimit) {
this.mOffscreenPageLimit = limit;
this.populate();
}
}
从上面可以看出,预加载的页面个数大于等于1。
缓存的是什么?
在缓存数量更新的时候,要执行缓存方法polulate()。去看看里面做了啥事
void populate(int newCurrentItem) {
ViewPager.ItemInfo oldCurInfo = null;
if (this.mCurItem != newCurrentItem) {
oldCurInfo = this.infoForPosition(this.mCurItem);
this.mCurItem = newCurrentItem;
}
if (this.mAdapter == null) {
this.sortChildDrawingOrder();
} else if (this.mPopulatePending) {
this.sortChildDrawingOrder();
} else if (this.getWindowToken() != null) {
this.mAdapter.startUpdate(this);//被弃用了
int pageLimit = this.mOffscreenPageLimit;//当前的缓存页面个数
int startPos = Math.max(0, this.mCurItem - pageLimit);//计算缓存的开始位置
int N = this.mAdapter.getCount();//adapter的子内容的数量
int endPos = Math.min(N - 1, this.mCurItem + pageLimit);//计算缓存的结束位置
if (N != this.mExpectedAdapterCount) {
String resName;
try {
resName = this.getResources().getResourceName(this.getId());
} catch (NotFoundException var17) {
resName = Integer.toHexString(this.getId());
}
throw new IllegalStateException("The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: " + this.mExpectedAdapterCount + ", found: " + N + " Pager id: " + resName + " Pager class: " + this.getClass() + " Problematic adapter: " + this.mAdapter.getClass());
} else {
int curIndex = true;
ViewPager.ItemInfo curItem = null;
//开始去找 ViewPager.ItemInfo
int curIndex;
for(curIndex = 0; curIndex < this.mItems.size(); ++curIndex) {
ViewPager.ItemInfo ii = (ViewPager.ItemInfo)this.mItems.get(curIndex);
if (ii.position >= this.mCurItem) {
if (ii.position == this.mCurItem) {
curItem = ii;
}
break;
}
}
//没找到就去创建 ViewPager.ItemInfo,并放入ArrayList<ViewPager.ItemInfo> mItems中
if (curItem == null && N > 0) {
curItem = this.addNewItem(this.mCurItem, curIndex);
}
int itemIndex;
ViewPager.ItemInfo ii;
int i;
·····一些计算操作,省略
}
this.mAdapter.finishUpdate(this);//完成条目的更新
int childCount = this.getChildCount();
for(itemIndex = 0; itemIndex < childCount; ++itemIndex) {
View child = this.getChildAt(itemIndex);
ViewPager.LayoutParams lp = (ViewPager.LayoutParams)child.getLayoutParams();
lp.childIndex = itemIndex;
if (!lp.isDecor && lp.widthFactor == 0.0F) {
ViewPager.ItemInfo ii = this.infoForChild(child);
if (ii != null) {
lp.widthFactor = ii.widthFactor;
lp.position = ii.position;
}
}
}
this.sortChildDrawingOrder();
}
}
}
前面提到了创建ViewPager.ItemInfo,这个是什么
ViewPager.ItemInfo addNewItem(int position, int index) {
ViewPager.ItemInfo ii = new ViewPager.ItemInfo();
ii.position = position;
注释①
ii.object = this.mAdapter.instantiateItem(this, position);
ii.widthFactor = this.mAdapter.getPageWidth(position);
if (index >= 0 && index < this.mItems.size()) {
this.mItems.add(index, ii);
} else {
this.mItems.add(ii);
}
return ii;
}
注释①:调用instantiateItem来创建object,在FragmentPagerAdapter的instantiateItem这个方法中,创建的就是fragment。所以缓存的就是fragment,fragment在创建时,会有ui操作,网络操作,在还未可见的时候,就去初始化fragment非常消耗性能,所以懒加载的方式来加载,不去缓存fragment。