Fragment雖然有onResume和onPause的,可是這兩個方法是Activity的方法,調用時機也是與Activity相同,和ViewPager搭配使用這個方法就很雞肋了,根本不是你想要的效果,這裏介紹一種方法。html
覆寫Fragment 的setUserVisibleHint方法便可:android
@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { //至關於Fragment的onResume } else { //至關於Fragment的onPause } }
代碼說明:api
經過閱讀ViewPager和PageAdapter相關的代碼,切換Fragment實際上就是經過設置setUserVisibleHint和setMenuVisibility來實現的,調用這個方法時並不會釋放掉Fragment(即不會執行onDestoryView)app
=========================更完美的封裝應該是以下的==============================================ide
在Fragment裏的setUserVisibleHint這個方法裏。請看關於Fragment裏這個方法的API文檔(國內鏡像地址:Fragment api):this
1
2
3
4
|
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.
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.
Parameters
isVisibleToUser
true
if
this
fragment's UI is currently visible to the user (
default
),
false
if
it is not.
|
1
2
|
該方法用於告訴系統,這個Fragment的UI是不是可見的。因此咱們只須要繼承Fragment並重寫該方法,便可實如今fragment可見時才進行數據加載操做,即Fragment的懶加載。
代碼以下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/*
* Date: 14-7-17
* Project: Access-Control-V2
*/
package cn.irains.access_control_v2.common;
import android.support.v4.app.Fragment;
/**
* Author: msdx (645079761@qq.com)
* Time: 14-7-17 下午5:46
*/
public abstract class LazyFragment extends Fragment {
protected boolean isVisible;
/**
* 在這裏實現Fragment數據的緩加載.
* @param isVisibleToUser
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super
.setUserVisibleHint(isVisibleToUser);
if
(getUserVisibleHint()) {
isVisible =
true
;
onVisible();
}
else
{
isVisible =
false
;
onInvisible();
}
}
protected void onVisible(){
lazyLoad();
}
protected abstract void lazyLoad();
protected void onInvisible(){}
}
|
在LazyFragment,我增長了三個方法,一個是onVisiable,即fragment被設置爲可見時調用,一個是onInvisible,即fragment被設置爲不可見時調用。另外再寫了一個lazyLoad的抽象方法,該方法在onVisible裏面調用。你可能會想,爲何不在getUserVisibleHint裏面就直接調用呢?spa
我這麼寫是爲了代碼的複用。由於在fragment中,咱們還須要建立視圖(onCreateView()方法),可能還須要在它不可見時就進行其餘小量的初始化操做(好比初始化須要經過AIDL調用的遠程服務)等。而setUserVisibleHint是在onCreateView以前調用的,那麼在視圖未初始化的時候,在lazyLoad當中就使用的話,就會有空指針的異常。而把lazyLoad抽離成一個方法,那麼它的子類就能夠這樣作:指針
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class OpenResultFragment extends LazyFragment{
// 標誌位,標誌已經初始化完成。
private boolean isPrepared;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LOG_TAG,
"onCreateView"
);
View view = inflater.inflate(R.layout.fragment_open_result, container,
false
);
//XXX初始化view的各控件
isPrepared =
true
;
lazyLoad();
return
view;
}
@Override
protected void lazyLoad() {
if
(!isPrepared || !isVisible) {
return
;
}
//填充各控件的數據
}
}
|
在上面的類當中,咱們增長了一個標誌位isPrepared,用於標誌是否初始化完成。而後在咱們所須要的初始化操做完成以後調用,如上面的例子當中,在初始化view以後,設置 isPrepared爲true,同時調用lazyLoad()方法。而在lazyLoad()當中,判斷isPrepared和isVisible只要有一個不爲true就不往下執行。也就是僅當初始化完成,而且可見的時候才繼續加載,這樣的避免了未初始化完成就使用而帶來的問題。rest
在這裏我對fragment的懶加載實現的介紹就到此爲止,若是你有興趣,能夠基於此再深刻探究,好比寫一個帶有緩初始化和可見時刷新的特性的Fragment。code
參考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1021/1813.html
http://www.cnblogs.com/over140/p/3392164.html