實現ViewPager懶加載的三種方法

在項目中ViewPager和Fragment接口框架已是到處可見,可是在使用中,咱們確定不但願用戶在當前頁面時就在先後頁面的數據,加入數據量很大,而用戶又不肯意左右滑動瀏覽,那麼這時候ViewPager中原本充滿善意的預加載就有點使人不爽了。咱們能作的就是屏蔽掉ViewPager的預加載機制。雖然ViewPager中提供的有java

setOffscreenPageLimit()來控制其預加載的數目,可是當設置爲0後咱們發現其根本沒效果,這個的最小值就是1,也就是你只能最少先後各預加載一頁。那麼,這時候就得另覓方法了。android


如下三種方法是我在學習和項目中嘗試過的,需求實現了,但各有千秋,可結合不一樣場景使用。由於打算慢慢養成寫博客的習慣,就總結在此,也但願對他人有所借鑑。git


方法一 

在Fragment可見時請求數據。此方案仍預加載了先後的頁面,可是沒有請求數據,只有進入到當前Framgent時才請求數據。github

優勢:實現了數據的懶加載框架

缺點:一次還是三個Framgment對象,不是徹底意義的懶加載ide

 
 
 
 
  1. 學習

  2. this

  3. spa

  4. code

public class FragmentSample extends Fragment{    ...      @Override    public void setUserVisibleHint(boolean isVisibleToUser) {        super.setUserVisibleHint(isVisibleToUser);        if (isVisibleToUser) {            requestData(); // 在此請求數據        }    }    ...}


方法二

直接修改ViewPager源碼。經過查看ViewPager源碼可知,控制其預加載的是一個常量

DEFAULT_OFFSCREEN_PAGES,其默認值爲1,表示當前頁面先後各預加載一個頁面,在這裏咱們直接將其設置爲0便可,即去掉預加載。可是,這樣有一個問題,那就是在使用其餘控件時須要傳入ViewPager時,這個就不能用了。

優勢:徹底屏蔽掉了預加載

缺點:應用太受限制,好比使用ViewPagerIndicator時須要傳入ViewPager對象,這時傻眼了。

  
  
  
  
// 注意,這是直接拷貝的ViewPager的源碼,只修改了註釋處的代碼public class LazyViewPager extends ViewGroup { private static final String TAG = "LazyViewPager"; private static final boolean DEBUG = false; private static final boolean USE_CACHE = false; // 默認爲1,即先後各預加載一個頁面,設置爲0去掉預加載 private static final int DEFAULT_OFFSCREEN_PAGES = 0; private static final int MAX_SETTLE_DURATION = 600; // ms static class ItemInfo { Object object; int position; boolean scrolling; } private static final Comparator<ItemInfo> COMPARATOR = new Comparator<ItemInfo>() { @Override public int compare(ItemInfo lhs, ItemInfo rhs) { return lhs.position - rhs.position; } }; ............}


方法三

直接繼承ViewPager,結合PagerAdapter實現懶加載。該方案是我用到的最完善的方法,徹底的懶加載,每次只會創建一個Fragment對象。

優勢:徹底屏蔽預加載

缺點:稍微複雜,可是人家已經造好的輪子,直接用吧,很簡潔

開源庫:https://github.com/lianghanzhen/LazyViewPager

這個庫就4個類,做者經過繼承ViewPager(保證其普適性)、自定義ViewPagerAdapter和 LazyFragmentPagerAdapter以及設置懶加載的標記接口,很好的實現了懶加載。感謝做者。

在此貼出關鍵代碼,有興趣的同窗能夠學習下。

LazyViewPager:

  
  
  
  
public class LazyViewPager extends ViewPager { private static final float DEFAULT_OFFSET = 0.5f; private LazyPagerAdapter mLazyPagerAdapter; private float mInitLazyItemOffset = DEFAULT_OFFSET; public LazyViewPager(Context context) { super(context); } public LazyViewPager(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LazyViewPager); setInitLazyItemOffset(a.getFloat(R.styleable.LazyViewPager_init_lazy_item_offset, DEFAULT_OFFSET)); a.recycle(); } /** * change the initLazyItemOffset * @param initLazyItemOffset set mInitLazyItemOffset if {@code 0 < initLazyItemOffset <= 1} */ public void setInitLazyItemOffset(float initLazyItemOffset) { if (initLazyItemOffset > 0 && initLazyItemOffset <= 1) { mInitLazyItemOffset = initLazyItemOffset; } } @Override public void setAdapter(PagerAdapter adapter) { super.setAdapter(adapter); mLazyPagerAdapter = adapter != null && adapter instanceof LazyPagerAdapter ? (LazyPagerAdapter) adapter : null; } @Override protected void onPageScrolled(int position, float offset, int offsetPixels) { if (mLazyPagerAdapter != null) { if (getCurrentItem() == position) { int lazyPosition = position + 1; if (offset >= mInitLazyItemOffset && mLazyPagerAdapter.isLazyItem(lazyPosition)) { mLazyPagerAdapter.startUpdate(this); mLazyPagerAdapter.addLazyItem(this, lazyPosition); mLazyPagerAdapter.finishUpdate(this); } } else if (getCurrentItem() > position) { int lazyPosition = position; if (1 - offset >= mInitLazyItemOffset && mLazyPagerAdapter.isLazyItem(lazyPosition)) { mLazyPagerAdapter.startUpdate(this); mLazyPagerAdapter.addLazyItem(this, lazyPosition); mLazyPagerAdapter.finishUpdate(this); } } } super.onPageScrolled(position, offset, offsetPixels); }}

  
  
  
  
public abstract class LazyFragmentPagerAdapter extends LazyPagerAdapter<Fragment> { private static final String TAG = "LazyFragmentPagerAdapter"; private static final boolean DEBUG = false; private final FragmentManager mFragmentManager; private FragmentTransaction mCurTransaction = null; public LazyFragmentPagerAdapter(FragmentManager fm) { mFragmentManager = fm; } @Override public void startUpdate(ViewGroup container) { } @Override public Object instantiateItem(ViewGroup container, int position) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } final long itemId = getItemId(position); // Do we already have this fragment? String name = makeFragmentName(container.getId(), itemId); Fragment fragment = mFragmentManager.findFragmentByTag(name); if (fragment != null) { if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment); mCurTransaction.attach(fragment); } else { fragment = getItem(container, position); if (fragment instanceof Laziable) { mLazyItems.put(position, fragment); } else { mCurTransaction.add(container.getId(), fragment, name); } } if (fragment != getCurrentItem()) { fragment.setMenuVisibility(false); fragment.setUserVisibleHint(false); } return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object + " v=" + ((Fragment) object).getView()); final long itemId = getItemId(position); String name = makeFragmentName(container.getId(), itemId); if (mFragmentManager.findFragmentByTag(name) == null) { mCurTransaction.detach((Fragment) object); } else { mLazyItems.remove(position); } } @Override public Fragment addLazyItem(ViewGroup container, int position) { Fragment fragment = mLazyItems.get(position); if (fragment == null) return null; final long itemId = getItemId(position); String name = makeFragmentName(container.getId(), itemId); if (mFragmentManager.findFragmentByTag(name) == null) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } mCurTransaction.add(container.getId(), fragment, name); mLazyItems.remove(position); } return fragment; } @Override public void finishUpdate(ViewGroup container) { if (mCurTransaction != null) { mCurTransaction.commitAllowingStateLoss(); mCurTransaction = null; mFragmentManager.executePendingTransactions(); } } @Override public boolean isViewFromObject(View view, Object object) { return ((Fragment) object).getView() == view; } public long getItemId(int position) { return position; } private static String makeFragmentName(int viewId, long id) { return "android:switcher:" + viewId + ":" + id; } /** * mark the fragment can be added lazily */ public interface Laziable { }}
最後提醒一下:填充LazyViewPager的Fragment必定要實現接口LazyFragmentPagerAdapter.Laziable。


以上。

相關文章
相關標籤/搜索