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
android 加载中、无网络、无数据、出错 四种状态的代码封装的更多相关文章
- 漂亮的Android加载中动画:AVLoadingIndicatorView
AVLoadingIndicatorView 包含一组漂亮的Android加载中动画. IOS版本:here. 示例 Download Apk 用法 步骤1 Add dependencies in b ...
- AsyncTask异步加载和HttpURLConnection网络请求数据
//获得网络数据 private void huodeshuju() { //这里是使用线程,已注释掉 /*new Thread(){ public void ...
- css3动画-加载中...
写几个简单的加载中动画吧. 像前面三种都是相当于几个不同的点轮流来播放同一动画:变大变小.css3里面有一个用于尺度变换的方法:scale(x,y):定义 2D 缩放转换,改变元素的宽度和高度. 第四 ...
- React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar
1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...
- JS脚本文件的位置对页面加载性能影响以及无阻塞脚本(javascript)模式
JS的阻塞特性:当<script>出现的时候,页面必须等待脚本文件的加载.解析.执行完毕后才能继续进行页面的渲染.不管脚本文件是以内联形式还是外部引入的形式出现在<script> ...
- 实现正在加载中界面的Android库:DynamicBox
转载. DynamicBox是一个Android库,能够inflates自定义布局来指示出: 正在加载内容 显示一个异常 或者是一个自定义视图 项目主页:http://www.open-ope ...
- EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载
之前的EF Code First系列讲了那么多如何配置实体和数据库表的关系,显然配置只是辅助,使用EF操作数据库才是每天开发中都需要用的,这个系列讲讲如何使用EF操作数据库.老版本的EF主要是通过Ob ...
- JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)
在很多项目中都会涉及到数据加载.数据加载有时可能会是2-3秒,为了给一个友好的提示,一般都会给一个[数据加载中...]的提示.今天就做了一个这样的提示框. 先去jQuery官网看看怎么写jQuery插 ...
- jquery mobile 请求数据方法执行时显示加载中提示框
在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...
随机推荐
- 谈谈Memcached与Redis
1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...
- python走起之第十四话
HTML 标题 HTML 标题(Heading)是通过<h1> - <h6> 标签来定义的. 实例 <h1>这是一个标题</h1> <h2> ...
- Maven打包程序
1.src/main目录下建立scritps.config.assembly目录 scritps:存放脚本文件如批处理 assembly:打包配置文件 2.assembly目录下建立package ...
- JS存取Cookie值
一:存Cookie //存Cookie document.cookie = "id=" + escape(value); 二:取Cookie //提取Cookie值 functio ...
- startActivity跳转失败而且没有异常信息
startActivity跳转不能显示目标activity的布局(显示空白页),而且没有异常信息 onCreate()方法重写错误 应该重写的是onCreate(Bundle savedInstanc ...
- native vlan(本征VLAN)
其实就是不打tag的VLAN,因为你想,一个VLAN在经过交换设备老是打tag,然后再脱掉tag...这个很浪费计算资源,尤其是在转发的报文量相当大的时候. 如何解决: 可以定义一种vlan, ...
- cookie封装
//设置cookie function setCookie(name,value,days){ //如果不设置天数 , 默认为30天 days=days?days:30; va ...
- AX2012 XppCompiler create method动态创建方法并运行
在用友系列的软件中经常可以看到可配置的计算公式,AX中其实也有可配置的公式,如call center呼叫中心的欺诈Fraud rule的配置,AX后台可以根据配置规则,变量,条件来动态产生方法并执行, ...
- web api+递归树型结构
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- css样式大全
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...