fragment預加載問題

我們在做應用開發的時候,一個Activity裏面可能會以viewpager(或其他容器)與多個Fragment來組合使用,而如果每個fragment都需要去加載數據,或從本地加載,或從網絡加載,那麼在這個activity剛創建的時候就變成需要初始化大量資源。這樣的結果,我們當然不會滿意。那麼,能不能做到當切換到這個fragment的時候,它纔去初始化呢?

答案就在Fragment裏的setUserVisibleHint這個方法裏。請看關於Fragment裏這個方法的API文檔(國內鏡像地址:http://zdz.la/YrpKlu):

[plain] view plain copy
  1. Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.  
  2.   
  3. An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.  
  4.   
  5. Parameters  
  6. isVisibleToUser true if this fragment's UI is currently visible to the user (default), false if it is not.  

該方法用於告訴系統,這個Fragment的UI是否是可見的。所以我們只需要繼承Fragment並重寫該方法,即可實現在fragment可見時才進行數據加載操作,即Fragment的懶加載。

代碼如下:

[java] view plain copy
  1. /* 
  2.  * Date: 14-7-17 
  3.  * Project: Access-Control-V2 
  4.  */  
  5. package cn.irains.access_control_v2.common;  
  6.   
  7. import android.support.v4.app.Fragment;  
  8.   
  9. /** 
  10.  * Author: msdx ([email protected]) 
  11.  * Time: 14-7-17 下午5:46 
  12.  */  
  13. public abstract class LazyFragment extends Fragment {  
  14.     protected boolean isVisible;  
  15.     /** 
  16.      * 在這裏實現Fragment數據的緩加載. 
  17.      * @param isVisibleToUser 
  18.      */  
  19.     @Override  
  20.     public void setUserVisibleHint(boolean isVisibleToUser) {  
  21.         super.setUserVisibleHint(isVisibleToUser);  
  22.         if(getUserVisibleHint()) {  
  23.             isVisible = true;  
  24.             onVisible();  
  25.         } else {  
  26.             isVisible = false;  
  27.             onInvisible();  
  28.         }  
  29.     }  
  30.   
  31.     protected void onVisible(){  
  32.         lazyLoad();  
  33.     }  
  34.   
  35.     protected abstract void lazyLoad();  
  36.   
  37.     protected void onInvisible(){}  
  38. }  

在LazyFragment,我增加了三個方法,一個是onVisiable,即fragment被設置爲可見時調用,一個是onInvisible,即fragment被設置爲不可見時調用。另外再寫了一個lazyLoad的抽象方法,該方法在onVisible裏面調用。你可能會想,爲什麼不在getUserVisibleHint裏面就直接調用呢?

我這麼寫是爲了代碼的複用。因爲在fragment中,我們還需要創建視圖(onCreateView()方法),可能還需要在它不可見時就進行其他小量的初始化操作(比如初始化需要通過AIDL調用的遠程服務)等。而setUserVisibleHint是在onCreateView之前調用的,那麼在視圖未初始化的時候,在lazyLoad當中就使用的話,就會有空指針的異常。而把lazyLoad抽離成一個方法,那麼它的子類就可以這樣做:

[java] view plain copy
  1. public class OpenResultFragment extends LazyFragment{  
  2.     // 標誌位,標誌已經初始化完成。  
  3.     private boolean isPrepared;  
  4.   
  5.     @Override  
  6.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  7.         Log.d(LOG_TAG, "onCreateView");  
  8.         View view = inflater.inflate(R.layout.fragment_open_result, container, false);  
  9.         //XXX初始化view的各控件  
  10.     isPrepared = true;  
  11.         lazyLoad();  
  12.         return view;  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void lazyLoad() {  
  17.         if(!isPrepared || !isVisible) {  
  18.             return;  
  19.         }  
  20.         //填充各控件的數據  
  21.     }  
  22.   
  23. }  

在上面的類當中,我們增加了一個標誌位isPrepared,用於標誌是否初始化完成。然後在我們所需要的初始化操作完成之後調用,如上面的例子當中,在初始化view之後,設置 isPrepared爲true,同時調用lazyLoad()方法。而在lazyLoad()當中,判斷isPrepared和isVisible只要有一個不爲true就不往下執行。也就是僅當初始化完成,並且可見的時候才繼續加載,這樣的避免了未初始化完成就使用而帶來的問題。

在這裏我對fragment的懶加載實現的介紹就到此爲止,如果你有興趣,可以基於此再深入探究,比如寫一個帶有緩初始化和可見時刷新的特性的Fragment。


轉載自:http://blog.csdn.NET/maosidiaoxian/article/details/38300627

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章