Android 自定義通用的loadingview實(shí)現(xiàn)代碼
功能
1、顯示加載視圖,加載失敗的時(shí)候顯示加載失敗視圖,數(shù)據(jù)為空時(shí)顯示數(shù)據(jù)為空視圖,支持為失敗視圖設(shè)置點(diǎn)擊事件重新加載數(shù)據(jù)。
2、支持個(gè)性化設(shè)置,自定義設(shè)置 加載、失敗、空數(shù)據(jù)視圖。
先放一張效果圖壓壓驚
實(shí)現(xiàn)
實(shí)現(xiàn)思路其實(shí)就是一個(gè)FrameLayout里添加三個(gè)布局做處理顯示隱藏,自定義視圖其實(shí)就是替換里面的view ,代碼比較簡(jiǎn)單,如果直接看過(guò)我的自定義view系列文章,或者對(duì)自定義view有所了解,都很容易看懂,所有直接上代碼了。
具體代碼
Java 代碼
public class CommonLoadingView extends FrameLayout { //加載時(shí)顯示文字 protected TextView mLoadingTextTv; public Context mContext; //加載錯(cuò)誤視圖 protected LinearLayout mLoadErrorLl; //加載錯(cuò)誤點(diǎn)擊事件處理 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(); } }
使用
基本使用
幾個(gè)基本的 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è)置點(diǎn)擊錯(cuò)誤視圖重新加載事件 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ò)錯(cuò)誤,加載失敗 mLoadingView.postDelayed(new Runnable() { @Override public void run() { mLoadingView.loadError(); } },2500); } }
自定義視圖 使用
只需要把自己自定義的view調(diào)用set方法設(shè)置進(jìn)去即可。
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è)置點(diǎn)擊錯(cuò)誤視圖重新加載事件 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ò)錯(cuò)誤,加載失敗 mLoadingView.postDelayed(new Runnable() { @Override public void run() { mLoadingView.loadError(); } },2500); } }
至于具體的布局和樣式文件就不貼了,主要是實(shí)現(xiàn)思路,代碼
下載請(qǐng)參考源碼下載 。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義ViewGroup之FlowLayout(三)
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup之FlowLayout,常用于關(guān)鍵字標(biāo)簽,搜索熱詞列表等功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Kotlin擴(kuò)展函數(shù)及實(shí)現(xiàn)機(jī)制的深入探索
擴(kuò)展函數(shù)與擴(kuò)展屬性的神奇之處在于,可以在不修改原來(lái)類的條件下,使用函數(shù)和屬性,表現(xiàn)得就像是屬于這個(gè)類的一樣。下面這篇文章主要給大家介紹了關(guān)于Kotlin擴(kuò)展函數(shù)及實(shí)現(xiàn)機(jī)制的相關(guān)資料,需要的朋友可以參考下2018-06-06Android自定義View實(shí)現(xiàn)投票進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)投票進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)登錄界面的方法,Android實(shí)現(xiàn)找回密碼、注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android實(shí)現(xiàn)ListView異步加載圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)ListView異步加載圖片的方法,以實(shí)例形式較為詳細(xì)的分析了Android中ListView異步加載圖片的原理與相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android實(shí)現(xiàn)循環(huán)輪播跑馬燈的效果
這篇文章主要介紹了為大家詳細(xì)介紹了如何通過(guò)Android實(shí)現(xiàn)循環(huán)輪播跑馬燈的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05