91亚洲精华国内精华精华液_国产高清在线精品一区不卡_精品特级一级毛片免费观看_欧美日韩中文制服有码_亚洲精品无码你懂的网站369

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

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

 

[plain] view plaincopy
 
  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.  


該方法用于告訴系統(tǒng),這個Fragment的UI是否是可見的。所以我們只需要繼承Fragment并重寫該方法,即可實現(xiàn)在fragment可見時才進行數(shù)據(jù)加載操作,即Fragment的懶加載。

 

代碼如下:

 

[java] view plaincopy
 
  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 (645079761@qq.com) 
  11.  * Time: 14-7-17 下午5:46 
  12.  */  
  13. public abstract class LazyFragment extends Fragment {  
  14.     protected boolean isVisible;  
  15.     /** 
  16.      * 在這里實現(xiàn)Fragment數(shù)據(jù)的緩加載. 
  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中,我們還需要創(chuàng)建視圖(onCreateView()方法),可能還需要在它不可見時就進行其他小量的初始化操作(比如初始化需要通過AIDL調用的遠程服務)等。而setUserVisibleHint是在onCreateView之前調用的,那么在視圖未初始化的時候,在lazyLoad當中就使用的話,就會有空指針的異常。而把lazyLoad抽離成一個方法,那么它的子類就可以這樣做:

 

[java] view plaincopy
 
  1. public class OpenResultFragment extends LazyFragment{  
  2.     // 標志位,標志已經(jīng)初始化完成。  
  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.         //填充各控件的數(shù)據(jù)  
  21.     }  
  22.   
  23. }  


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

 

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

穩(wěn)定

產(chǎn)品高可用性高并發(fā)

貼心

項目群及時溝通

專業(yè)

產(chǎn)品經(jīng)理1v1支持

快速

MVP模式小步快跑

承諾

我們選擇聲譽

堅持

10年專注高端品質開發(fā)
  • 返回頂部