Android Custom Loadpager page, no need to worry about duplicate code

Source: Internet
Author: User

On the internet for a long time, have not found their own desired results, so I wrote a, and put it on GitHub, like friends can go to download source to see. First on the code
Public abstract class Loadpager extends Framelayout {public static final int state_unkown = 0;    public static final int state_loading = 1;    public static final int state_error = 2;    public static final int state_empty = 3;    public static final int state_success = 4;    public int state = State_unkown; Private view loadingview;//Loading interface Private View errorview;//Error interface private view emptyview;//empty interface private View SUCC    essview;//loading Successful interface private Context Mcontext;        Public Loadpager (Context context) {super (context);        Mcontext = context;    Init ();        } public Loadpager (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);        Mcontext = context;    Init ();        } public Loadpager (context context, AttributeSet Attrs) {Super (context, attrs);        Mcontext = context;    Init (); } private void Init () {Loadingview = Createloadingview ();//created interface in load if (Loadingview! = null) {This.addview (Loadingview, New Framelayout.layoutparams (layoutparams.match_parent        , layoutparams.match_parent)); } Errorview = Createerrorview ();                    Load Error interface if (Errorview! = null) {This.addview (Errorview, New Framelayout.layoutparams (        Layoutparams.match_parent, layoutparams.match_parent)); } Emptyview = Createemptyview ();                    Loads an empty interface if (Emptyview! = null) {This.addview (Emptyview, New Framelayout.layoutparams (        Layoutparams.match_parent, layoutparams.match_parent));        } showpage (state_loading);//Display different interface according to different status}//Display different interface according to different status public void showpage (int result) {        state = result; if (Loadingview! = null) {loadingview.setvisibility (state = = State_unkown | | state = = STATE _loading?        View.VISIBLE:View.INVISIBLE); } if (Errorview! = null) {Errorview.setvisibility (state = = State_error?)        View.VISIBLE:View.INVISIBLE); } if (Emptyview! = null) {emptyview.setvisibility (state = = State_empty?)        View.VISIBLE:View.INVISIBLE); if (state = = state_success) {if (Successview = = null) {Successview = Createsuccessvie                W (); This.addview (Successview, New Framelayout.layoutparams (Layoutparams.match_parent, Layoutparams.mat            Ch_parent));        } successview.setvisibility (view.visible);            } else {if (Successview! = null) {successview.setvisibility (view.invisible); }}/* created an empty interface */private View Createemptyview () {return view.inflate (Mcontext, R.layout.loadpag    E_empty, NULL);                }/* created the error interface */private View Createerrorview () {View view = View.inflate (Mcontext, R.layout.loadpage_error, NULL);       Button PAGE_BT = (button) View.findviewbyid (R.ID.PAGE_BT);                Page_bt.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {            Show ();        }        });    return view; }/* Create interface in Load */private View Createloadingview () {return view.inflate (Mcontext, R.layout.loa    Dpage_loading, NULL); }//Depending on the server's data switchover status public void show () {if (state = = State_error | | state = = state_empty) {state        = state_loading;        } load ();    ShowPage (state);    }/*** * Create a successful interface */public abstract View Createsuccessview (); /** * Request Server * Must call the ShowPage method after the request is completed */protected abstract void Load ();}
The above comments are written very clearly, if you do not know this kind of friends please leave a message below, see I will reply to you.
The following shows how to use Loadpager in a base class so that we can write a lot less code later.
<pre name= "code" class= "Java" >public abstract class Basefragment extends fragment{protected Loadpager Mloadpager    = NULL; protected Context Mcontext = getactivity () = = null?    App.getinstance (): Getactivity ();    protected ViewGroup Mcontainer;    protected Layoutinflater Minflater; @Nullable @Override public View oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundl        E savedinstancestate) {mcontainer = container;        Minflater = Inflater; if (Mloadpager = = null) {Mloadpager = new Loadpager (mcontext) {@Override public                View Createsuccessview () {return BaseFragment.this.createSuccessView ();                } @Override protected void Load () {BaseFragment.this.loadData ();        }            };    } return Mloadpager; } @Override public void onactivitycreated (@Nullable Bundle SavedinstaNcestate) {super.onactivitycreated (savedinstancestate);    Show ();        public void Show () {if (mloadpager!=null) {mloadpager.show ();    }}/** * must call the Checkdata method after acquiring the data * */protected abstract void loaddata ();    Protected abstract View Createsuccessview (); /** CHECKSUM data */protected void Checkdata (Object datas) {if (datas==null) {mloadpager.showpage (loadpager.st ATE_ERROR)///Request Server failed}else{try {@SuppressWarnings ("unchecked") list<object> DS                = (list<object>) datas;                if (Ds.size () ==0) {mloadpager.showpage (loadpager.state_empty);                }else{mloadpager.showpage (loadpager.state_success);                }} catch (Exception e) {e.printstacktrace ();                if ("". Equals (Datas)) {mloadpager.showpage (loadpager.state_empty);                   }else{ Mloadpager.showpage (loadpager.state_success); }            }        }    }}
 below to see how we use 
in subclasses
</pre><pre code_snippet_id= "1677905" snippet_file_name= "blog_20160510_8_2136022" name= "code" class= "Java"     ><pre name= "code" class= "Java" >public class Errorfragment extends Basefragment {private String str = "loaded successfully"; @Override protected void LoadData () {New Thread (new Runnable () {@Override public voi                    D run () {try {thread.sleep (3000);  App.getmainthreadhandler (). Post (new Runnable () {@Override public void run ()                        {checkdata (NULL);                }                    });                } catch (Interruptedexception e) {e.printstacktrace ();    }}). Start ();        } @Override protected View Createsuccessview () {TextView TV = new TextView (mcontext); Tv.setlayoutparams (New Linearlayout.layoutparams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));        Tv.settext (str);    return TV; }}
Threads are used here to simulate the time-consuming operation of requesting data in the network.
OK, basically this is the case, if your code is not good enough, there is any good idea can give me a message or add me QQ347402035, please fill in the reason, otherwise you may add failure.



Android Custom Loadpager page, no need to worry about duplicate code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.