一。属性
1.setPageMargin
2.setOffscreenPageLimit
二。
1.什么时候调用PagerAdapter.instantiateItem
//在populate中,遍历mCurItem之前的, addNewItem满足这个条件的pos == ii.position && !ii.scrolling
遍历mCurItem之后的, addNewItem满足这个条件的pos == ii.position && !ii.scrolling
ItemInfo addNewItem(int position, int index) { ItemInfo ii = new ItemInfo(); ii.position = position; ii.object = mAdapter.instantiateItem(this, position); ii.widthFactor = mAdapter.getPageWidth(position); if (index < 0 || index >= mItems.size()) { mItems.add(ii); } else { mItems.add(index, ii); } return ii; }
void populate(int newCurrentItem) { ... ... // Locate the currently focused item or add it if needed. ItemInfo curItem = null; for (curIndex = 0; curIndex < mItems.size(); curIndex++) { final ItemInfo ii = mItems.get(curIndex); if (ii.position >= mCurItem) { if (ii.position == mCurItem) curItem = ii; break; } } if (curItem == null && N > 0) { curItem = addNewItem(mCurItem, curIndex); } ... ... // Fill 3x the available width or up to the number of offscreen // pages requested to either side, whichever is larger. // If we have no current item we have no work to do. if (curItem != null) { float extraWidthLeft = 0.f; int itemIndex = curIndex - 1; ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null; final int clientWidth = getClientWidth(); final float leftWidthNeeded = clientWidth <= 0 ? 0 : 2.f - curItem.widthFactor + (float) getPaddingLeft() / (float) clientWidth; for (int pos = mCurItem - 1; pos >= 0; pos--) { if (extraWidthLeft >= leftWidthNeeded && pos < startPos) { if (ii == null) { break; } if (pos == ii.position && !ii.scrolling) { mItems.remove(itemIndex); mAdapter.destroyItem(this, pos, ii.object); if (DEBUG) { Log.i(TAG, "populate() - destroyItem() with pos: " + pos + " view: " + ((View) ii.object)); } itemIndex--; curIndex--; ii = itemIndex >= 0 ? mItems.get(itemIndex) : null; } } else if (ii != null && pos == ii.position) { extraWidthLeft += ii.widthFactor; itemIndex--; ii = itemIndex >= 0 ? mItems.get(itemIndex) : null; } else { ii = addNewItem(pos, itemIndex + 1); extraWidthLeft += ii.widthFactor; curIndex++; ii = itemIndex >= 0 ? mItems.get(itemIndex) : null; } } ... ... if (extraWidthRight < 2.f) { ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null; final float rightWidthNeeded = clientWidth <= 0 ? 0 : (float) getPaddingRight() / (float) clientWidth + 2.f; for (int pos = mCurItem + 1; pos < N; pos++) { if (extraWidthRight >= rightWidthNeeded && pos > endPos) { if (ii == null) { break; } if (pos == ii.position && !ii.scrolling) { mItems.remove(itemIndex); mAdapter.destroyItem(this, pos, ii.object); if (DEBUG) { Log.i(TAG, "populate() - destroyItem() with pos: " + pos + " view: " + ((View) ii.object)); } ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null; } } else if (ii != null && pos == ii.position) { extraWidthRight += ii.widthFactor; itemIndex++; ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null; } else { ii = addNewItem(pos, itemIndex); itemIndex++; extraWidthRight += ii.widthFactor; ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null; } } } }
2.什么时候调用PagerAdapter.destroyItem
//在dataSetChanged中遍历mItems,利用PagerAdapter.getItemPosition() destroyItem pos
//在populate中,遍历mCurItem之前的, destroyItem满足这个条件的pos == ii.position && !ii.scrolling
遍历mCurItem之后的, destroyItem满足这个条件的pos == ii.position && !ii.scrolling
void dataSetChanged() { ... ... for (int i = 0; i < mItems.size(); i++) { final ItemInfo ii = mItems.get(i); final int newPos = mAdapter.getItemPosition(ii.object); if (newPos == PagerAdapter.POSITION_UNCHANGED) { continue; } if (newPos == PagerAdapter.POSITION_NONE) { mItems.remove(i); i--; if (!isUpdating) { mAdapter.startUpdate(this); isUpdating = true; } mAdapter.destroyItem(this, ii.position, ii.object); needPopulate = true; if (mCurItem == ii.position) { // Keep the current item in the valid range newCurrItem = Math.max(0, Math.min(mCurItem, adapterCount - 1)); needPopulate = true; } continue; } ... ... }
原理:
3.什么时候调用populate(item)
item是curItem
void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) { ... ... if (mFirstLayout) { // We don"t have any idea how big we are yet and shouldn"t have any pages either. // Just set things up and let the pending layout handle things. mCurItem = item; if (dispatchSelected) { dispatchOnPageSelected(item); } requestLayout(); } else { populate(item); scrollToItem(item, smoothScroll, velocity, dispatchSelected); } }
在onRestoreInstanceState,
onTouchEvent(MotionEvent ev) MotionEvent.ACTION_UP mIsBeingDragged==true时,
程序员调用setCurrentItem,setAdapter, endFakeDrag时
dataSetChanged,
private class PagerObserver extends DataSetObserver { PagerObserver() { } @Override public void onChanged() { dataSetChanged(); } @Override public void onInvalidated() { dataSetChanged(); } }
PagerAdapter.java:
public void notifyDataSetChanged() { synchronized (this) { if (mViewPagerObserver != null) { mViewPagerObserver.onChanged(); } } mObservable.notifyChanged(); }
也就说除非用户自己调用setCurrentItem,setAdapter, endFakeDrag,PagerAdapter.notifyDataSetChanged ,只有在ACTION_UP时,才会调用populate(item)
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/70766.html
摘要:最后,当一次更新添加和或移除完成之后将会调用来通知提交关联和或取消关联的操作。懒加载的实现弊端概念当需要时才加载,加载之后一直保持该对象。而且为了实现滑动效果,都是预加载左右两侧的页面。预加载的预加载机制。 目录介绍 01.ViewPager简单介绍 02.ViewPager弊端分析 03.ViewPager预加载 04.ViewPager部分源码 05.懒加载出现问题 06.如何实...
摘要:前言最近我在关注的使用,期间一直基于官方的调试,今天遇到一个奇葩的问题,捉摸了半天最终找到原因,原来是中布局的问题,事后感觉有必要分享一下这个过程,一来可以巩固测量的知识,二来希望大家能避开这个坑阅读指南代码基于,看官老爷最好能下载前言 最近我在关注ViewPager2的使用,期间一直基于官方的Demo调试android-viewpager2,今天遇到一个奇葩的问题,捉摸了半天最终找到原因,...
阅读 3464·2021-11-24 11:17
阅读 2224·2021-11-15 11:38
阅读 3353·2021-10-14 09:42
阅读 2921·2019-08-30 15:54
阅读 1891·2019-08-28 18:09
阅读 511·2019-08-26 11:48
阅读 1616·2019-08-26 10:48
阅读 2132·2019-08-26 10:45