zoukankan      html  css  js  c++  java
  • 3.加载界面

    1.  四种界面: 加载中, 加载错误,加载为空 ,加载成功
    2.  根据不同的状态去切换界面
    HomeFragment 
    1. public class HomeFragment extends Fragment {
    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 static int state = STATE_UNKOWN;
    8. @Override
    9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
    10. Bundle savedInstanceState) {
    11. if (frameLayout == null) { 
    12. // 之前的frameLayout 已经记录了一个爹了 爹是之前的ViewPager,先干掉之前的爹
    13. frameLayout = new FrameLayout(getActivity());
    14. init(); // 在FrameLayout中 添加4种不同的界面
    15. } else {
    16. ViewUtils.removeParent(frameLayout);// 移除frameLayout之前的爹
    17. }
    18. show();// 根据服务器的数据 切换状态
    19. return frameLayout; // 拿到当前viewPager 添加这个framelayout
    20. }
    21. private View loadingView;// 加载中的界面
    22. private View errorView;// 错误界面
    23. private View emptyView;// 空界面
    24. private View successView;// 加载成功的界面
    25. private FrameLayout frameLayout;
    26. // 在FrameLayout中 添加几种不同的界面
    27. private void init() {
    28. loadingView = createLoadingView(); // 创建了加载中的界面
    29. if (loadingView != null) {
    30. frameLayout.addView(loadingView, new FrameLayout.LayoutParams(
    31. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    32. }
    33. errorView = createErrorView(); // 加载错误界面
    34. if (errorView != null) {
    35. frameLayout.addView(errorView, new FrameLayout.LayoutParams(
    36. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    37. }
    38. emptyView = createEmptyView(); // 加载空的界面
    39. if (emptyView != null) {
    40. frameLayout.addView(emptyView, new FrameLayout.LayoutParams(
    41. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    42. }
    43. showPage();// 根据不同的状态显示不同的界面
    44. }
    45. // 根据不同的状态显示不同的界面
    46. private void showPage() {
    47. if (loadingView != null) {
    48. loadingView.setVisibility(state == STATE_UNKOWN
    49. || state == STATE_LOADING ? View.VISIBLE : View.INVISIBLE);
    50. }
    51. if (errorView != null) {
    52. errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE
    53. : View.INVISIBLE);
    54. }
    55. if (emptyView != null) {
    56. emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE
    57. : View.INVISIBLE);
    58. }
    59. if (state == STATE_SUCCESS) {
    60. successView = createSuccessView();
    61. if (successView != null) {
    62. frameLayout.addView(successView, new FrameLayout.LayoutParams(
    63. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    64. successView.setVisibility(View.VISIBLE);
    65. }
    66. }
    67. }
    68. private View createSuccessView() {
    69. TextView tv = new TextView(getActivity());
    70. tv.setText("加载成功了....");
    71. tv.setTextSize(30);
    72. return tv;
    73. }
    74. //枚举,相当于内部类
    75. public enum LoadResult {
    76. error(2), empty(3), success(4);
    77. int value;
    78. LoadResult(int value) {
    79. this.value = value;
    80. }
    81. public int getValue() {
    82. return value;
    83. }
    84. }
    85. // 根据服务器的数据 切换状态
    86. private void show() {
    87. if (state == STATE_ERROR || state == STATE_EMPTY) {
    88. state = STATE_LOADING;
    89. }
    90. // 请求服务器 获取服务器上数据 进行判断,模拟服务器
    91. // 请求服务器 返回一个结果
    92. new Thread() {
    93. public void run() {
    94. SystemClock.sleep(2000);//这个方法也能睡
    95. final LoadResult result = load();
    96. if (getActivity() != null) {
    97. getActivity().runOnUiThread(new Runnable() {//主线程刷新ui
    98. @Override
    99. public void run() {
    100. if (result != null) {
    101. state = result.getValue();
    102. showPage(); // 状态改变了,重新判断当前应该显示哪个界面
    103. }
    104. }
    105. });
    106. }
    107. };
    108. }.start();
    109. showPage();
    110. }
    111. private LoadResult load() {

    112. return LoadResult.success;
    113. }
    114. /* 创建了空的界面 */
    115. private View createEmptyView() {
    116. View view = View.inflate(getActivity(), R.layout.loadpage_empty, null);
    117. return view;
    118. }
    119. /* 创建了错误界面 */
    120. private View createErrorView() {
    121. View view = View.inflate(getActivity(), R.layout.loadpage_error, null);
    122. Button page_bt = (Button) view.findViewById(R.id.page_bt);
    123. page_bt.setOnClickListener(new OnClickListener() {
    124. @Override
    125. public void onClick(View v) {
    126. show();
    127. }
    128. });
    129. return view;
    130. }
    131. /* 创建加载中的界面 */
    132. private View createLoadingView() {
    133. View view = View
    134. .inflate(getActivity(), R.layout.loadpage_loading, null);
    135. return view;
    136. }
    137. }
    ViewUtils 
    1. public class ViewUtils {
    2. public static void removeParent(View v){
    3. // 先找到爹 在通过爹去移除孩子
    4. ViewParent parent = v.getParent();
    5. //所有的控件 都有爹 爹一般情况下 就是ViewGoup
    6. if(parent instanceof ViewGroup){
    7. ViewGroup group=(ViewGroup) parent;
    8. group.removeView(v);
    9. }
    10. }
    11. }

     





  • 相关阅读:
    Python 面向对象
    python Flask
    工作中的Python脚本
    python模块
    python函数
    Python小课题练习作业
    Python文件处理
    Maven 生成可执行的jar包
    Maven [ERROR] 不再支持源选项 5。请使用 6 或更高版本
    MySQL 导入导出数据
  • 原文地址:https://www.cnblogs.com/sixrain/p/4968933.html
Copyright © 2011-2022 走看看