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 ...
随机推荐
- 找礼物(find)
找礼物(find) 题目描述 新年到了,你的好友和你(共K个人)的周围满是礼物,你让你的好友先拿,但是每个人只能拿当前离自己最近的礼物[当然如果有并列的多个礼物离你的距离相等(精确到小数点后四位,所有 ...
- 采用多线程方式,解决由于查询等待造成winfrom假死问题
1.这里是触发一个比较耗时的操作,比如一次大数据量的查询: Thread thread = new Thread(new ThreadStart(DoWord)); thread.Start(); 2 ...
- 未在本地计算机上注册“Microsoft.Ace.OleDB.12.0”
这是异常 我的电脑室x86的所以选择x86.
- word采用尾注进行参考文献排版的一些问题
使用Word中尾注的功能可以很好地解决论文中参考文献的排序问题.方法如下: 1.光标移到要插入参考文献的地方,菜单中“插入”——“引用”——“脚注和尾注”. 2.对话框中选择“尾注”,编号方式选“自动 ...
- HDU 1811 Rank of Tetris 拓补排序+并查集
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [ ...
- git使用系列(一)
git commit 的时候出现了问题: change not staged for commit. no changes added to commit(use "git add" ...
- OC与Swift创建pod
Cocoa pods 是iOS最常用的类库管理工具 OC的使用 删除源 sudo gem sources -r https://rubygems.org/ 添加源(使用淘宝的镜像,记住要用 ...
- ArcEngine部分工作总结
Arcengine工作总结地物点查询本部分可以在一个窗体中实现,也可以在两个窗体中实现.由于工作要求本人是在两个窗体中实现的:弹出窗体的名称为FormQuery主窗体单机查询时间的代码FormQuer ...
- VSC#2010打开视图编辑器假死/卡死
最近写项目代码的时候写C#,VSC#2010刚配置好,打开之前同学写的项目的设计界面结果...我擦,卡死了,重复几次都是这样. 于是上网搜发现是VS一个bug,打一个VSSP1补丁就好了~ http: ...
- 浅谈一个网页打开的全过程(涉及DNS、CDN、Nginx负载均衡等)
1.概要 从用户在浏览器输入域名开始,到web页面加载完毕,这是一个说复杂不复杂,说简单不简单的过程,下文暂且把这个过程称作网页加载过程.下面我将依靠自己的经验,总结一下整个过程.如有错漏,欢迎指正. ...