Android Custom Universal Loadingview

Source: Internet
Author: User

Introduced

Long time no blog, recently in close to the new year, years ago to finish the work, so special busy, the weekend to write a general load view, write a blog share.

Function

1, display load view, load failed to display the load failure view, the data is empty when the display data is empty view, support for the failure view set click events Reload data.

2, support personalization, custom settings loading, failure, empty data view.

First put a pressure yajing

Realize

Implementation of the idea is actually a framelayout add three layout to do processing display hidden, custom view is actually replace the inside view, the code is relatively simple, if directly read my custom view series articles, or to customize the view of the understanding, it is easy to understand, All directly on the code.

Specific code

Java code

 Public  class commonloadingview extends framelayout {    //display text at load    protectedTextView Mloadingtexttv; PublicContext Mcontext;//Load Error view    protectedLinearLayout Mloaderrorll;//Load Error Click event Handling    PrivateLoadinghandler Mloadinghandler;//Load View    PrivateView Loadingview;//Load failed view    PrivateView Loadingerrorview;//Data is empty    PrivateView Emptyview; Public Commonloadingview(context context, AttributeSet attrs) { This(Context, Attrs,0); } Public Commonloadingview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);    Mcontext = context; } Public void Setloadinghandler(Loadinghandler Loadinghandler)    {Mloadinghandler = Loadinghandler; } Public void Setloadingerrorview(View Loadingerrorview) { This. Removeviewat (1); This. Loadingerrorview = Loadingerrorview; This. Loadingerrorview.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {if(Mloadinghandler! =NULL) {mloadinghandler.dorequestdata (); Commonloadingview. This. Load (); }            }        }); This. AddView (Loadingerrorview,1); } Public void Setloadingview(View Loadingview) { This. Removeviewat (0); This. Loadingview = Loadingview; This. AddView (Loadingview,0); }@Override    protected void onfinishinflate() {Super. Onfinishinflate (); Loadingview = Inflate (Mcontext, R.layout.common_loading_view,NULL); Loadingerrorview = Inflate (Mcontext, R.layout.network_layout,NULL); Emptyview = Inflate (Mcontext, R.layout.empty_layout,NULL); This. AddView (Loadingview); This. AddView (Loadingerrorview); This. AddView (Emptyview);        Loadingerrorview.setvisibility (GONE);        Emptyview.setvisibility (GONE); Initview ( This); } Public void Setmessage(String message)    {mloadingtexttv.settext (message); }Private void Initview(View Rootview)        {Mloadingtexttv = (TextView) Rootview.findviewbyid (R.ID.LOADING_TEXT_TV);        Mloaderrorll = (linearlayout) Rootview.findviewbyid (R.ID.LOAD_ERROR_LL); Mloaderrorll.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {if(Mloadinghandler! =NULL) {Commonloadingview. This. Load ();                Mloadinghandler.dorequestdata ();    }            }        }); } Public void Load() {loadingview.setvisibility (VISIBLE);        Loadingerrorview.setvisibility (GONE);    Emptyview.setvisibility (GONE); } Public void Load(String message)        {mloadingtexttv.settext (message);        Loadingview.setvisibility (VISIBLE);        Loadingerrorview.setvisibility (GONE);    Emptyview.setvisibility (GONE); } Public void loadsuccess(){ This. loadsuccess (false); } Public void loadsuccess(BooleanIsEmpty) {loadingview.setvisibility (GONE); Loadingerrorview.setvisibility (GONE);if(IsEmpty)        {emptyview.setvisibility (VISIBLE); }Else{emptyview.setvisibility (GONE); }    } Public void Loaderror() {loadingview.setvisibility (GONE);    Loadingerrorview.setvisibility (VISIBLE); } Public  interface loadinghandler{        voidDorequestdata (); }}
Use basic usage

Several basic load Loaderror loadsucccess methods are used.

 Public  class defaultviewactivity extends appcompatactivity {    protectedListView Mlistview;protectedCommonloadingview Mloadingview;PrivateList<string> mlist =NewArraylist<> (); Arrayadapter adapter;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);Super. Setcontentview (R.layout.activity_default_view);    Initview (); }Private void Initview() {Mlistview = (ListView) Findviewbyid (R.id.listview);        Mloadingview = (Commonloadingview) Findviewbyid (R.id.loadingview); Mloadingview.load ();//Set Click Error View reload eventMloadingview.setloadinghandler (NewCommonloadingview.loadinghandler () {@Override             Public void Dorequestdata() {mloadingview.postdelayed (NewRunnable () {@Override                     Public void Run() { for(inti =1; I <= -; i++) {Mlist.add (i+""); } adapter =NewArrayadapter (defaultviewactivity. ThisAndroid. R.layout.simple_list_item_1, Android.                        R.id.text1, mlist);                        Mlistview.setadapter (adapter); Mloadingview.loadsuccess (false); }                },2500); }        });//Analog network error, load failureMloadingview.postdelayed (NewRunnable () {@Override             Public void Run() {mloadingview.loaderror (); }        },2500); }}
Custom views Use

You only need to set your own custom view call set method.

this.mLoadingView.setLoadingView(loadingView);    this.mLoadingView.setLoadingErrorView(loadingErrorView);
 Public  class customviewactivity extends appcompatactivity {    protectedListView Mlistview;protectedCommonloadingview Mloadingview;PrivateList<string> mlist =NewArraylist<> (); Arrayadapter adapter;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);Super. Setcontentview (R.layout.activity_default_view);    Initview (); }Private void Initview() {Mlistview = (ListView) Findviewbyid (R.id.listview); Mloadingview = (Commonloadingview) Findviewbyid (R.id.loadingview);//Set Custom ViewProgressBar ProgressBar =NewProgressBar ( This); This. Mloadingview.setloadingview (ProgressBar); TextView TextView =NewTextView ( This); Textview.settext ("Load failed ..."); This. Mloadingview.setloadingerrorview (TextView); Mloadingview.load ();//Set Click Error View reload eventMloadingview.setloadinghandler (NewCommonloadingview.loadinghandler () {@Override             Public void Dorequestdata() {mloadingview.postdelayed (NewRunnable () {@Override                     Public void Run() { for(inti =1; I <= -; i++) {Mlist.add (i+""); } adapter =NewArrayadapter (customviewactivity. ThisAndroid. R.layout.simple_list_item_1, Android.                        R.id.text1, mlist);                        Mlistview.setadapter (adapter); Mloadingview.loadsuccess (false); }                },2500); }        });//Analog network error, load failureMloadingview.postdelayed (NewRunnable () {@Override             Public void Run() {mloadingview.loaderror (); }        },2500); }}

As for the specific layout and style files are not posted, mainly to realize the idea, code

Download please refer to the source code download, remember to praise Yo!

Android Custom Universal Loadingview

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.