• android 加载中、无网络、无数据、出错 四种状态的代码封装


    package com.weavey.loading.lib;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.support.annotation.ColorRes;
    import android.support.annotation.DrawableRes;
    import android.support.annotation.IntDef;
    import android.support.annotation.LayoutRes;
    import android.support.annotation.NonNull;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    import android.widget.TextView;


    /**
    * create by Weavey
    * on date 2016-03-12
    * TODO
    */
    public class LoadingLayout extends FrameLayout{

    public final static int Success = 0;
    public final static int Empty = 1;
    public final static int Error = 2;
    public final static int No_Network = 3;
    public final static int Loading = 4;
    private int state;

    private Context mContext;
    private View loadingPage;
    private View errorPage;
    private View emptyPage;
    private View networkPage;
    private View defineLoadingPage;

    /**
    * 错误的图片
    */
    private ImageView errorImg;
    /**
    * 空的图片
    */
    private ImageView emptyImg;
    /**
    * 没有网图片
    */
    private ImageView networkImg;

    /**
    * 错误的文字
    */
    private TextView errorText;
    /**
    * 为空时显示的文字
    */
    private TextView emptyText;
    /**
    * 没有网的文字
    */
    private TextView networkText;
    /**
    * 错误时 重新加载的按钮
    */
    private TextView errorReloadBtn;
    /**
    * 无网时 重新加载的按钮
    */
    private TextView networkReloadBtn;

    /**
    * 显示内容的View
    */
    private View contentView;
    private OnReloadListener listener;
    private boolean isFirstVisible; //是否一开始显示contentview,默认不显示

    //配置
    private static Config mConfig = new Config();
    private static String emptyStr = "暂无数据";
    private static String errorStr = "加载失败,请稍后重试···";
    private static String netwrokStr = "无网络连接,请检查网络···";
    private static String reloadBtnStr = "点击重试";
    private static int emptyImgId = R.mipmap.empty;
    private static int errorImgId = R.mipmap.error;
    private static int networkImgId = R.mipmap.no_network;
    private static int reloadBtnId = R.drawable.selector_btn_back_gray;
    private static int tipTextSize = 14;
    private static int buttonTextSize = 14;
    private static int tipTextColor = R.color.base_text_color_light;
    private static int buttonTextColor = R.color.base_text_color_light;
    private static int buttonWidth = -1;
    private static int buttonHeight = -1;
    /**
    * 加载时的 布局
    */
    private static int loadingLayoutId = R.layout.widget_loading_page;
    /**
    * 背景颜色
    */
    private static int backgroundColor = R.color.base_loading_background;

    public LoadingLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    // 用attrs 来进行配置 xml的属性
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadingLayout);
    isFirstVisible = a.getBoolean(R.styleable.LoadingLayout_isFirstVisible, false);
    a.recycle();
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.mContext = context;
    }

    public LoadingLayout(Context context) {
    super(context);
    this.mContext = context;
    }

    /**
    * 当View中所有的子控件均被映射成xml后触发 也就是说当此ViewGroup 中的xml 初始化完毕的时候 调用此方法(个人理解)
    * <p>
    * 次布局中只能放一个 子类 建议用 ll
    */
    @Override
    protected void onFinishInflate() {
    super.onFinishInflate();
    if (getChildCount() > 1) {
    throw new IllegalStateException("LoadingLayout can host only one direct child");
    }
    contentView = this.getChildAt(0); //得到布局下的 View (content的View)
    if (!isFirstVisible) {
    contentView.setVisibility(View.GONE);
    }
    build();
    }

    private void build() {

    /**
    * loading圈
    */
    loadingPage = LayoutInflater.from(mContext).inflate(loadingLayoutId, null);
    /**
    * 错误的界面
    */
    errorPage = LayoutInflater.from(mContext).inflate(R.layout.widget_error_page, null);
    /**
    * 空的界面
    */
    emptyPage = LayoutInflater.from(mContext).inflate(R.layout.widget_empty_page, null);
    /**
    * 没有网的界面
    */
    networkPage = LayoutInflater.from(mContext).inflate(R.layout.widget_nonetwork_page, null);
    /**
    * 默认加载时的loading圈
    */
    defineLoadingPage = null;

    /**
    * 设置loading圈加载时的背景颜色
    */
    loadingPage.setBackgroundColor(Utils.getColor(mContext, backgroundColor));
    /**
    * 设置错误界面加载时的背景颜色
    */
    errorPage.setBackgroundColor(Utils.getColor(mContext, backgroundColor));
    /**
    * 设置空界面加载时的背景颜色
    */
    emptyPage.setBackgroundColor(Utils.getColor(mContext, backgroundColor));
    /**
    * 设置没有网络时界面加载时的背景颜色
    */
    networkPage.setBackgroundColor(Utils.getColor(mContext, backgroundColor));
    /**
    * 错误的文字
    */
    errorText = Utils.findViewById(errorPage, R.id.error_text);
    /**
    * 空的文字
    */
    emptyText = Utils.findViewById(emptyPage, R.id.empty_text);
    /**
    * 没有网络时的文字
    */
    networkText = Utils.findViewById(networkPage, R.id.no_network_text);
    /**
    * 错误界面的文字
    */
    errorImg = Utils.findViewById(errorPage, R.id.error_img);
    /**
    * 空界面的图片
    */
    emptyImg = Utils.findViewById(emptyPage, R.id.empty_img);
    /**
    * 没有网络界面的图片
    */
    networkImg = Utils.findViewById(networkPage, R.id.no_network_img);
    /**
    * 错误界面的 重新加载的按钮
    */
    errorReloadBtn = Utils.findViewById(errorPage, R.id.error_reload_btn);
    /**
    * 没有网界面的 重新加载的按钮
    */
    networkReloadBtn = Utils.findViewById(networkPage, R.id.no_network_reload_btn);

    /**
    * 错误界面重新加载 按钮的点击事件
    */
    errorReloadBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

    if (listener != null) {
    listener.onReload(v);
    }
    }
    });
    /**
    * 没有网界面重新加载 按钮的点击事件
    */
    networkReloadBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

    if (listener != null) {
    listener.onReload(v);
    }
    }
    });

    /**
    * 给view 进行赋值 (一般都是空、错误、无网等)
    */
    errorText.setText(errorStr);
    emptyText.setText(emptyStr);
    networkText.setText(netwrokStr);
    /**
    * 给View 设置大小
    */
    errorText.setTextSize(tipTextSize);
    emptyText.setTextSize(tipTextSize);
    networkText.setTextSize(tipTextSize);

    /**
    * 设置 veiw 颜色
    */
    errorText.setTextColor(Utils.getColor(mContext, tipTextColor));
    emptyText.setTextColor(Utils.getColor(mContext, tipTextColor));
    networkText.setTextColor(Utils.getColor(mContext, tipTextColor));

    /**
    * 设置View的图片资源
    */
    errorImg.setImageResource(errorImgId);
    emptyImg.setImageResource(emptyImgId);
    networkImg.setImageResource(networkImgId);

    /**
    * 重新加载按钮的背景颜色
    */
    errorReloadBtn.setBackgroundResource(reloadBtnId);
    networkReloadBtn.setBackgroundResource(reloadBtnId);

    /**
    * 重新加载(点击重试)设置字体
    */
    errorReloadBtn.setText(reloadBtnStr);
    networkReloadBtn.setText(reloadBtnStr);
    /**
    * 设置字体的大小
    */
    errorReloadBtn.setTextSize(buttonTextSize);
    networkReloadBtn.setTextSize(buttonTextSize);

    /**
    * 设置重新加载的颜色
    */
    errorReloadBtn.setTextColor(Utils.getColor(mContext, buttonTextColor));
    networkReloadBtn.setTextColor(Utils.getColor(mContext, buttonTextColor));


    if (buttonHeight != -1) {
    /**
    * dp-->px 给 ‘重新加载’ 设置高度
    */
    errorReloadBtn.setHeight(Utils.dp2px(mContext, buttonHeight));
    networkReloadBtn.setHeight(Utils.dp2px(mContext, buttonHeight));
    }
    if (buttonWidth != -1) {
    /**
    * dp-->px 给 ‘重新加载’ 设置宽度
    */
    errorReloadBtn.setWidth(Utils.dp2px(mContext, buttonWidth));
    networkReloadBtn.setWidth(Utils.dp2px(mContext, buttonWidth));
    }
    /**、
    * 添加View到 次布局中
    */
    this.addView(networkPage);
    this.addView(emptyPage);
    this.addView(errorPage);
    this.addView(loadingPage);
    }

    /**
    * 根据状态 设置显示哪个 View
    *
    * @param status
    */
    public void setStatus(@Flavour int status) {

    this.state = status;

    switch (status) {
    case Success:

    contentView.setVisibility(View.VISIBLE);
    emptyPage.setVisibility(View.GONE);
    errorPage.setVisibility(View.GONE);
    networkPage.setVisibility(View.GONE);
    if (defineLoadingPage != null) {
    defineLoadingPage.setVisibility(View.GONE);
    } else {
    loadingPage.setVisibility(View.GONE);
    }
    break;

    case Loading:

    contentView.setVisibility(View.GONE);
    emptyPage.setVisibility(View.GONE);
    errorPage.setVisibility(View.GONE);
    networkPage.setVisibility(View.GONE);
    if (defineLoadingPage != null) {
    defineLoadingPage.setVisibility(View.VISIBLE);
    } else {
    loadingPage.setVisibility(View.VISIBLE);
    }
    break;

    case Empty:

    contentView.setVisibility(View.GONE);
    emptyPage.setVisibility(View.VISIBLE);
    errorPage.setVisibility(View.GONE);
    networkPage.setVisibility(View.GONE);
    if (defineLoadingPage != null) {
    defineLoadingPage.setVisibility(View.GONE);
    } else {
    loadingPage.setVisibility(View.GONE);
    }
    break;

    case Error:

    contentView.setVisibility(View.GONE);
    loadingPage.setVisibility(View.GONE);
    emptyPage.setVisibility(View.GONE);
    errorPage.setVisibility(View.VISIBLE);
    networkPage.setVisibility(View.GONE);
    if (defineLoadingPage != null) {
    defineLoadingPage.setVisibility(View.GONE);
    } else {
    loadingPage.setVisibility(View.GONE);
    }
    break;

    case No_Network:

    contentView.setVisibility(View.GONE);
    loadingPage.setVisibility(View.GONE);
    emptyPage.setVisibility(View.GONE);
    errorPage.setVisibility(View.GONE);
    networkPage.setVisibility(View.VISIBLE);
    if (defineLoadingPage != null) {

    defineLoadingPage.setVisibility(View.GONE);
    } else {
    loadingPage.setVisibility(View.GONE);
    }
    break;

    default:
    break;
    }

    }


    /**
    * 返回当前状态{Success, Empty, Error, No_Network, Loading}
    *
    * @return
    */
    public int getStatus() {

    return state;
    }

    /**
    * 设置Empty状态提示文本,仅对当前所在的地方有效
    *
    * @param text
    * @return
    */
    public LoadingLayout setEmptyText(String text) {

    emptyText.setText(text);
    return this;
    }

    /**
    * 设置Error状态提示文本,仅对当前所在的地方有效
    *
    * @param text
    * @return
    */
    public LoadingLayout setErrorText(String text) {

    errorText.setText(text);
    return this;
    }

    /**
    * 设置No_Network状态提示文本,仅对当前所在的地方有效
    *
    * @param text
    * @return
    */
    public LoadingLayout setNoNetworkText(String text) {

    networkText.setText(text);
    return this;
    }

    /**
    * 设置Empty状态显示图片,仅对当前所在的地方有效
    *
    * @param id
    * @return
    */
    public LoadingLayout setEmptyImage(@DrawableRes int id) {


    emptyImg.setImageResource(id);
    return this;
    }

    /**
    * 设置Error状态显示图片,仅对当前所在的地方有效
    *
    * @param id
    * @return
    */
    public LoadingLayout setErrorImage(@DrawableRes int id) {

    errorImg.setImageResource(id);
    return this;
    }

    /**
    * 设置No_Network状态显示图片,仅对当前所在的地方有效
    *
    * @param id
    * @return
    */
    public LoadingLayout setNoNetworkImage(@DrawableRes int id) {

    networkImg.setImageResource(id);
    return this;
    }

    /**
    * 设置Empty状态提示文本的字体大小,仅对当前所在的地方有效
    *
    * @param sp
    * @return
    */
    public LoadingLayout setEmptyTextSize(int sp) {

    emptyText.setTextSize(sp);
    return this;
    }

    /**
    * 设置Error状态提示文本的字体大小,仅对当前所在的地方有效
    *
    * @param sp
    * @return
    */
    public LoadingLayout setErrorTextSize(int sp) {

    errorText.setTextSize(sp);
    return this;
    }

    /**
    * 设置No_Network状态提示文本的字体大小,仅对当前所在的地方有效
    *
    * @param sp
    * @return
    */
    public LoadingLayout setNoNetworkTextSize(int sp) {

    networkText.setTextSize(sp);
    return this;
    }

    /**
    * 设置Empty状态图片的显示与否,仅对当前所在的地方有效
    *
    * @param bool
    * @return
    */
    public LoadingLayout setEmptyImageVisible(boolean bool) {

    if (bool) {
    emptyImg.setVisibility(View.VISIBLE);
    } else {
    emptyImg.setVisibility(View.GONE);
    }
    return this;
    }

    /**
    * 设置Error状态图片的显示与否,仅对当前所在的地方有效
    *
    * @param bool
    * @return
    */
    public LoadingLayout setErrorImageVisible(boolean bool) {

    if (bool) {
    errorImg.setVisibility(View.VISIBLE);
    } else {
    errorImg.setVisibility(View.GONE);
    }
    return this;
    }

    /**
    * 设置No_Network状态图片的显示与否,仅对当前所在的地方有效
    *
    * @param bool
    * @return
    */
    public LoadingLayout setNoNetworkImageVisible(boolean bool) {

    if (bool) {
    networkImg.setVisibility(View.VISIBLE);
    } else {
    networkImg.setVisibility(View.GONE);
    }
    return this;
    }

    /**
    * 设置ReloadButton的文本,仅对当前所在的地方有效
    *
    * @param text
    * @return
    */
    public LoadingLayout setReloadButtonText(@NonNull String text) {

    errorReloadBtn.setText(text);
    networkReloadBtn.setText(text);
    return this;
    }

    /**
    * 设置ReloadButton的文本字体大小,仅对当前所在的地方有效
    *
    * @param sp
    * @return
    */
    public LoadingLayout setReloadButtonTextSize(int sp) {

    errorReloadBtn.setTextSize(sp);
    networkReloadBtn.setTextSize(sp);
    return this;
    }

    /**
    * 设置ReloadButton的文本颜色,仅对当前所在的地方有效
    *
    * @param id
    * @return
    */
    public LoadingLayout setReloadButtonTextColor(@ColorRes int id) {

    errorReloadBtn.setTextColor(Utils.getColor(mContext, id));
    networkReloadBtn.setTextSize(Utils.getColor(mContext, id));
    return this;
    }

    /**
    * 设置ReloadButton的背景,仅对当前所在的地方有效
    *
    * @param id
    * @return
    */
    public LoadingLayout setReloadButtonBackgroundResource(@DrawableRes int id) {

    errorReloadBtn.setBackgroundResource(id);
    networkReloadBtn.setBackgroundResource(id);
    return this;
    }

    /**
    * 设置ReloadButton的监听器
    *
    * @param listener
    * @return
    */
    public LoadingLayout setOnReloadListener(OnReloadListener listener) {

    this.listener = listener;
    return this;
    }

    /**
    * 自定义加载页面,仅对当前所在的Activity有效
    *
    * @param view
    * @return
    */
    public LoadingLayout setLoadingPage(View view) {

    defineLoadingPage = view;
    this.removeView(loadingPage);
    defineLoadingPage.setVisibility(View.GONE);
    this.addView(view);
    return this;
    }

    /**
    * 自定义加载页面,仅对当前所在的地方有效
    *
    * @param id
    * @return
    */
    public LoadingLayout setLoadingPage(@LayoutRes int id) {

    this.removeView(loadingPage);
    View view = LayoutInflater.from(mContext).inflate(id, null);
    defineLoadingPage = view;
    defineLoadingPage.setVisibility(View.GONE);
    this.addView(view);
    return this;
    }

    /**
    * 获取当前自定义的loadingpage
    *
    * @return
    */
    public View getLoadingPage() {

    return defineLoadingPage;
    }


    /**
    * 获取全局使用的loadingpage
    *
    * @return
    */
    public View getGlobalLoadingPage() {

    return loadingPage;
    }

    @IntDef({Success, Empty, Error, No_Network, Loading})
    public @interface Flavour {

    }

    public interface OnReloadListener {

    void onReload(View v);
    }

    /**
    * 获取全局配置的class
    *
    * @return
    */
    public static Config getConfig() {

    return mConfig;
    }

    /**
    * 全局配置的Class,对所有使用到的地方有效
    */
    public static class Config {

    public Config setErrorText(@NonNull String text) {

    errorStr = text;
    return mConfig;
    }

    public Config setEmptyText(@NonNull String text) {

    emptyStr = text;
    return mConfig;
    }

    public Config setNoNetworkText(@NonNull String text) {

    netwrokStr = text;
    return mConfig;
    }

    public Config setReloadButtonText(@NonNull String text) {

    reloadBtnStr = text;
    return mConfig;
    }

    /**
    * 设置所有提示文本的字体大小
    *
    * @param sp
    * @return
    */
    public Config setAllTipTextSize(int sp) {

    tipTextSize = sp;
    return mConfig;
    }

    /**
    * 设置所有提示文本的字体颜色
    *
    * @param color
    * @return
    */
    public Config setAllTipTextColor(@ColorRes int color) {

    tipTextColor = color;
    return mConfig;
    }

    public Config setReloadButtonTextSize(int sp) {

    buttonTextSize = sp;
    return mConfig;
    }

    public Config setReloadButtonTextColor(@ColorRes int color) {

    buttonTextColor = color;
    return mConfig;
    }

    public Config setReloadButtonBackgroundResource(@DrawableRes int id) {

    reloadBtnId = id;
    return mConfig;
    }

    public Config setReloadButtonWidthAndHeight(int width_dp, int height_dp) {

    buttonWidth = width_dp;
    buttonHeight = height_dp;
    return mConfig;
    }

    public Config setErrorImage(@DrawableRes int id) {

    errorImgId = id;
    return mConfig;
    }

    public Config setEmptyImage(@DrawableRes int id) {

    emptyImgId = id;
    return this;
    }

    public Config setNoNetworkImage(@DrawableRes int id) {

    networkImgId = id;
    return mConfig;
    }

    public Config setLoadingPageLayout(@LayoutRes int id) {

    loadingLayoutId = id;
    return mConfig;
    }

    public Config setAllPageBackgroundColor(@ColorRes int color) {

    backgroundColor = color;
    return mConfig;
    }
    }
    }

    可以设置全局 也可以设置 也可以单独设置 (在activity中设置 )



    本地文件://e/androidDownLoad/android_loadinglayout

    url--->

    https://gold.xitu.io/post/583c242061ff4b006b59c7fb
  • 相关阅读:
    C++中static_cast和dynamic_cast强制类型转换
    Oozie-
    Spring Boot- 用idea新建spring boot web项目
    Spring Boot- 设置拦截打印日志
    Storm- 使用Storm实现词频汇总
    Storm- 使用Storm实现累积求和的操作
    HTTP- 头部信息
    Python- 文件读写
    JQuery- JQuery学习
    Javascript- Javascript学习
  • 原文地址:https://www.cnblogs.com/jeno-song/p/6211508.html
Copyright © 2020-2023  润新知