背景 在多个Fragment需要加载的时候,启动速度往往会变慢,影响用户体验。分析发现并非所有的Fragment都需要第一时间将数据填充完毕,因为它们都还没有被用户所”看见”.所以我们要达到的效果是Fragment被显示后才加载数据(lazy load)。由于这种需求具有共性,即每个Fragment都可能有懒加载的需要,故封装成基类BaseFragment.
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 public abstract class BaseFragment extends Fragment { public String fragmentTitle; private boolean isFragmentVisible; private boolean isPrepared; private boolean isFirstLoad = true ; private boolean forceLoad = false ; @Override public void onCreate (@Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); Bundle bundle = getArguments(); if (bundle != null && bundle.size() > 0 ) { initVariables(bundle); } } @Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { isFirstLoad = true ; View view = initViews(inflater, container, savedInstanceState); isPrepared = true ; lazyLoad(); return view; } @Override public void setUserVisibleHint (boolean isVisibleToUser) { super .setUserVisibleHint(isVisibleToUser); if (getUserVisibleHint()) { onVisible(); } else { onInvisible(); } } @Override public void onHiddenChanged (boolean hidden) { super .onHiddenChanged(hidden); if (!hidden) { onVisible(); } else { onInvisible(); } } protected void onVisible () { isFragmentVisible = true ; lazyLoad(); } protected void onInvisible () { isFragmentVisible = false ; } protected void lazyLoad () { if (isPrepared() && isFragmentVisible()) { if (forceLoad || isFirstLoad()) { forceLoad = false ; isFirstLoad = false ; initData(); } } } @Override public void onDestroyView () { super .onDestroyView(); isPrepared = false ; } public void initVariables (Bundle bundle) {} protected abstract View initViews (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) ; protected abstract void initData () ; public boolean isPrepared () { return isPrepared; } public void setForceLoad (boolean forceLoad) { this .forceLoad = forceLoad; } public boolean isFirstLoad () { return isFirstLoad; } public boolean isFragmentVisible () { return isFragmentVisible; } public String getTitle () { if (null == fragmentTitle) { setDefaultFragmentTitle(null ); } return TextUtils.isEmpty(fragmentTitle) ? "" : fragmentTitle; } public void setTitle (String title) { fragmentTitle = title; } protected abstract void setDefaultFragmentTitle (String title) ; }
参考:LazyFragment
Prev: 【Android TimeCat】 Android Studio拾色器
Next: 【Android TimeCat】 Android中使用矢量图(SVG, VectorDrawable)