介绍
好久没有写博客啦,最近在接近新年了,年前的工作都要收尾,所以特别忙,周末抽空写了个通用的加载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