An awesome Android custom load control _android

Source: Internet
Author: User
Tags visibility

In the design of applications, we should love minimalism, simple is good, for many users, complex things are not popular.
I want to achieve is according to different situations to show different loading effect, with Tiaogan, the effect is to draw lessons from the effect of a project, I think it is necessary to extract the package to improve the future use. The situation is roughly divided into: loading, no network, no data, loading failure, and so on, these just need a view can be done!

Preview the effect chart:

How do we achieve this?
The layout of the View_loading.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <linearlayout android:id= "@+id/lin_loading" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ gravity= "center" android:visibility= "Gone" android:orientation= "vertical" > <imageview android:id= "@+id/img" _loading "android:layout_width=" 100DP "android:layout_height=" 100DP "android:src=" @drawable/loading_animation "/&

  Gt <textview android:id= "@+id/tv_loading" android:layout_width= wrap_content "android:layout_height=" wrap_content "Android:layout_margintop=" 16DP "android:layout_gravity=" Center_horizontal "android:textsize=" 14sp "/> < /linearlayout> <linearlayout android:id= "@+id/lin_load" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:layout_gravity= "Center" android:gravity= "center" android:orientation= "vertical" android:visibility= "visible" > &L T;imageview android:id= "@+id/iv_load" android:layout_width= "100DP" android:layout_height= "100DP" android:src= " @mipmap/ic_launcher "/> <textview android:id= @+id/tv_load" android:layout_width= "Wrap_content" Android: Layout_gravity= "Center_horizontal" android:layout_height= "wrap_content"/> <button android:id= "@+id/btn_lo" Ad "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:layout_margintop=" 8DP "an
 Droid:layout_gravity= "Center_horizontal" android:textsize= "14sp"/> </LinearLayout> </FrameLayout>

From the layout point of view, I divided two parts, one is loaded, the other is with Imagview, text and button layout, someone saw this, will say, wow, this is not very simple? According to the different circumstances to set the value of visibility ah, yes, the principle is this.

The code for Xhloadingview.java is as follows:

Package com.woyou.loadingdemo.widget;
Import Android.content.Context;
Import android.graphics.drawable.AnimationDrawable;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.FrameLayout;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;
Import Com.woyou.loadingdemo.LoadingState;


