Android通用LoadingView加载框架详解
作者:qq_14876133 发布时间:2021-12-01 10:43:24
标签:Android,LoadingView,加载
手写一个通用加载中、显示数据、加载失败、空数据的LoadingView框架。
定义3个布局:加载中,加载失败,空数据
加载中:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true" />
</FrameLayout>
加载失败:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="@+id/error_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/loading_retry" />
</FrameLayout>
空数据:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/loading_empty" />
</FrameLayout>
自定义一个LoadingView:
package com.sample.loadingview.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.sample.loadingview.R;
public class LoadingView extends FrameLayout {
private int emptyLayoutId, errorLayoutId, loadingLayoutId;
private View contentView, emptyView, errorView, loadingView;
private LayoutInflater mInflater;
private SparseArray<View> views = new SparseArray<>();
public LoadingView(@NonNull Context context) {
this(context, null);
}
public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadLayout);
emptyLayoutId = a.getResourceId(R.styleable.LoadLayout_emptyView, R.layout.loading_empty);
errorLayoutId = a.getResourceId(R.styleable.LoadLayout_errorView, R.layout.loading_error);
loadingLayoutId = a.getResourceId(R.styleable.LoadLayout_loadingView, R.layout.loading_load);
mInflater = LayoutInflater.from(getContext());
a.recycle();
loadingView = mInflater.inflate(loadingLayoutId, null);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() < 1) {
throw new RuntimeException("content view can not be null");
}
contentView = getChildAt(0);
if (loadingView.getVisibility() != GONE)
loadingView.setVisibility(GONE);
addView(loadingView);
views.put(loadingLayoutId, loadingView);
}
public void showError() {
errorView = views.get(errorLayoutId);
if (errorView == null) {
errorView = mInflater.inflate(errorLayoutId, null);
addView(errorView);
views.put(errorLayoutId, errorView);
final ImageView errorRetry = (ImageView) errorView.findViewById(R.id.error_retry);
errorRetry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnRetryClickListener != null)
mOnRetryClickListener.onClick(errorRetry);
}
});
}
if (errorView.getVisibility() != VISIBLE)
errorView.setVisibility(VISIBLE);
for (int i = 0, len = views.size(); i < len; i++) {
int key = views.keyAt(i);
if (key != errorLayoutId) {
View view = views.valueAt(i);
if (view != null)
if (view.getVisibility() != GONE)
view.setVisibility(GONE);
}
}
}
public void showEmpty() {
emptyView = views.get(emptyLayoutId);
if (emptyView == null) {
emptyView = mInflater.inflate(emptyLayoutId, null);
addView(emptyView);
views.put(emptyLayoutId, emptyView);
}
if (emptyView.getVisibility() != VISIBLE)
emptyView.setVisibility(VISIBLE);
for (int i = 0, len = views.size(); i < len; i++) {
int key = views.keyAt(i);
if (key != emptyLayoutId) {
View view = views.valueAt(i);
if (view != null)
if (view.getVisibility() != GONE)
view.setVisibility(GONE);
}
}
}
public void showLoading() {
loadingView = views.get(loadingLayoutId);
if (loadingView.getVisibility() != VISIBLE)
loadingView.setVisibility(VISIBLE);
for (int i = 0, len = views.size(); i < len; i++) {
int key = views.keyAt(i);
if (key != loadingLayoutId) {
View view = views.valueAt(i);
if (view != null)
if (view.getVisibility() != GONE)
view.setVisibility(GONE);
}
}
}
public void showContent() {
for (int i = 0, len = views.size(); i < len; i++) {
View view = views.valueAt(i);
if (view != null)
if (view.getVisibility() != GONE)
view.setVisibility(GONE);
}
}
private OnClickListener mOnRetryClickListener;
public void setOnRetryClickListener(OnClickListener onRetryClickListener) {
this.mOnRetryClickListener = onRetryClickListener;
}
}
定义attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="LoadLayout">
<attr name="loadingView" format="reference" />
<attr name="errorView" format="reference" />
<attr name="retryView" format="reference" />
<attr name="emptyView" format="reference" />
</declare-styleable>
</resources>
以上就这么些代码,接下来我们测试一下
Activity
package com.sample.loadingview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.sample.loadingview.widget.LoadingView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private LoadingView loading_view;
private Button btn_loading;
private Button btn_content;
private Button btn_error;
private Button btn_empty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading_view = (LoadingView) findViewById(R.id.loading_view);
btn_loading = (Button) findViewById(R.id.btn_loading);
btn_content = (Button) findViewById(R.id.btn_content);
btn_error = (Button) findViewById(R.id.btn_error);
btn_empty = (Button) findViewById(R.id.btn_empty);
//设置加载错误的点击事件
loading_view.setOnRetryClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "重新加载", Toast.LENGTH_SHORT).show();
loading_view.showLoading();
}
});
btn_loading.setOnClickListener(this);
btn_content.setOnClickListener(this);
btn_error.setOnClickListener(this);
btn_empty.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_loading:
loading_view.showLoading();//显示加载界面
break;
case R.id.btn_content:
loading_view.showContent();//显示内容界面
break;
case R.id.btn_error:
loading_view.showError();//显示错误界面
break;
case R.id.btn_empty:
loading_view.showEmpty();//显示空数据界面
break;
}
}
}
源码:下载地址
来源:https://blog.csdn.net/qq_14876133/article/details/80924356


