@Override public Fragment getItem(int position){ return ArrayListFragment.newInstance(position); } }
/* *ListFragment is the same as a ListActivity - it's just a Fragment, *that extends List methods. You can just add Fragment, that contains ListView and implement *all required for list methods, like in Activity */ publicstaticclassArrayListFragmentextendsListFragment{ int mNum;
/** * Create a new instance of CountingFragment, providing "num" * as an argument. */ static ArrayListFragment newInstance(int num){ ArrayListFragment f = new ArrayListFragment();
// Supply num input as an argument. /* 为什么不通过构造函数直接传参数?详见 https://blog.csdn.net/tu_bingbing/article/details/24143249 */ Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args);
return f; }
/** * When creating, retrieve this instance's number from its arguments. */ @Override publicvoidonCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; }
/** * The Fragment's UI is just a simple text view showing its * instance number. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View v = inflater.inflate(R.layout.fragment_pager_list, container, false); View tv = v.findViewById(R.id.text); ((TextView) tv).setText("Fragment #" + mNum); return v; }
// Dispatch the change to any listeners if (mAdapterChangeListeners != null && !mAdapterChangeListeners.isEmpty()) { for (int i = 0, count = mAdapterChangeListeners.size(); i < count; i++) { mAdapterChangeListeners.get(i).onAdapterChanged(this, oldAdapter, adapter); } } }
voidpopulate(int newCurrentItem){ //adapter页面将更改时调用 mAdapter.startUpdate(this); finalint N = mAdapter.getCount(); // Locate the currently focused item or add it if needed. int curIndex = -1; 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; } } //找不到当前需要的item,则add if (curItem == null && N > 0) { curItem = addNewItem(mCurItem, curIndex); } //...两个循环对左右两边的Fragment进行缓存及清除(代码略) mAdapter.setPrimaryItem(this, mCurItem, curItem.object); }
staticclassItemInfo{ Object object; int position; boolean scrolling; float widthFactor; float offset; } //存放缓存的Fragment privatefinal ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>(); int mCurItem; // Index of currently displayed page.
查看addNewItem对应的方法
1 2 3 4 5 6 7 8 9 10 11 12
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; }
注释1处,上述例子如果设置了mBehavior为BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,则该预加载的Fragment生命周期只会执行到onStart(),原因是调用了setMaxLifecycle()。该方法可以设置活跃状态下 Fragment 最大的状态,如果该 Fragment 超过了设置的最大状态,那么会强制将 Fragment 降级到相应的状态。详细可以看这篇博客
public Object instantiateItem(@NonNull ViewGroup container, int position){ Fragment fragment; //先判断是否缓存了该Fragment,若有则直接复用 if (this.mFragments.size() > position) { fragment = (Fragment)this.mFragments.get(position); if (fragment != null) { return fragment; } }
if (this.mCurTransaction == null) { this.mCurTransaction = this.mFragmentManager.beginTransaction(); } //getItem是自行重写的方法 fragment = this.getItem(position); //查询是否保存有对应的Bundle信息 if (this.mSavedState.size() > position) { SavedState fss = (SavedState)this.mSavedState.get(position); if (fss != null) { fragment.setInitialSavedState(fss); } }
publicvoidsetOffscreenPageLimit(@OffscreenPageLimitint limit){ if (limit < 1 && limit != OFFSCREEN_PAGE_LIMIT_DEFAULT) { thrownew IllegalArgumentException( "Offscreen page limit must be OFFSCREEN_PAGE_LIMIT_DEFAULT or a number > 0"); } mOffscreenPageLimit = limit; // Trigger layout so prefetch happens through getExtraLayoutSize() mRecyclerView.requestLayout(); }