b43865531fdfc616199293f165191bd6d4566cc0.svn-base 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package com.hjq.base;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.annotation.IdRes;
  7. import android.support.annotation.NonNull;
  8. import android.support.annotation.Nullable;
  9. import android.support.v4.app.Fragment;
  10. import android.view.KeyEvent;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import java.lang.reflect.Field;
  15. import java.util.Random;
  16. /**
  17. * desc : Fragment 懒加载基类
  18. */
  19. public abstract class BaseLazyFragment<A extends BaseActivity> extends Fragment {
  20. // Activity对象
  21. public A mActivity;
  22. // 根布局
  23. private View mRootView;
  24. // 是否进行过懒加载
  25. private boolean isLazyLoad;
  26. // Fragment 是否可见
  27. private boolean isFragmentVisible;
  28. // 是否是 replace Fragment 的形式
  29. private boolean isReplaceFragment;
  30. /**
  31. * 获得全局的,防止使用getActivity()为空
  32. */
  33. @SuppressWarnings("unchecked")
  34. @Override
  35. public void onAttach(Context context) {
  36. super.onAttach(context);
  37. mActivity = (A) requireActivity();
  38. }
  39. /**
  40. * 获取绑定的Activity,防止出现 getActivity() 为空
  41. */
  42. public A getBindingActivity() {
  43. return mActivity;
  44. }
  45. @Override
  46. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  47. if (mRootView == null && getLayoutId() > 0) {
  48. mRootView = inflater.inflate(getLayoutId(), null);
  49. }
  50. ViewGroup parent = (ViewGroup) mRootView.getParent();
  51. if (parent != null) {
  52. parent.removeView(mRootView);
  53. }
  54. return mRootView;
  55. }
  56. @Override
  57. public View getView() {
  58. return mRootView;
  59. }
  60. /**
  61. * 是否进行了懒加载
  62. */
  63. protected boolean isLazyLoad() {
  64. return isLazyLoad;
  65. }
  66. /**
  67. * 当前 Fragment 是否可见
  68. */
  69. public boolean isFragmentVisible() {
  70. return isFragmentVisible;
  71. }
  72. @Override
  73. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  74. super.onActivityCreated(savedInstanceState);
  75. if (isReplaceFragment) {
  76. if (isFragmentVisible) {
  77. initLazyLoad();
  78. }
  79. }else {
  80. initLazyLoad();
  81. }
  82. }
  83. // replace Fragment时使用,ViewPager 切换时会回调此方法
  84. @Override
  85. public void setUserVisibleHint(boolean isVisibleToUser) {
  86. super.setUserVisibleHint(isVisibleToUser);
  87. this.isReplaceFragment = true;
  88. this.isFragmentVisible = isVisibleToUser;
  89. if (isVisibleToUser && mRootView != null) {
  90. if (!isLazyLoad) {
  91. initLazyLoad();
  92. }else {
  93. // 从不可见到可见
  94. onRestart();
  95. }
  96. }
  97. }
  98. /**
  99. * 初始化懒加载
  100. */
  101. protected void initLazyLoad() {
  102. if (!isLazyLoad) {
  103. isLazyLoad = true;
  104. initFragment();
  105. }
  106. }
  107. /**
  108. * 从可见的状态变成不可见状态,再从不可见状态变成可见状态时回调
  109. */
  110. protected void onRestart() {}
  111. @Override
  112. public void onDetach() {
  113. super.onDetach();
  114. // 解决java.lang.IllegalStateException: Activity has been destroyed 的错误
  115. try {
  116. Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
  117. childFragmentManager.setAccessible(true);
  118. childFragmentManager.set(this, null);
  119. } catch (NoSuchFieldException e) {
  120. throw new RuntimeException(e);
  121. } catch (IllegalAccessException e) {
  122. throw new RuntimeException(e);
  123. }
  124. }
  125. protected void initFragment() {
  126. initView();
  127. initData();
  128. }
  129. //引入布局
  130. protected abstract int getLayoutId();
  131. //标题栏id
  132. protected abstract int getTitleId();
  133. //初始化控件
  134. protected abstract void initView();
  135. //初始化数据
  136. protected abstract void initData();
  137. /**
  138. * 根据资源 id 获取一个 View 对象
  139. */
  140. protected <V extends View> V findViewById(@IdRes int id) {
  141. return mRootView.findViewById(id);
  142. }
  143. protected <V extends View> V findActivityViewById(@IdRes int id) {
  144. return mActivity.findViewById(id);
  145. }
  146. /**
  147. * startActivity 方法优化
  148. */
  149. public void startActivity(Class<? extends Activity> cls) {
  150. startActivity(new Intent(mActivity, cls));
  151. }
  152. public void startActivityFinish(Class<? extends Activity> cls) {
  153. startActivityFinish(new Intent(mActivity, cls));
  154. }
  155. public void startActivityFinish(Intent intent) {
  156. startActivity(intent);
  157. finish();
  158. }
  159. /**
  160. * startActivityForResult 方法优化
  161. */
  162. private BaseActivity.ActivityCallback mActivityCallback;
  163. private int mActivityRequestCode;
  164. public void startActivityForResult(Class<? extends Activity> cls, BaseActivity.ActivityCallback callback) {
  165. startActivityForResult(new Intent(mActivity, cls), null, callback);
  166. }
  167. public void startActivityForResult(Intent intent, BaseActivity.ActivityCallback callback) {
  168. startActivityForResult(intent, null, callback);
  169. }
  170. public void startActivityForResult(Intent intent, Bundle options, BaseActivity.ActivityCallback callback) {
  171. if (mActivityCallback == null) {
  172. mActivityCallback = callback;
  173. // 随机生成请求码,这个请求码在 0 - 255 之间
  174. mActivityRequestCode = new Random().nextInt(255);
  175. startActivityForResult(intent, mActivityRequestCode, options);
  176. }else {
  177. // 回调还没有结束,所以不能再次调用此方法,这个方法只适合一对一回调,其他需求请使用原生的方法实现
  178. throw new IllegalArgumentException("Error, The callback is not over yet");
  179. }
  180. }
  181. @Override
  182. public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  183. if (mActivityCallback != null && mActivityRequestCode == requestCode) {
  184. mActivityCallback.onActivityResult(resultCode, data);
  185. mActivityCallback = null;
  186. } else {
  187. super.onActivityResult(requestCode, resultCode, data);
  188. }
  189. }
  190. /**
  191. * 销毁当前 Fragment 所在的 Activity
  192. */
  193. public void finish() {
  194. mActivity.finish();
  195. mActivity = null;
  196. }
  197. /**
  198. * 获取系统服务
  199. */
  200. @SuppressWarnings("unchecked")
  201. public <S extends Object> S getSystemService(@NonNull String name) {
  202. return (S) mActivity.getSystemService(name);
  203. }
  204. /**
  205. * Fragment返回键被按下时回调
  206. */
  207. public boolean onKeyDown(int keyCode, KeyEvent event) {
  208. //默认不拦截按键事件,传递给Activity
  209. return false;
  210. }
  211. }