Import COM.WOYOU.LOADINGDEMO.R;
 /** * Created by Xiho on 11:21.
 * * public class Xhloadingview extends Framelayout {private context mcontext;
 The layout of the load private linearlayout mlinearload;

 Other loaded layouts are private linearlayout mlinearloading;

 Private TextView mtvloading;

 Private TextView mtvload;

 Private ImageView mivloading;

 Private ImageView mivload;

 Private Button mbtnload;

 Private Loadingstate mstate;


 Private animationdrawable animation;
  Public Xhloadingview {Super (context);
 Mcontext = context; Public Xhloadingview (COntext context, AttributeSet attrs) {Super (context, attrs);
 Mcontext = context;
  Public Xhloadingview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
 Mcontext = context;  Public Xhloadingview (context, AttributeSet attrs, int defstyleattr, int defstyleres) {Super (context, attrs,
  Defstyleattr, Defstyleres);

 Mcontext = context;

  public void Build () {Layoutinflater.from (Mcontext). Inflate (r.layout.view_loading, this, true);

  mlinearloading = (linearlayout) Findviewbyid (r.id.lin_loading);

  Mlinearload = (linearlayout) Findviewbyid (r.id.lin_load);

  mivloading = (ImageView) Findviewbyid (r.id.img_loading);

  Mivload = (ImageView) Findviewbyid (r.id.iv_load);

  mtvloading = (TextView) Findviewbyid (r.id.tv_loading);

  Mtvload = (TextView) Findviewbyid (r.id.tv_load);

  Mbtnload = (Button) Findviewbyid (r.id.btn_load); Mbtnload.setonclicklistener (New Onclicklistener () {@Override public void OnClick (VIew v) {setstate (loadingstate.state_loading);
   Monretrylistener.onretry ();

 }
  });
  @Override public void setvisibility (int visibility) {super.setvisibility (visibility); if (view.gone==visibility && mstate==loadingstate.state_loading && animation!=null&&
  Animation.isrunning ()) {animation.stop ();
 }/** * Loading prompt text/private String mloadingtext;
 private int Mloadingicon;
  Public Xhloadingview withloadingicon (int resid) {Mloadingicon = Resid;
 return this;
 /** * Load data as NULL hint text/private String mloademptytext;
 private int Mloademptyicon;
  Public Xhloadingview withemptyicon (int resid) {Mloademptyicon = Resid;
 return this;
 /** * No network hint/private String mloadnonetworktext;
 private int Mnonetworkicon;
  Public Xhloadingview withnoneticon (int resid) {Mnonetworkicon = Resid;
 return this;

 Private Onretrylistener Monretrylistener; /** * Defines a retry interface/public interface Onretrylistener {void Onretry (); Public Xhloadingview Withonretrylistener (Onretrylistener monretrylistener) {This.monretrylistener = MOnRetryListene
  R
 return this;
  /** * Sets the state of loading * @param states * */public void SetState (loadingstate) {if (mstate==state) {return;
   }else if (state==loadingstate.state_loading) {mlinearloading.setvisibility (VISIBLE);
  Mlinearload.setvisibility (GONE);
   }else if (state!=loadingstate.state_loading) {mlinearloading.setvisibility (GONE);
   Mlinearload.setvisibility (VISIBLE);
  if (animation!=null && mstate==loadingstate.state_loading) animation.stop ();
 } changestate (state);
 public Boolean btnemptyenable = true;
 public Boolean btnerrorenable = true;

 public Boolean btnnonetworkenable = true;
  Public Xhloadingview withbtnnonetennable (Boolean ennable) {btnnonetworkenable = ennable;
 return this;
  Public Xhloadingview withbtnerrorennable (Boolean ennable) {btnerrorenable = ennable;
 return this; Public Xhloadingview WIThbtnemptyennable (Boolean ennable) {btnemptyenable = ennable;
 return this;  /** * @param state/private void Changestate (loadingstate) {switch (states) {//Loaded case
    State_loading:mstate = loadingstate.state_loading;
    Mivloading.setimageresource (Mloadingicon);
    Mtvloading.settext (Mloadingtext);
    if (animation = = null) {animation = (animationdrawable) mivloading.getdrawable ();
    } if (animation!= null) Animation.start ();
   Break
    The data is empty case state_empty:mstate = loadingstate.state_empty;
    Mivload.setimageresource (Mloademptyicon);
    Mtvload.settext (Mloademptytext);
     if (btnemptyenable) {mbtnload.setvisibility (VISIBLE);
    Mbtnload.settext (Btn_empty_text);
    else {mbtnload.setvisibility (GONE);
   } break;
    Load failed case state_error:mstate = loadingstate.state_error;
    Mivload.setimageresource (Merrorico);
    Mtvload.settext (Mloaderrortext);
if (btnerrorenable) {     Mbtnload.setvisibility (VISIBLE);
    Mbtnload.settext (Btn_error_text);
    else {mbtnload.setvisibility (GONE);
   } break;
    No network case state_no_net:mstate = loadingstate.state_no_net;
    Mivload.setimageresource (Mnonetworkicon);
    Mtvload.settext (Mloadnonetworktext);
     if (btnnonetworkenable) {mbtnload.setvisibility (VISIBLE);
    Mbtnload.settext (Btn_nonet_text);
    else {mbtnload.setvisibility (GONE);
  } break;
 }/** * Background or local error prompt * * Private String Mloaderrortext;

 private int Merrorico;
  Public Xhloadingview witherrorico (int resid) {Merrorico = Resid;
 return this; /** * Load NULL data * @param RESID * @return/public xhloadingview withloademptytext (int resid) {Mloademptytext
  = Getresources (). getString (RESID);
 return this;
  Public Xhloadingview Withloademptytext (String mloademptytext) {this.mloademptytext = Mloademptytext;
 return this;
 /** * Load text without network * @param resid * @return */Public Xhloadingview withloadnonetworktext (int resid) {mloadnonetworktext = Getresources (). getString (RESID);
 return this;
 Public String Btn_empty_text = "Retry";
 Public String Btn_error_text = "Retry";

 Public String Btn_nonet_text = "Retry"; /** * Text hint for button with blank * data * @param text * @return/public Xhloadingview withbtnemptytext (String text) {THIS.BTN
  _empty_text = text;
 return this; /** * Text hint for the button that loaded the error * @param text * @return/public Xhloadingview withbtnerrortext (String text) {This
  . btn_error_text = text;
 return this; /** * Load Error text hint * @param resid * @return/public xhloadingview withloaderrortext (int resid) {This.mloade
  Rrortext = Getresources (). getString (RESID);
 return this;
  Public Xhloadingview Withloaderrortext (String mloadederrortext) {this.mloaderrortext = Mloadederrortext;
 return this;
  /** * Text hint for a button without a network * @param text * @return/public Xhloadingview withbtnnonettext (String text) {This.btn_nonet_text = text;
 return this; /** * Load No network text hint * @param mloadednonettext * @return/Public Xhloadingview withloadnonetworktext (String ML
  Oadednonettext) {this.mloadnonetworktext = Mloadednonettext;
 return this;
  Public Xhloadingview withloadingtext (int resid) {this.mloadingtext = Getresources (). getString (RESID);
 return this;
  Public Xhloadingview Withloadingtext (String mloadingtext) {this.mloadingtext = Mloadingtext;
 return this;

 }

}

Different processing is done for different situations, and then we call on the activity in need.

 Private Xhloadingview Mloadingview;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_display);
  Mloadingview = (Xhloadingview) Findviewbyid (r.id.lv_loading); Mloadingview.withloademptytext ("≥﹏≤, what also wood has!"). Withemptyicon (R.drawable.disk_file_no_data). Withbtnemptyennable (False). Witherrorico (R.drawable.ic_chat_empty). Withloaderrortext (῀ (˙᷄ỏ˙᷅) ῀) ᵒᵐᵍᵎᵎᵎ, my family program ape ran away! ").
     Withbtnerrortext ("Smelly dog!!!") . Withloadnonetworktext ("You are blocking the signal O ( ̄ヘ ̄o) ☞ᗒᗒ you Go"). Withnoneticon (R.drawable.ic_chat_empty). Withbtnnonettext ("The net is ready, try again. "). Withloadingicon (r.drawable.loading_animation). Withloadingtext (" Loading ... "). Withonretrylistener (New Xhloadingview.onretrylistener () {@Override public void Onretry () {snackbarutil.show (Mloadingview, "already trying to try again.")
    , 0);
  }). build ();
  } ...//loading mloadingview.setvisibility (view.visible);

 Mloadingview.setstate (loadingstate.state_loading); Null data MloadinGview.setvisibility (view.visible);
  Mloadingview.setstate (Loadingstate.state_empty)//No network mloadingview.setvisibility (view.visible);

 Mloadingview.setstate (loadingstate.state_no_net);
  Load Error mloadingview.setvisibility (view.visible);

Mloadingview.setstate (Loadingstate.state_error);

 .......


 }

Above I was in order to show different effects, so wrote a few click events, the actual is not such, after initialization, in need of the place call OK, the source code comments in detail, you do not have to do too much explanation!

Complete code: Xhloadingview

Author: Xuhao
qq:504105930
Reprint please indicate the source: http://blog.csdn.net/u011974987/article/details/51455333

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

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.