5.代码抽取(加载界面)

简单框架的搭建主要就是泛型T和抽象类(让子类去实现)的运用
抽取3中的代码,加载界面逻辑是一样的分别抽取到 BaseFragment和 LoadingPage中。自定义布局如果不在布局写,那就直接new就可以
LoadingPage是自定义的帧布局
  1. public abstract class LoadingPage extends FrameLayout {
  2. public static final int STATE_UNKOWN = 0;
  3. public static final int STATE_LOADING = 1;
  4. public static final int STATE_ERROR = 2;
  5. public static final int STATE_EMPTY = 3;
  6. public static final int STATE_SUCCESS = 4;
  7. public int state = STATE_UNKOWN;
  8. private View loadingView;// 加载中的界面
  9. private View errorView;// 错误界面
  10. private View emptyView;// 空界面
  11. private View successView;// 加载成功的界面
  12. public LoadingPage(Context context) {
  13. super(context);
  14. init();
  15. }
  16. public LoadingPage(Context context, AttributeSet attrs, int defStyle) {
  17. super(context, attrs, defStyle);
  18. init();
  19. }
  20. public LoadingPage(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. init();
  23. }
  24. private void init() {
  25. loadingView = createLoadingView(); // 创建了加载中的界面
  26. if (loadingView != null) {
  27. this.addView(loadingView, new FrameLayout.LayoutParams(
  28. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  29. }
  30. errorView = createErrorView(); // 加载错误界面
  31. if (errorView != null) {
  32. this.addView(errorView, new FrameLayout.LayoutParams(
  33. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  34. }
  35. emptyView = createEmptyView(); // 加载空的界面
  36. if (emptyView != null) {
  37. this.addView(emptyView, new FrameLayout.LayoutParams(
  38. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  39. }
  40. showPage();// 根据不同的状态显示不同的界面
  41. }
  42. // 根据不同的状态显示不同的界面
  43. private void showPage() {
  44. if (loadingView != null) {
  45. loadingView.setVisibility(state == STATE_UNKOWN
  46. || state == STATE_LOADING ? View.VISIBLE : View.INVISIBLE);
  47. }
  48. if (errorView != null) {
  49. errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE
  50. : View.INVISIBLE);
  51. }
  52. if (emptyView != null) {
  53. emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE
  54. : View.INVISIBLE);
  55. }
  56. if (state == STATE_SUCCESS) {
  57. if (successView == null) {
  58. successView = createSuccessView();
  59. this.addView(successView, new FrameLayout.LayoutParams(
  60. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  61. }
  62. successView.setVisibility(View.VISIBLE);
  63. } else {
  64. if (successView != null) {
  65. successView.setVisibility(View.INVISIBLE);
  66. }
  67. }
  68. }
  69. /* 创建了空的界面 */
  70. private View createEmptyView() {
  71. View view = View.inflate(UiUtils.getContext(), R.layout.loadpage_empty,
  72. null);
  73. return view;
  74. }
  75. /* 创建了错误界面 */
  76. private View createErrorView() {
  77. View view = View.inflate(UiUtils.getContext(), R.layout.loadpage_error,
  78. null);
  79. Button page_bt = (Button) view.findViewById(R.id.page_bt);
  80. page_bt.setOnClickListener(new OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. show();
  84. }
  85. });
  86. return view;
  87. }
  88. /* 创建加载中的界面 */
  89. private View createLoadingView() {
  90. View view = View.inflate(UiUtils.getContext(),
  91. R.layout.loadpage_loading, null);
  92. return view;
  93. }
  94. public enum LoadResult {
  95. error(2), empty(3), success(4);
  96. int value;
  97. LoadResult(int value) {
  98. this.value = value;
  99. }
  100. public int getValue() {
  101. return value;
  102. }
  103. }
  104. // 根据服务器的数据 切换状态
  105. public void show() {
  106. if (state == STATE_ERROR || state == STATE_EMPTY) {
  107. state = STATE_LOADING;
  108. }
  109. // 请求服务器 获取服务器上数据 进行判断
  110. // 请求服务器 返回一个结果
  111. ThreadManager.getInstance().createLongPool().execute(new Runnable() {
  112. @Override
  113. public void run() {
  114. SystemClock.sleep(2000);
  115. final LoadResult result = load();
  116. UiUtils.runOnUiThread(new Runnable() {
  117. @Override
  118. public void run() {
  119. if (result != null) {
  120. state = result.getValue();
  121. showPage(); // 状态改变了,重新判断当前应该显示哪个界面
  122. }
  123. }
  124. });
  125. }
  126. });
  127. showPage();
  128. }
  129. /***
  130. * 创建成功的界面
  131. *
  132. * @return
  133. */
  134. public abstract View createSuccessView();
  135. /**
  136. * 请求服务器
  137. *
  138. * @return
  139. */
  140. protected abstract LoadResult load();
  141. }
在uiutils添加这个方法
  1. /**
  2. * 把Runnable 方法提交到主线程运行
  3. * @param runnable
  4. */
  5. public static void runOnUiThread(Runnable runnable) {
  6. // 在主线程运行
  7. if(android.os.Process.myTid()==BaseApplication.getMainTid()){
  8. runnable.run();
  9. }else{
  10. //获取handler
  11. BaseApplication.getHandler().post(runnable);
  12. }
  13. }
修改BaseApplication
  1. public class BaseApplication extends Application {
  2. private static BaseApplication application;
  3. private static int mainTid;
  4. private static Handler handler;
  5. @Override
  6. // 在主线程运行的
  7. public void onCreate() {
  8. super.onCreate();
  9. application=this;
  10. mainTid = android.os.Process.myTid();
  11. handler=new Handler();
  12. }
  13. public static Context getApplication() {
  14. return application;
  15. }
  16. public static int getMainTid() {
  17. return mainTid;
  18. }
  19. public static Handler getHandler() {
  20. return handler;
  21. }
  22. }
BaseFragment 
  1. public abstract class BaseFragment extends Fragment {
  2. private LoadingPage loadingPage;
  3. protected BitmapUtils bitmapUtils;
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  6. Bundle savedInstanceState) {
  7. bitmapUtils = BitmapHelper.getBitmapUtils();
  8. if (loadingPage == null) { // 之前的frameLayout 已经记录了一个爹了 爹是之前的ViewPager
  9. loadingPage = new LoadingPage(getActivity()) {
  10. @Override
  11. public View createSuccessView() {
  12. return BaseFragment.this.createSuccessView();
  13. }
  14. @Override
  15. protected LoadResult load() {
  16. return BaseFragment.this.load();
  17. }
  18. };
  19. } else {
  20. ViewUtils.removeParent(loadingPage);// 移除frameLayout之前的爹
  21. }
  22. return loadingPage; // 拿到当前viewPager 添加这个framelayout
  23. }
  24. /***
  25. * 创建成功的界面
  26. *
  27. * @return
  28. */
  29. public abstract View createSuccessView();
  30. /**
  31. * 请求服务器
  32. *
  33. * @return
  34. */
  35. protected abstract LoadResult load();
  36. public void show() {
  37. if (loadingPage != null) {
  38. loadingPage.show();
  39. }
  40. }
  41. /** 校验数据 */
  42. public LoadResult checkData(List datas) {
  43. if (datas == null) {
  44. return LoadResult.error;// 请求服务器失败
  45. } else {
  46. if (datas.size() == 0) {
  47. return LoadResult.empty;
  48. } else {
  49. return LoadResult.success;
  50. }
  51. }
  52. }
  53. }
这样子类只需要实现createSuccessView和load这俩个方法就可以了
自定义个xutils功能一样的BitmapHelper 
 
  1. public class BitmapHelper {
  2. private BitmapHelper() {
  3. }
  4. private static BitmapUtils bitmapUtils;
  5. /**
  6. * BitmapUtils不是单例的 根据需要重载多个获取实例的方法
  7. *
  8. * @param appContext
  9. * application context
  10. * @return
  11. */
  12. public static BitmapUtils getBitmapUtils() {
  13. if (bitmapUtils == null) {
  14. // 第二个参数 缓存图片的路径 // 加载图片 最多消耗多少比例的内存(16M) 0.05-0.8f
  15. bitmapUtils = new BitmapUtils(UiUtils.getContext(), FileUtils
  16. .getIconDir().getAbsolutePath(), 0.3f);
  17. }
  18. return bitmapUtils;
  19. }
  20. }
UiUtils 添加这俩个方法,省略了代码的书写 
  1. public static View inflate(int id) {
  2. return View.inflate(getContext(), id, null);
  3. }
  4. public static Drawable getDrawalbe(int id) {
  5. return getResource().getDrawable(id);
  6. }





posted @ 2015-11-16 16:04  梦和远方  阅读(209)  评论(0编辑  收藏  举报