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

android 加载中、无网络、无数据、出错 四种状态的代码封装的更多相关文章

  1. 漂亮的Android加载中动画:AVLoadingIndicatorView

    AVLoadingIndicatorView 包含一组漂亮的Android加载中动画. IOS版本:here. 示例 Download Apk 用法 步骤1 Add dependencies in b ...

  2. AsyncTask异步加载和HttpURLConnection网络请求数据

    //获得网络数据    private void huodeshuju() { //这里是使用线程,已注释掉        /*new Thread(){            public void ...

  3. css3动画-加载中...

    写几个简单的加载中动画吧. 像前面三种都是相当于几个不同的点轮流来播放同一动画:变大变小.css3里面有一个用于尺度变换的方法:scale(x,y):定义 2D 缩放转换,改变元素的宽度和高度. 第四 ...

  4. React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar

    1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...

  5. JS脚本文件的位置对页面加载性能影响以及无阻塞脚本(javascript)模式

    JS的阻塞特性:当<script>出现的时候,页面必须等待脚本文件的加载.解析.执行完毕后才能继续进行页面的渲染.不管脚本文件是以内联形式还是外部引入的形式出现在<script> ...

  6. 实现正在加载中界面的Android库:DynamicBox

    转载. DynamicBox是一个Android库,能够inflates自定义布局来指示出: 正在加载内容 显示一个异常 或者是一个自定义视图     项目主页:http://www.open-ope ...

  7. EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载

    之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...

  8. JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)

    在很多项目中都会涉及到数据加载.数据加载有时可能会是2-3秒,为了给一个友好的提示,一般都会给一个[数据加载中...]的提示.今天就做了一个这样的提示框. 先去jQuery官网看看怎么写jQuery插 ...

  9. jquery mobile 请求数据方法执行时显示加载中提示框

    在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...

随机推荐

  1. angular中ng-repeat ng-if 中的变量的值控制器中为什么取不到

    这个问题的本质是:v-repeat会产生子scope,这时你在控制器里拿值,相当于父scope里面取子scope的值,因为Angular.js中作用域是向上查找的,所以取不到. 操作过程如下: 相关代 ...

  2. js中call、apply、bind的用法

    原文链接:http://www.cnblogs.com/xljzlw/p/3775162.html var zlw = { name: "zlw", sayHello: funct ...

  3. 【java基础】面向对象的三大基本特征之-------继承

    面向对象的三大特征:封装,继承,多态 java通过extends关键字来实现继承,而且是单继承,一个子类只可以有一个直接父类,但是父类还可以有父类... java.long.Object是所有类的父类 ...

  4. OnTimer

    OnTimer不是多线程. OnTimer是以SendMessage的方式发送消息到消息队列. sendMessage必须等待对话框响应完消息后才返回.

  5. mac环境brew安装freetype,imagick等yii2所需要的库

    之前整理了一下内置的php环境,各种缺库是很坑爹的,而且内置的php编译目录找了老半天没找到.所以决定使用brew去重新编译一边php brew的安装就不说了,上篇博客有说.直入主题 brew安装完p ...

  6. 禁止手机页面中A标签长按弹出路径框

    //禁止手机页面中A标签长按弹出路径框    window.onload=function(){        document.documentElement.style.webkitTouchCa ...

  7. 项目公共js(vue.js)

    var urlHead = "http://hm.runorout.com/";// var urlHead = "/";/*加入跑班相关*/var urlGe ...

  8. 使用 Box2D 做一个 JansenWalker 机器人

    在 Box2DFlash 的官网的首页有一个小 Demo,这个 Demo 中有11个例子,可以通过左右方向键查看不同的例子,里面的每个例子都非常有趣,但最让我感兴趣的,是其中一个叫 JansenWal ...

  9. 20169212《Linux内核原理与分析》第六周作业

    视频学习 一.用户态.内核态和中断 内核态:处于高的执行级别下,代码可以执行特权指令,访问任意的物理地址,这时的CPU就对应内核态 用户态:处于低的执行级别下,代码只能在级别允许的特定范围内活动.在日 ...

  10. hexo 本地local4000打不开解决方法

    错误:Cannot GET /spadesq.github.io/ (注:spadesq.github.io是原来放hexo文件夹的名字) 由于我后来把hexo文件夹搬迁到别处,但我发现打开本地,地址 ...