ViewPager+Fragment取消预加载(延迟加载)

在项目中,都或多或少地使用的Tab布局,所以大都会用到ViewPager+Fragment,但是Fragment有个不好或者太好的地方。例如你在ViewPager中添加了三个Fragment,当加载ViewPager中第一个Fragment时,它会默认帮你预先加载了第二个Fragment,当你加载第二个Fragment时,它会帮你加载第三个Fragment。这样虽然有时很好,但是用户只需看一个Fragment时,我们就做了一些多余工作加载了第二个Fragment。在这只需要取消Fragment的预加载即可,只有当用户切换到某个Fragment才加载..

ViewPager+Fragment取消预加载(延迟加载)_第1张图片

首先,介绍两个方法void setUserVisibleHint(boolean isVisibleToUser)、boolean getUserVisibleHint(),它们分别用作设置/获得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

34

35

36

37

38

39

40

41

42

43

44

45importandroid.support.v4.app.Fragment;

publicabstractclassBaseFragmentextendsFragment {

/** Fragment当前状态是否可见 */

protectedbooleanisVisible;

@Override

publicvoidsetUserVisibleHint(booleanisVisibleToUser) {

super.setUserVisibleHint(isVisibleToUser);

if(getUserVisibleHint()) {

isVisible =true;

onVisible();

}else{

isVisible =false;

onInvisible();

}

}

/**

* 可见

*/

protectedvoidonVisible() {

lazyLoad();

}

/**

* 不可见

*/

protectedvoidonInvisible() {

}

/**

* 延迟加载

* 子类必须重写此方法

*/

protectedabstractvoidlazyLoad();

}

在我们的Fragment中,只需要继承这个类,然后重写其中的lazyLoad()方法,当Fragment对用户可见(即用户切换到此Fragment时)我们在lazyLoad()中加载所需数据,详细代码看下面,我写了个假的获取数据线程:

?

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116importandroid.os.AsyncTask;

importandroid.os.Bundle;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.TextView;

publicclassCustomListFragmentextendsBaseFragment {

privatestaticfinalString FRAGMENT_INDEX = fragment_index;

privatefinalintFIRST_FRAGMENT =0;

privatefinalintSECOND_FRAGMENT =1;

privatefinalintTHIRD_FRAGMENT =2;

privateTextView mFragmentView;

privateintmCurIndex = -1;

/** 标志位,标志已经初始化完成 */

privatebooleanisPrepared;

/** 是否已被加载过一次,第二次就不再去请求数据了 */

privatebooleanmHasLoadedOnce;

/**

* 创建新实例

*

* @param index

* @return

*/

publicstaticCustomListFragment newInstance(intindex) {

Bundle bundle =newBundle();

bundle.putInt(FRAGMENT_INDEX, index);

CustomListFragment fragment =newCustomListFragment();

fragment.setArguments(bundle);

returnfragment;

}

@Override

publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if(mFragmentView ==null) {

mFragmentView = (TextView) inflater.inflate(R.layout.fragment, container,false);

//获得索引值

Bundle bundle = getArguments();

if(bundle !=null) {

mCurIndex = bundle.getInt(FRAGMENT_INDEX);

}

isPrepared =true;

lazyLoad();

}

//因为共用一个Fragment视图,所以当前这个视图已被加载到Activity中,必须先清除后再加入Activity

ViewGroup parent = (ViewGroup)mFragmentView.getParent();

if(parent !=null) {

parent.removeView(mFragmentView);

}

returnmFragmentView;

}

@Override

protectedvoidlazyLoad() {

if(!isPrepared || !isVisible || mHasLoadedOnce) {

return;

}

newAsyncTask() {

@Override

protectedvoidonPreExecute() {

super.onPreExecute();

//显示加载进度对话框

UIHelper.showDialogForLoading(getActivity(), 正在加载...,true);

}

@Override

protectedBoolean doInBackground(Void... params) {

try{

Thread.sleep(2000);

//在这里添加调用接口获取数据的代码

//doSomething()

}catch(Exception e) {

e.printStackTrace();

}

returntrue;

}

@Override

protectedvoidonPostExecute(Boolean isSuccess) {

if(isSuccess) {

// 加载成功

setView();

mHasLoadedOnce =true;

}else{

// 加载失败

}

//关闭对话框

UIHelper.hideDialogForLoading();

}

}.execute();

}

