Android 自定义通用的loadingview
介绍
好久没有写博客啦,最近在接近新年了,年前的工作都要收尾,所以特别忙,周末抽空写了个通用的加载view,写篇博客分享出来。
功能
1、显示加载视图,加载失败的时候显示加载失败视图,数据为空时显示数据为空视图,支持为失败视图设置点击事件重新加载数据。
2、支持个性化设置,自定义设置 加载、失败、空数据视图。
先放一张效果图压压惊
实现
实现思路其实就是一个FrameLayout里添加三个布局做处理显示隐藏,自定义视图其实就是替换里面的view ,代码比较简单,如果直接看过我的自定义view系列文章,或者对自定义view有所了解,都很容易看懂,所有直接上代码了。
具体代码
java 代码
public class CommonLoadingView extends FrameLayout {
//加载时显示文字
protected TextView mLoadingTextTv;
public Context mContext;
//加载错误视图
protected LinearLayout mLoadErrorLl;
//加载错误点击事件处理
private LoadingHandler mLoadingHandler;
//加载view
private View loadingView;
//加载失败view
private View loadingErrorView;
//数据为空
private View emptyView;
public CommonLoadingView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CommonLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
public void setLoadingHandler(LoadingHandler loadingHandler) {
mLoadingHandler = loadingHandler;
}
public void setLoadingErrorView(View loadingErrorView) {
this.removeViewAt(1);
this.loadingErrorView = loadingErrorView;
this.loadingErrorView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mLoadingHandler != null) {
mLoadingHandler.doRequestData();
CommonLoadingView.this.load();
}
}
});
this.addView(loadingErrorView,1);
}
public void setLoadingView(View loadingView) {
this.removeViewAt(0);
this.loadingView = loadingView;
this.addView(loadingView,0);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
loadingView = inflate(mContext, R.layout.common_loading_view, null);
loadingErrorView = inflate(mContext, R.layout.network_layout, null);
emptyView = inflate(mContext, R.layout.empty_layout, null);
this.addView(loadingView);
this.addView(loadingErrorView);
this.addView(emptyView);
loadingErrorView.setVisibility(GONE);
emptyView.setVisibility(GONE);
initView(this);
}
public void setMessage(String message) {
mLoadingTextTv.setText(message);
}
private void initView(View rootView) {
mLoadingTextTv = (TextView) rootView.findViewById(R.id.loading_text_tv);
mLoadErrorLl = (LinearLayout) rootView.findViewById(R.id.load_error_ll);
mLoadErrorLl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mLoadingHandler != null) {
CommonLoadingView.this.load();
mLoadingHandler.doRequestData();
}
}
});
}
public void load(){
loadingView.setVisibility(VISIBLE);
loadingErrorView.setVisibility(GONE);
emptyView.setVisibility(GONE);
}
public void load(String message){
mLoadingTextTv.setText(message);
loadingView.setVisibility(VISIBLE);
loadingErrorView.setVisibility(GONE);
emptyView.setVisibility(GONE);
}
public void loadSuccess(){
this.loadSuccess(false);
}
public void loadSuccess(boolean isEmpty){
loadingView.setVisibility(GONE);
loadingErrorView.setVisibility(GONE);
if (isEmpty) {
emptyView.setVisibility(VISIBLE);
}else{
emptyView.setVisibility(GONE);
}
}
public void loadError(){
loadingView.setVisibility(GONE);
loadingErrorView.setVisibility(VISIBLE);
}
public interface LoadingHandler{
void doRequestData();
}
}
使用
基本使用
几个基本的 load loadError loadSucccess方法的使用。
public class DefaultViewActivity extends AppCompatActivity {
protected ListView mListView;
protected CommonLoadingView mLoadingView;
private List<String> mList = new ArrayList<>();
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_default_view);
initView();
}
private void initView() {
mListView = (ListView) findViewById(R.id.listView);
mLoadingView = (CommonLoadingView) findViewById(R.id.loadingView);
mLoadingView.load();
//设置点击错误视图重新加载事件
mLoadingView.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
@Override
public void doRequestData() {
mLoadingView.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=20 ; i++) {
mList.add(i+"");
}
adapter = new ArrayAdapter(DefaultViewActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, mList);
mListView.setAdapter(adapter);
mLoadingView.loadSuccess(false);
}
},2500);
}
});
//模拟网络错误,加载失败
mLoadingView.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingView.loadError();
}
},2500);
}
}
自定义视图 使用
只需要把自己自定义的view调用set方法设置进去即可。
this.mLoadingView.setLoadingView(loadingView);
this.mLoadingView.setLoadingErrorView(loadingErrorView);
public class CustomViewActivity extends AppCompatActivity {
protected ListView mListView;
protected CommonLoadingView mLoadingView;
private List<String> mList = new ArrayList<>();
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_default_view);
initView();
}
private void initView() {
mListView = (ListView) findViewById(R.id.listView);
mLoadingView = (CommonLoadingView) findViewById(R.id.loadingView);
//设置自定义视图
ProgressBar progressBar = new ProgressBar(this);
this.mLoadingView.setLoadingView(progressBar);
TextView textView = new TextView(this);
textView.setText("加载失败...");
this.mLoadingView.setLoadingErrorView(textView);
mLoadingView.load();
//设置点击错误视图重新加载事件
mLoadingView.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
@Override
public void doRequestData() {
mLoadingView.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=20 ; i++) {
mList.add(i+"");
}
adapter = new ArrayAdapter(CustomViewActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, mList);
mListView.setAdapter(adapter);
mLoadingView.loadSuccess(false);
}
},2500);
}
});
//模拟网络错误,加载失败
mLoadingView.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingView.loadError();
}
},2500);
}
}
至于具体的布局和样式文件就不贴了,主要是实现思路,代码
下载请参考源码下载,记得点赞哟!
Android 自定义通用的loadingview的更多相关文章
- 2.Android 自定义通用的Item布局
转载:http://www.jianshu.com/p/e7ba4884dcdd BaseItemLayout 简介 在工作中经常会遇到下面的一些布局,如图标红处: 05.png 07.png 08. ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
- Android自定义View(LimitScrollerView-仿天猫广告栏上下滚动效果)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53303872 本文出自:[openXu的博客] 1分析 2定义组合控件布局 3继承最外层控件 ...
- Android自定义View(三、深入解析控件测量onMeasure)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51490283 本文出自:[openXu的博客] 目录: onMeasure什么时候会被调用 ...
- 简单说说Android自定义view学习推荐的方式
这几天比较受关注,挺开心的,嘿嘿. 这里给大家总结一下学习自定义view的一些技巧. 以后写自定义view可能不会写博客了,但是可以开源的我会把源码丢到github上我的地址:https://git ...
- Android自定义视图四:定制onMeasure强制显示为方形
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Android自定义视图一:扩展现有的视图,添加新的XML属性
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- 自定义通用dialogFragment
代码地址如下:http://www.demodashi.com/demo/12844.html 前言 之前写过一篇dialogFragmnet封装默认dialog的文章 DialogFragment创 ...
- android 自定义动画
android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...
随机推荐
- ruby, gem install 出现网络错误
gem sources #查看目前的源 gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.or ...
- hbase 第一篇
参考:http://www.jdon.com/38244 http://chuanwang66.iteye.com/blog/1683533
- Ubuntu下安装PDF 文档阅读器Adobe Reader 9.5.5
由于没有PPA所以我们必须在Adobe的官方FTP上下载安装,下面的方法同时适用于32位和64位系统: wget ftp://ftp.adobe.com/pub/adobe/reader/unix/9 ...
- 公司搬家,拿了个费机器,没root密码,又忘了怎么搞了,
grub中找到ro->rw single init=/bin/bash passwd root
- jemalloc Mongodb Nginx 优化
下载 http://www.canonware.com/jemalloc/download.html 下载 wget http://www.canonware.com/download/jemallo ...
- (中等) HDU 4069 Squiggly Sudoku , DLX+精确覆盖。
Description Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that ...
- 基础数据结构之(Binary Trees)
从头开始刷ACM,真的发现过去的很多漏洞,特别越是基础的数据结构,越应该学习得精,无论是ACM竞赛,研究生考试,还是工程上,对这些基础数据结构的应用都非常多,深刻理解非常必要.不得不说最近感触还是比较 ...
- php+socket模拟表单发送请求
<?php /** * http请求类(php + socket) * @todo 这里还有很多未完善的地方,仅有简单的get post head请求 * @author chuangrain@ ...
- Servlet中的过滤器Filter用法
1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...
- VS2010环境下用ANSI C创建DLL和使用方法(转)
源:VS2010环境下用ANSI C创建DLL和使用方法 . 创建DLL工程 1.2 创建一个dll工程. 操作:a.文件->新建->项目->Win32控制台应用程序. b.输入工程 ...