Android 自定義通用的loadingview實現(xiàn)代碼
功能
1、顯示加載視圖,加載失敗的時候顯示加載失敗視圖,數(shù)據(jù)為空時顯示數(shù)據(jù)為空視圖,支持為失敗視圖設(shè)置點擊事件重新加載數(shù)據(jù)。
2、支持個性化設(shè)置,自定義設(shè)置 加載、失敗、空數(shù)據(jù)視圖。
先放一張效果圖壓壓驚

實現(xiàn)
實現(xiàn)思路其實就是一個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;
//數(shù)據(jù)為空
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();
//設(shè)置點擊錯誤視圖重新加載事件
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);
}
});
//模擬網(wǎng)絡(luò)錯誤,加載失敗
mLoadingView.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingView.loadError();
}
},2500);
}
}
自定義視圖 使用
只需要把自己自定義的view調(diào)用set方法設(shè)置進去即可。
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);
//設(shè)置自定義視圖
ProgressBar progressBar = new ProgressBar(this);
this.mLoadingView.setLoadingView(progressBar);
TextView textView = new TextView(this);
textView.setText("加載失敗...");
this.mLoadingView.setLoadingErrorView(textView);
mLoadingView.load();
//設(shè)置點擊錯誤視圖重新加載事件
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);
}
});
//模擬網(wǎng)絡(luò)錯誤,加載失敗
mLoadingView.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingView.loadError();
}
},2500);
}
}
至于具體的布局和樣式文件就不貼了,主要是實現(xiàn)思路,代碼
下載請參考源碼下載 。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Gradle Plug 4.1.0 升級后gradle獲取manifest位置失敗問題解決
這篇文章主要介紹了Android Gradle Plug 4.1.0 升級后gradle獲取manifest位置失敗問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
android TextView不用ScrollViewe也可以滾動的方法
這篇文章主要介紹了android TextView不用ScrollViewe也可以滾動的方法,很簡單實用的代碼,大家參考使用吧2013-11-11
Android SurfaceView基礎(chǔ)用法詳解
這篇文章主要介紹了Android SurfaceView基礎(chǔ)用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android 7.0調(diào)用相機崩潰詳解及解決辦法
這篇文章主要介紹了 Android 7.0調(diào)用相機崩潰詳解及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12

