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. Javascript中的链表

    function LinkedList() { // 辅助类,表示加入链表的每一项 var Node=function(element){ this.element=element; this.nex ...

  2. Linux中设置服务自启动的三种方式

    有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s                       在/etc/rc.d/rc*.d目录中建立/e ...

  3. vc6.0如何显示行号以及出现版本不兼容问题

    有时编译时,提示某某行有错,但是要定位到某一行的话,如果在编辑页面能够将行号显示出来,查找也就更方便了,下面我来介绍一下让VC6.0显示行号的方法.   工具/原料   VC6.0.显示行号的插件 方 ...

  4. asp 使用TreeView控件

    这段代码为了使用 TreeNodeCheckChanged 事件,会有回刷新的效果: 不喜欢的可查看改进版,利用js控制选择操作,无界面刷新, “http://www.cnblogs.com/GoCi ...

  5. [官方作品] 关于ES4的设首页问题

    [官方作品] 关于ES4的设首页问题 Skyfree 发表于 2013-2-10 21:55:03 https://www.itsk.com/thread-254503-1-1.html 关于ES4设 ...

  6. http响应需要记住的状态码

    200:请求成功. 301:被请求的资源已永久移动到新位置.302:请求的资源现在临时从不同的 URI 响应请求.401:当前请求需要用户验证.403:服务器已经理解请求,但是拒绝执行它. 404:请 ...

  7. 用margin还是padding

    用margin还是用padding这个问题是每个学习CSS进阶时的必经之路. CSS边距属性定义元素周围的空间.通过使用单独的属性,可以对上.右.下.左的外边距进行设置.也可以使用简写的外边距属性同时 ...

  8. Git代码管理常用命令

    1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git查看远程仓库:$ git remote -v添加远程仓库:$ git re ...

  9. u盘文件恢复

    同事的一个u盘,在别的机器上用过之后,插到自己的机器上,被360报警有木马,处理完后,一些文件和文件夹不见了. 拿到我的机器上,360弹出框问要不要处理,列表里显示有几个文件夹被隐藏起来了,选择显示后 ...

  10. C#如果把A.new()编译成new A()

    缘由 对于初次接触某个第三方库的C#开发者,假如要调用里面一个方法,发现需要一个A类型的实例作为参数,怎么获得这个实例呢? 我想大多数人会先尝试new A吧: 如果没有,可能会尝试输入A.看看有没可能 ...