privatevoidsetView() {

// 根据索引加载不同视图

switch(mCurIndex) {

caseFIRST_FRAGMENT:

mFragmentView.setText(第一个);

break;

caseSECOND_FRAGMENT:

mFragmentView.setText(第二个);

break;

caseTHIRD_FRAGMENT:

mFragmentView.setText(第三个);

break;

}

}

}

到这里我们只是写好了Fragment,在FragmentActivity中还需要对ViewPager设置一下,让它每次只加载一个Fragment,ViewPager.setOffscreenPageLimit(int limit),其中参数可以设为0或者1,参数小于1时,会默认用1来作为参数,未设置之前,ViewPager会默认加载两个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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118importjava.util.ArrayList;

importjava.util.List;

importandroid.support.v4.app.Fragment;

importandroid.support.v4.app.FragmentActivity;

importandroid.support.v4.view.ViewPager;

importandroid.support.v4.view.ViewPager.OnPageChangeListener;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.RadioButton;

importandroid.os.Bundle;

publicclassMainActivityextendsFragmentActivityimplementsOnClickListener{

privateRadioButton mFstBtn;

privateRadioButton mSndBtn;

privateRadioButton mThdBtn;

privateViewPager mViewPager;

privateListFragmentPagerAdapter mPagerAdapter;

privateList mFragments =newArrayList();

privatefinalintFIRST_FRAGMENT =0;

privatefinalintSECOND_FRAGMENT =1;

privatefinalintTHIRD_FRAGMENT =2;

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initButton();

initViewPager();

}

/**

* 初始化按钮

*/

privatevoidinitButton() {

mFstBtn = (RadioButton)findViewById(R.id.id_rb_fst);

mFstBtn.setOnClickListener(this);

mSndBtn = (RadioButton)findViewById(R.id.id_rb_snd);

mSndBtn.setOnClickListener(this);

mThdBtn = (RadioButton)findViewById(R.id.id_rb_thd);

mThdBtn.setOnClickListener(this);

}

/**

* 初始化ViewPager控件

*/

privatevoidinitViewPager() {

mViewPager = (ViewPager)findViewById(R.id.id_vp_viewpager);

//关闭预加载,默认一次只加载一个Fragment

mViewPager.setOffscreenPageLimit(1);

//添加Fragment

mFragments.add(CustomListFragment.newInstance(FIRST_FRAGMENT));

mFragments.add(CustomListFragment.newInstance(SECOND_FRAGMENT));

mFragments.add(CustomListFragment.newInstance(THIRD_FRAGMENT));

//适配器

mPagerAdapter =newListFragmentPagerAdapter(getSupportFragmentManager(), mFragments);

mViewPager.setAdapter(mPagerAdapter);

mViewPager.setOnPageChangeListener(onPageChangeListener);

}

privateOnPageChangeListener onPageChangeListener =newOnPageChangeListener() {

@Override

publicvoidonPageSelected(intposition) {

//根据用户选中的按钮修改按钮样式

switch(position) {

caseFIRST_FRAGMENT:

mFstBtn.setChecked(true);

mSndBtn.setChecked(false);

mThdBtn.setChecked(false);

break;

caseSECOND_FRAGMENT:

mFstBtn.setChecked(false);

mSndBtn.setChecked(true);

mThdBtn.setChecked(false);

break;

caseTHIRD_FRAGMENT:

mFstBtn.setChecked(false);

mSndBtn.setChecked(false);

mThdBtn.setChecked(true);

break;

}

}

@Override

publicvoidonPageScrolled(intarg0,floatarg1,intarg2) {}

@Override

publicvoidonPageScrollStateChanged(intarg0) {}

};

@Override

publicvoidonClick(View v) {

switch(v.getId()) {

caseR.id.id_rb_fst:

mViewPager.setCurrentItem(FIRST_FRAGMENT);

break;

caseR.id.id_rb_snd:

mViewPager.setCurrentItem(SECOND_FRAGMENT);

break;

caseR.id.id_rb_thd:

mViewPager.setCurrentItem(THIRD_FRAGMENT);

break;

}

}

}

你可能感兴趣的:(ViewPager+Fragment取消预加载(延迟加载))