猜你喜欢
- 介绍  日常的业务开发,我们会重复编写一些代码:日期和字符串相互转换、发送Http请求调用接口、拷贝对象
- Java的Arrays类中有一个sort()方法,该方法是Arrays类的静态方法,在需要对数组进行排序时,非常的好用。但是sort()的参
- 一、网站微信扫码支付开发并没有现成的java示例,总结一下自己微信扫码支付心得二、首先去微信公众平台申请账户 https://mp.weix
- 一、泛型的概念1.1 基础案例泛型在Java中的应用非常广泛,最常见则是在集合容器中,先看下基础用法:public class Generi
- 本文是在我的文章android图片处理,让图片变成圆形 的基础上继续写的,可以去看看,直接看也没关系,也能看懂 1、首先在res文
- 前言想必大家对c语言的动态内存分配并不陌生,忘了的小伙伴也可以看看我的这篇文章C语言动态内存分配c语言的动态内存分配由于有些地方用起来比较麻
- 先看看效果图:实现思路:擦除图片相应的角,然后层叠图片,产生倾斜效果代码实现:1、定义属性在values文件夹下的attrs文件添加以下代码
- 一、概述我们对于这个图片肯定会非常熟悉,这两幅图片我们都可以看做是一个文件结构,对于这样的结构我们称之为树形结构。在数据结构中我们了解到可以
- 深入理解IOC思想spring本质就在于将对象全部交由给spring容器创建和管理,由容器控制对象的整个生命周期、核心就是IOC控制反转和A
- merge结合include优化android布局,效果不知道,个人感觉使用上也有很大的局限,不过还是了解一下,记录下来。布局文件都要有根节
- 一. String对象的比较1. ==比较是否引用同一个对象注意:对于内置类型,==比较的是变量中的值;对于引用类型 , == 比较的是引用
- VC程序设计中屏幕上的文字大都是由gdi32.dll的以下几个函数显示的:TextOutA、TextOutW、ExtTextOutA、Ext
- 本文实例讲述了java Swing组件setBounds()简单用法。分享给大家供大家参考,具体如下:先看API:public void s
- 本文参考文档Add Flutter to existing apps。首先有一个可以运行的原生项目第一步:新建Flutter moduleT
- 一、事务隔离级别①介绍数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题。一个事 务与其他事务隔离的程度称为
- 详解JDK中ExecutorService与Callable和Future对线程的支持1、代码背景: 假
- 本文实例讲述的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDia
- 本文实例分析了Android中ListActivity用法。分享给大家供大家参考,具体如下:程序如下:import android.app.
- 相关api见:点击进入/* * Copyright 2014 the original author or authors. * * Lic
- 场景在任何一个Form表单的操作页面或者数据台账的查询页面,基本都会看到一个清除的按钮,其功能就是用来清除我们需要抛弃的已经写入到控件内的数