Loading page

There are four situations for general pages

Loading: it is to scroll the page and get the loaded data in the background. The data of each page is different, so let the subclass implement it and directly abstract it.

Loading failed: generally, you need to click and reload

Empty page: also need to reload after clicking

Loading success: display the successful pages. Each page is different, so let the subclass implement it. It must be abstract

What I take is that each page is a framelayout to display the loaded pages. There are four pages. The status returned by the loaded data enables the page to display the corresponding animation

Think again and again

1 load three pages first, and then execute the loading page

2 loading data, using thread pool processing time-consuming hype, how to access the network for subclasses to determine whether the data is available

3 data available display success interface

Data unavailable display load failure page

list of data, such as loading empty page for 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 first -- & gt; showpagerview -- & gt; createsucessview
 14  *In the subclass, the time-consuming operation is put into the load, and then the load returns a status. In the showPagerView, select the displayed page according to the status
 15  *If the installation is successful. So long as the createsucessview is displayed
 16  */
 17 public abstract class LoadingPager extends FrameLayout {
 18  
 19     // Load default state
 20     private static final int STATE_UNLOADED = 1;
 21     // Loaded state
 22     private static final int STATE_LOADING = 2;
 23     // Status of load failure
 24     private static final int STATE_ERROR = 3;
 25     // Load empty state
 26     private static final int STATE_EMPTY = 4;
 27     // Status of loading success
 28     private static final int STATE_SUCCEED = 5;
 29  
 30     private View mLoadingView;// Circling view
 31     private View mErrorView;// FALSE view
 32     private View mEmptyView;// Empty view
 33     private View mSucceedView;// Successful view
 34  
 35     private int mState;// Default state
 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         // Initialization status
 66         mState = STATE_UNLOADED;
 67         // Initialize three state view Three states at this time view It's superimposed
 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         // Run directly to the main thread
 87         UIUtils.runInMainThread(new Runnable() {
 88             @Override
 89             public void run() {
 90                 showPagerView();
 91             }
 92         });
 93     }
 94     private void showPagerView() {
 95  
 96         // This time is not empty mState The default is STATE_UNLOADED Status so only display lodaing Underneath error
 97         // and empty Do not display temporarily
 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         // The first time I come in, it's bound to turn, so even if it's error and empty Let the state be unload
127         if (mState == STATE_ERROR || mState == STATE_EMPTY) {
128             mState = STATE_UNLOADED;
129         }
130         // If it is unload Just change the state to loading At this time, get the data from the server
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      * Production interface
141      *
142      * @return
143      */
144     protected abstract View createSuccessView();
145  
146     /**
147      * Handling download time consuming operations
148      *
149      * @return
150      */
151     protected abstract LoadResult load();
152  
153     /**
154      * Empty interface
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      * Failed pages
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      * Rotating page
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 Creation time: 11:31:11 a.m. July 8, 2015 base class describing 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)//Three pages loaded
26         {
27             @Override
28             protected LoadResult load() {
29                 return BaseActivity.this.load();//Pass to subclass
30             }
31             @Override
32             protected View createSuccessView() {
33                 return BaseActivity.this.createSuccessView();//Pass to subclass
34             }
35         };
36 //      Clickable
37         loadingPage.setOnClickListener(new OnClickListener() {
38             @Override
39             public void onClick(View v) {
40                 loadingPage.show();
41             }
42         });
43 //      display loading Page
44         loadingPage.show();
45         setContentView(loadingPage);
46     }
47  
48     /**
49      * Refresh page project
50      *
51      * @return
52      */
53     protected abstract View createSuccessView();
54  
55     /**
56      * Request server to get current status
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 Creation time: 11:31:11 a.m. on July 8, 2015
16  */
17 public class MainActivity extends BaseActivity {
18     // Refresh page project
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     // Refresh page project
36     @Override
37     protected LoadResult load() {
38         SystemClock.sleep(2000);
39         return LoadResult.SUCCESS;
40     }
41 }

Tags: Android network

Posted on Thu, 26 Mar 2020 12:43:03 -0400 by sureshmaharana