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. IOS 微信 6.5.2 自动播放音乐 解决方案

    之前仅仅是IPhone7\7p 的问题,现在已经扩展到6 .6s.今天在下也行了最新微信,音乐问题果然来了. 好了 下面直接进入正题 首先 引入 <script src="http:/ ...

  2. 参考__HTML5 Game

    基础知识 TypeScript TypeScript 目录教程 Egret Egret II HTML5晃动DeviceMotionEvent事件 博客 html5tricks 库和框架 html5t ...

  3. 获取openid 的步骤

    1.引导客户打开 https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid &redirect_uri=https://w ...

  4. CentOS 配置防火墙操作实例(启、停、开、闭端口):

    CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service   iptables status< ...

  5. String根据、拆分

    String cla="信1305.信1304.信1204.信1103.信1403"; while(cla.contains(".")){ int a=cla. ...

  6. centos中安装字体

    转载自:http://blog.csdn.net/wlwlwlwl015/article/details/51482065 在使用phantomjs做自动化网页截图时,发现截图都没有文字.最后好久才发 ...

  7. eclipse: The superclass "javax.servlet.http.HttpServlet" was not found 解决方案

    解决步骤: 1. 安装Tomcat8.5 Server 2. eclipse 新建Tomcat 8.5 Server 3. 配置build path 添加  Server Runtime 1.右键项目 ...

  8. MySQL DML 整理

    DML(Data Manipulation Language)数据操纵语言statements are used for managing data within schema objects. 由D ...

  9. 7 -- Spring的基本用法 -- 5...

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  10. 联合体union和大小端(big-endian、little-endian)

    1.联合体union的基本特性——和struct的同与不同 union,中文名“联合体.共用体”,在某种程度上类似结构体struct的一种数据结构,共用体(union)和结构体(struct)同样可以 ...