一般頁面有四種情況
加載中 :就是滾動頁面,后台獲取加載的數據,每個頁面的數據不同所以就讓子類來實現,直接抽象abstract了。
加載失敗 :一般都需要點擊后重新加載
空頁面 :也需要點擊后重新加載
加載成功 :顯示成功的頁面,每個頁面都不同所以讓子類實現,那必須是抽象的 abstract了
我采取的是每個頁面都是framelayout來顯示 加載的頁面。一共有四個頁面。通過加載的數據返回來的 狀態 進而讓頁面顯示相應的動畫
先屢一下思路
1 先加載三個頁面,開始都執行loading頁面
2 加載數據, 用到了線程池處理耗時炒作,具體如何訪問網絡讓子類來實現判斷數據是否可用
3 數據可用顯示 成功界面
數據不可用顯示 加載失敗頁面
數據的list比如為0 加載空頁面
1 package com.example.every_text.view; 2 3 import com.wang.cn.manager.ThreadManager; 4 import com.wang.cn.utils.UIUtils; 5 6 import android.content.Context; 7 import android.os.SystemClock; 8 import android.util.AttributeSet; 9 import android.view.View; 10 import android.widget.FrameLayout; 11 12 /** 13 * 先加順序 load -->showPagerView-->createSuccessView 14 *在子類中 耗時操作放到 load中,然后load返回一個狀態,在showPagerView中根據狀態選擇 顯示的頁面 15 *如果裝在是成功的。那么久顯示 createSuccessView 16 */ 17 public abstract class LoadingPager extends FrameLayout { 18 19 // 加載默認的狀態 20 private static final int STATE_UNLOADED = 1; 21 // 加載的狀態 22 private static final int STATE_LOADING = 2; 23 // 加載失敗的狀態 24 private static final int STATE_ERROR = 3; 25 // 加載空的狀態 26 private static final int STATE_EMPTY = 4; 27 // 加載成功的狀態 28 private static final int STATE_SUCCEED = 5; 29 30 private View mLoadingView;// 轉圈的view 31 private View mErrorView;// 錯誤的view 32 private View mEmptyView;// 空的view 33 private View mSucceedView;// 成功的view 34 35 private int mState;// 默認的狀態 36 37 private int loadpage_empty; 38 private int loadpage_error; 39 private int loadpage_loading; 40 41 public LoadingPager(Context context, int loading, int error, int empty) { 42 super(context); 43 loadpage_empty = empty; 44 loadpage_error = error; 45 loadpage_loading = loading; 46 init(); 47 } 48 49 public LoadingPager(Context context, AttributeSet attrs, int defStyle, 50 int loading, int error, int empty) { 51 super(context, attrs, defStyle); 52 loadpage_empty = empty; 53 loadpage_error = error; 54 loadpage_loading = loading; 55 init(); 56 } 57 58 public LoadingPager(Context context, AttributeSet attrs, int loading, 59 int error, int empty) { 60 super(context, attrs); 61 62 init(); 63 } 64 private void init() { 65 // 初始化狀態 66 mState = STATE_UNLOADED; 67 // 初始化三個 狀態的view 這個時候 三個狀態的view疊加在一起了 68 mLoadingView = createLoadingView(); 69 if (null != mLoadingView) { 70 addView(mLoadingView, new LayoutParams(LayoutParams.MATCH_PARENT, 71 LayoutParams.MATCH_PARENT)); 72 } 73 mErrorView = createErrorView(); 74 if (null != mErrorView) { 75 addView(mErrorView, new LayoutParams(LayoutParams.MATCH_PARENT, 76 LayoutParams.MATCH_PARENT)); 77 } 78 mEmptyView = createEmptyView(); 79 if (null != mEmptyView) { 80 addView(mEmptyView, new LayoutParams(LayoutParams.MATCH_PARENT, 81 LayoutParams.MATCH_PARENT)); 82 } 83 showSafePagerView(); 84 } 85 private void showSafePagerView() { 86 // 直接運行到主線程 87 UIUtils.runInMainThread(new Runnable() { 88 @Override 89 public void run() { 90 showPagerView(); 91 } 92 }); 93 } 94 private void showPagerView() { 95 96 // 這個時候 都不為空 mState默認是STATE_UNLOADED狀態所以只顯示 lodaing 下面的 error 97 // 和empty暫時不顯示 98 if (null != mLoadingView) { 99 mLoadingView.setVisibility(mState == STATE_UNLOADED 100 || mState == STATE_LOADING ? View.VISIBLE : 101 102 View.INVISIBLE); 103 } 104 if (null != mErrorView) { 105 mErrorView.setVisibility(mState == STATE_ERROR ? View.VISIBLE 106 : View.INVISIBLE); 107 } 108 if (null != mEmptyView) { 109 mEmptyView.setVisibility(mState == STATE_EMPTY ? View.VISIBLE 110 : View.INVISIBLE); 111 } 112 113 if (mState == STATE_SUCCEED && mSucceedView == null) { 114 mSucceedView = createSuccessView(); 115 addView(mSucceedView, new LayoutParams 116 117 (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 118 } 119 if (null != mSucceedView) { 120 mSucceedView.setVisibility(mState == STATE_SUCCEED ? 121 122 View.VISIBLE : View.INVISIBLE); 123 } 124 } 125 public void show() { 126 // 第一次進來肯定要 轉圈的 所以就算是 error和empty 也要讓狀態是 unload 127 if (mState == STATE_ERROR || mState == STATE_EMPTY) { 128 mState = STATE_UNLOADED; 129 } 130 // 如果是unload 就把狀態 變為 loading了 這時候從服務器拿數據 131 if (mState == STATE_UNLOADED) { 132 mState = STATE_LOADING; 133 134 TaskRunnable task = new TaskRunnable(); 135 ThreadManager.getLongPool().execute(task); 136 } 137 showSafePagerView(); 138 } 139 /** 140 * 制作界面 141 * 142 * @return 143 */ 144 protected abstract View createSuccessView(); 145 146 /** 147 * 處理下載 耗時操作 148 * 149 * @return 150 */ 151 protected abstract LoadResult load(); 152 153 /** 154 * 空界面 155 * 156 * @return 157 */ 158 public View createEmptyView() { 159 if (loadpage_empty != 0) { 160 return UIUtils.inflate(loadpage_empty); 161 } 162 return null; 163 164 } 165 166 /** 167 * 失敗的頁面 168 * 169 * @return 170 */ 171 public View createErrorView() { 172 if (loadpage_empty != 0) { 173 return UIUtils.inflate(loadpage_error); 174 } 175 return null; 176 } 177 178 /** 179 * 正在旋轉的頁面 180 * 181 * @return 182 */ 183 public View createLoadingView() { 184 if (loadpage_empty != 0) { 185 return UIUtils.inflate(loadpage_loading); 186 } 187 return null; 188 } 189 190 class TaskRunnable implements Runnable { 191 @Override 192 public void run() { 193 final LoadResult loadResult = load(); 194 SystemClock.sleep(500); 195 UIUtils.runInMainThread(new Runnable() { 196 @Override 197 public void run() { 198 mState = loadResult.getValue(); 199 showPagerView(); 200 } 201 }); 202 } 203 } 204 public enum LoadResult { 205 ERROR(3), EMPTY(4), SUCCESS(5); 206 int value; 207 208 LoadResult(int value) { 209 this.value = value; 210 } 211 212 public int getValue() { 213 return value; 214 } 215 } 216 }
1 package com.wang.cn.base; 2 3 import com.example.every_text.view.LoadingPager; 4 import com.example.every_text.view.LoadingPager.LoadResult; 5 import com.wang.cn.R; 6 import com.wang.cn.utils.UIUtils; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 13 /** 14 * @version 創建時間:2015年7月8日 上午11:31:11 類說明 activity的基類 15 */ 16 public abstract class BaseActivity extends Activity { 17 public LoadingPager loadingPage; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 23 loadingPage = new LoadingPager(UIUtils.getContext(), 24 R.layout.loadpage_loading, R.layout.loadpage_error, 25 R.layout.loadpage_empty)//加載了三個頁面 26 { 27 @Override 28 protected LoadResult load() { 29 return BaseActivity.this.load();//傳遞給子類 30 } 31 @Override 32 protected View createSuccessView() { 33 return BaseActivity.this.createSuccessView();//傳遞給子類 34 } 35 }; 36 // 可以點擊 37 loadingPage.setOnClickListener(new OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 loadingPage.show(); 41 } 42 }); 43 // 顯示 loading的頁面 44 loadingPage.show(); 45 setContentView(loadingPage); 46 } 47 48 /** 49 * 刷新頁面工程 50 * 51 * @return 52 */ 53 protected abstract View createSuccessView(); 54 55 /** 56 * 請求服務器 獲取當前狀態 57 * 58 */ 59 protected abstract LoadResult load(); 60 61 }
1 package com.wang.cn; 2 3 import android.content.Intent; 4 import android.os.SystemClock; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.TextView; 8 9 import com.example.every_text.view.LoadingPager.LoadResult; 10 import com.wang.cn.base.BaseActivity; 11 import com.wang.cn.utils.UIUtils; 12 import com.wang.cn.utils.ViewUtils; 13 14 /** 15 * @version 創建時間:2015年7月8日 上午11:31:11 類說明 主函數 16 */ 17 public class MainActivity extends BaseActivity { 18 // 刷新頁面工程 19 @Override 20 protected View createSuccessView() { 21 View inflate = UIUtils.inflate(R.layout.activity_main); 22 23 TextView tv=ViewUtils.findViewById(inflate, R.id.textView1); 24 tv.setOnClickListener(new OnClickListener() { 25 26 @Override 27 public void onClick(View v) { 28 Intent intent=new Intent(UIUtils.getContext(),FragmetActivity.class); 29 startActivity(intent); 30 } 31 }); 32 return inflate; 33 } 34 35 // 刷新頁面工程 36 @Override 37 protected LoadResult load() { 38 SystemClock.sleep(2000); 39 return LoadResult.SUCCESS; 40 } 41 }