Android通用LoadingView加載框架詳解
手寫一個(gè)通用加載中、顯示數(shù)據(jù)、加載失敗、空數(shù)據(jù)的LoadingView框架。

定義3個(gè)布局:加載中,加載失敗,空數(shù)據(jù)
加載中:
<?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>
空數(shù)據(jù):
<?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>
自定義一個(gè)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>
以上就這么些代碼,接下來我們測(cè)試一下
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);
//設(shè)置加載錯(cuò)誤的點(diǎn)擊事件
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();//顯示內(nèi)容界面
break;
case R.id.btn_error:
loading_view.showError();//顯示錯(cuò)誤界面
break;
case R.id.btn_empty:
loading_view.showEmpty();//顯示空數(shù)據(jù)界面
break;
}
}
}
源碼:下載地址
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)仿iOS菊花加載圈動(dòng)畫效果
- Android繪制圓形百分比加載圈效果
- Android自定義加載圈動(dòng)畫效果
- android動(dòng)態(tài)加載布局文件示例
- Android使用控件ImageView加載圖片的方法
- Android使用glide加載gif動(dòng)畫設(shè)置播放次數(shù)
- Android實(shí)現(xiàn)跳動(dòng)的小球加載動(dòng)畫效果
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
- Android實(shí)現(xiàn)加載圈
相關(guān)文章
Android中分析Jetpack?Compose動(dòng)畫內(nèi)部的實(shí)現(xiàn)原理
這篇文章主要介紹了Android中分析Jetpack?Compose動(dòng)畫內(nèi)部的實(shí)現(xiàn)原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09
Android中程序的停止?fàn)顟B(tài)詳細(xì)介紹
這篇文章主要介紹了Android中程序的停止?fàn)顟B(tài)詳細(xì)介紹,本文講解了什么是程序的停止?fàn)顟B(tài)、為什么Android要引入這一狀態(tài)、激活狀態(tài)和停止?fàn)顟B(tài)的切換、如何變?yōu)橥V範(fàn)顟B(tài)等內(nèi)容,需要的朋友可以參考下2015-01-01
Android實(shí)現(xiàn)底部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部導(dǎo)航欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式
這篇文章主要介紹了Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式,需要的朋友可以參考下2020-03-03
React Native中Android物理back鍵按兩次返回鍵即退出應(yīng)用
這篇文章主要給大家介紹了關(guān)于React Native中Android物理back鍵按兩次返回鍵即退出應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法
這篇文章主要介紹了Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法,以實(shí)例形式較為詳細(xì)的分析了TextView設(shè)置及xml布局與調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android用MVP實(shí)現(xiàn)一個(gè)簡單的類淘寶訂單頁面的示例
本篇文章主要介紹了Android用MVP實(shí)現(xiàn)一個(gè)簡單的類淘寶訂單頁面的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
一個(gè)簡單的toolabar結(jié)合drawlayout使用方法
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡單的toolabar結(jié)合drawlayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

