Today's app basically has two operations, one is to load the data, and the other is to display the data on the page. But if the page is more specific. To load data on each page, write the loading page. I used to write with dialog and extract a class. Where you need it, add the following code there. I've found that I've been adding loading code for most of the time. The total overtime for this purpose.
Please refer to this article for unlimited reference: http://blog.csdn.net/wanghao200906/article/details/46805085
Here are the times when the pages are more state
It's going to be a little bit more. Not only is the code not good-looking, but also I'm too tired to panic
Let's take a look at how to write loading efficiently.
General page has four kinds of situation
Loading: Is scrolling the page, the background to get the loaded data, each page of data is different so let the subclass to achieve, direct abstraction abstract.
Load failure: typically requires a click and reload
Empty page: Also need to click and reload
Load success: Display a successful page, each page is different so let the subclass implementation, it must be abstract abstraction
What I have taken is that each page is framelayout to display the loaded page. A total of four pages. Return a state by loading the data to let the page display the corresponding animation
first , a few thoughts.
1 load three pages first, execute loading page at the beginning
2 loading data, using the thread pool processing time-consuming hype, how to access the network to let subclasses to determine whether the data is available
3 data available to display a successful interface
Data unavailable display load Failure page
List of data such as 0 loading empty pages
Directly below the code time:
PackageCom.example.every_text.view;ImportCom.wang.cn.manager.ThreadManager;ImportCom.wang.cn.utils.UIUtils;ImportAndroid.content.Context;ImportAndroid.os.SystemClock;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.widget.FrameLayout;/** * First Order load-->showpagerview-->createsuccessview * * @author Wanghao * * Time-consuming operations in subclasses are put into load, then load returns A state, in Showpagerview, selects the displayed page according to the status * if installed on is successful. So long show Createsuccessview * * Public Abstract class loadingpager extends framelayout { //Load the default state Private Static Final intstate_unloaded =1;//Load status Private Static Final intState_loading =2;//Load Failed status Private Static Final intState_error =3;//Load Empty state Private Static Final intState_empty =4;//Load Status of Success Private Static Final intState_succeed =5;PrivateView Mloadingview;//view in circles PrivateView Merrorview;//Wrong view PrivateView Memptyview;//Empty view PrivateView Msucceedview;//Successful view Private intMstate;//Default status Private intLoadpage_empty;Private intLoadpage_error;Private intloadpage_loading; Public Loadingpager(Context context,intLoadingintErrorintEmpty) {Super(context); Loadpage_empty = empty; Loadpage_error = error; loadpage_loading = loading; Init (); } Public Loadingpager(context context, AttributeSet attrs,intDefstyle,intLoadingintErrorintEmpty) {Super(Context, attrs, Defstyle); Loadpage_empty = empty; Loadpage_error = error; loadpage_loading = loading; Init (); } Public Loadingpager(context context, AttributeSet attrs,intLoadingintErrorintEmpty) {Super(context, attrs); Init (); }Private void Init() {//Initialization statusMstate = state_unloaded;//Initialize view of three states this time the view of three states is superimposed on one another.Mloadingview = Createloadingview ();if(NULL! = Mloadingview) {AddView (Mloadingview,NewLayoutparams (Layoutparams.match_parent, layoutparams.match_parent)); } Merrorview = Createerrorview ();if(NULL! = Merrorview) {AddView (Merrorview,NewLayoutparams (Layoutparams.match_parent, layoutparams.match_parent)); } Memptyview = Createemptyview ();if(NULL! = Memptyview) {AddView (Memptyview,NewLayoutparams (Layoutparams.match_parent, layoutparams.match_parent)); } showsafepagerview (); }Private void Showsafepagerview() {//Run directly to the main threadUiutils.runinmainthread (NewRunnable () {@Override Public void Run() {Showpagerview (); } }); }Private void Showpagerview() {//This is not an empty mstate state_unloaded state, so it only shows lodaing the following error //And empty temporarily not displayed if(NULL! = Mloadingview) {mloadingview.setvisibility (mstate = = State_unloaded | | mstate = State_lo Ading? View.VISIBLE:View.INVISIBLE); }if(NULL! = Merrorview) {merrorview.setvisibility (mstate = = State_error? View.VISIBLE:View.INVISIBLE); }if(NULL! = Memptyview) {memptyview.setvisibility (mstate = = State_empty? View.VISIBLE:View.INVISIBLE); }if(Mstate = = State_succeed && Msucceedview = =NULL) {Msucceedview = Createsuccessview (); AddView (Msucceedview,NewLayoutparams (Layoutparams.match_parent, layoutparams.match_parent)); }if(NULL! = Msucceedview) {msucceedview.setvisibility (mstate = = State_succeed? View.VISIBLE:View.INVISIBLE); } } Public void Show() {//The first time in the circle must be in the circle so it is error and empty also want to let the state is unload if(mstate = = State_error | | mstate = = state_empty) {mstate = state_unloaded; }//If it's unload, turn the state into loading. Take the data from the server if(mstate = = state_unloaded) {mstate = state_loading; Taskrunnable task =NewTaskrunnable (); Threadmanager.getlongpool (). Execute (Task); } showsafepagerview (); }/** * Production interface * * @return * * protected AbstractViewCreatesuccessview();/** * Processing download time-consuming operation * * @return */ protected AbstractLoadresultLoad();/** * Empty interface * * @return * * PublicViewCreateemptyview() {if(Loadpage_empty! =0) {returnUiutils.inflate (Loadpage_empty); }return NULL; }/** * Failed page * * @return * * PublicViewCreateerrorview() {if(Loadpage_empty! =0) {returnUiutils.inflate (Loadpage_error); }return NULL; }/** * Spinning page * * @return * * PublicViewCreateloadingview() {if(Loadpage_empty! =0) {returnUiutils.inflate (loadpage_loading); }return NULL; } class Taskrunnable implements Runnable {@Override Public void Run() {FinalLoadresult loadresult = load (); Systemclock.sleep ( -); Uiutils.runinmainthread (NewRunnable () {@Override Public void Run() {mstate = Loadresult.getvalue (); Showpagerview (); } }); } } Public enumLoadresult {ERROR (3), EMPTY (4), SUCCESS (5);intValue Loadresult (intValue) { This. value = value; } Public int GetValue() {returnValue } }}
Here's how to use the
PackageCom.wang.cn.base;ImportCom.example.every_text.view.LoadingPager;ImportCom.example.every_text.view.LoadingPager.LoadResult;ImportCOM.WANG.CN.R;ImportCom.wang.cn.utils.UIUtils;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;/** * @author Wang * @version created: July 8, 2015 11:31:11 class describes the base class for activity */ Public Abstract class baseactivity extends Activity { PublicLoadingpager Loadingpage;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Loadingpage =NewLoadingpager (Uiutils.getcontext (), r.layout.loadpage_loading, R.layout.loadpage_error, R.lay Out.loadpage_empty)//three pages loaded{@Override protectedLoadresultLoad() {returnBaseactivity. This. Load ();//Pass to sub-class}@Override protectedViewCreatesuccessview() {returnBaseactivity. This. Createsuccessview ();//Pass to sub-class} };//You can clickLoadingpage.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {loadingpage.show (); } });//Show loading pageLoadingpage.show (); Setcontentview (Loadingpage); }/** * Refresh Page Project * * @return */ protected AbstractViewCreatesuccessview();/** * Request the server to get the current status * */ protected AbstractLoadresultLoad();}
What about the base class calling the parent class?
Packagecom.wang.cn;ImportAndroid.content.Intent;ImportAndroid.os.SystemClock;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView;ImportCom.example.every_text.view.LoadingPager.LoadResult;Importcom.wang.cn.base.BaseActivity;ImportCom.wang.cn.utils.UIUtils;ImportCom.wang.cn.utils.ViewUtils;/** * @author Wang * @version created: July 8, 2015 11:31:11 class description main function */ Public class mainactivity extends baseactivity { //Refresh page Project @Override protectedViewCreatesuccessview() {View inflate = uiutils.inflate (R.layout.activity_main); TextView Tv=viewutils.findviewbyid (inflate, r.id.textview1); Tv.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {Intent intent=NewIntent (Uiutils.getcontext (), fragmetactivity.class); StartActivity (Intent); } });returnInflate; }//Refresh page Project @Override protectedLoadresultLoad() {Systemclock.sleep ( -);returnloadresult.success; }}
So, let's get started. Loading data and display code are all done. Don't ever have to worry about writing loading. Also need not because of handler get a lot of msg trouble where Add.
Code: http://download.csdn.net/detail/wanghao200906/8880719 (with tools I've used for years)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Efficient extraction of loading, no more loading pages are not afraid