Android最簡(jiǎn)單的狀態(tài)切換布局實(shí)現(xiàn)教程
前言
項(xiàng)目中經(jīng)常遇到這樣一種情況,新打開的界面需要加載數(shù)據(jù),存在多種狀態(tài)的結(jié)果,需要根據(jù)不同結(jié)果展示界面,這個(gè)過程歸納起來可以分為五種狀態(tài):初始狀態(tài)、請(qǐng)求狀態(tài)、空數(shù)據(jù)狀態(tài)、網(wǎng)絡(luò)錯(cuò)誤狀態(tài)、成功請(qǐng)求狀態(tài)。 如果多個(gè)界面都存在這個(gè)流程,那么封裝整個(gè)過程的調(diào)用就很有必要了,既可以簡(jiǎn)化調(diào)用過程,又可以很方便的管理整個(gè)流程。
下面話不多說了,來一起看看詳細(xì)的介紹吧
功能簡(jiǎn)介
- 正在加載數(shù)據(jù)
- 數(shù)據(jù)加載失敗
- 數(shù)據(jù)加載為空
- 網(wǎng)絡(luò)加載失敗
- 重試點(diǎn)擊事件
- 支持自定義布局
效果圖展示
最簡(jiǎn)單的使用方式
1.Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
2.Add the dependency
dependencies { implementation 'com.github.pengMaster:MultipleLayout:1.0.0' }
3.在布局中添加
<king.bird.multipleview.MultipleLayout android:id="@+id/mMultipleLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <!--任意內(nèi)容--> <TextView android:id="@+id/mTvContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/content" /> </king.bird.multipleview.MultipleLayout>
注意: MultipleLayout 可做為沒有標(biāo)題欄的最外層布局,內(nèi)部可包裹任何內(nèi)容
4.代碼中使用
//重試點(diǎn)擊事件 mMultipleLayout.setOnRetryClickListener { //模擬網(wǎng)絡(luò)請(qǐng)求 Toast.makeText(this@MainActivity,"正在加載。。",Toast.LENGTH_SHORT).show() } //數(shù)據(jù)為空 mMultipleLayout.showEmpty() //加載失敗 mMultipleLayout.showError() //正在加載 mMultipleLayout.showLoading() //網(wǎng)絡(luò)加載失敗 mMultipleLayout.showNoNetwork() //顯示內(nèi)容 mMultipleLayout.showContent()
擴(kuò)展功能
1.自定義狀態(tài)布局
<king.bird.multipleview.MultipleLayout android:id="@+id/multipleStatusView" android:layout_width="match_parent" android:layout_height="match_parent" <!--自定義布局--> app:emptyView="@layout/layout_empty_view" app:errorView="@layout/layout_error_view" app:loadingView="@layout/layout_loading_view" app:noNetworkView="@layout/layout_network_view"> </king.bird.multipleview.MultipleLayout>
2.代碼引入布局
//數(shù)據(jù)為空 showEmpty(int layoutId, ViewGroup.LayoutParams layoutParams) showEmpty(View view, ViewGroup.LayoutParams layoutParams) //加載失敗 showError(int layoutId, ViewGroup.LayoutParams layoutParams) showError(View view, ViewGroup.LayoutParams layoutParams) //正在加載 showLoading(int layoutId, ViewGroup.LayoutParams layoutParams) showLoading(View view, ViewGroup.LayoutParams layoutParams) //網(wǎng)絡(luò)加載失敗 void showNoNetwork(int layoutId, ViewGroup.LayoutParams layoutParams) showNoNetwork(View view, ViewGroup.LayoutParams layoutParams)
3.擴(kuò)展
后續(xù)添加各種彈框
參與貢獻(xiàn)
- Fork 本項(xiàng)目
- 新建 Feat_xxx 分支
- 提交代碼
- 新建 Pull Request
github地址
項(xiàng)目地址:https://github.com/pengMaster/MultipleLayout
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無法接收到Touch事件的問題
- 詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法
- Android使用libgdx實(shí)現(xiàn)模擬方向鍵控制角色移動(dòng)的方法
- Android 游戲引擎libgdx 資源加載進(jìn)度百分比顯示案例分析
- Android drawable微技巧,你不知道的drawable細(xì)節(jié)
- Android指紋識(shí)別API講解,一種更快更好的用戶體驗(yàn)
- Android在Kotlin中更好地使用LitePal
- Android Studio輕松構(gòu)建自定義模板的步驟記錄
- 詳解Android 檢測(cè)權(quán)限的三種寫法
- android自定義環(huán)形對(duì)比圖效果
- Libgdx解決部分Android機(jī)型鎖屏崩潰的方法
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android實(shí)現(xiàn)GPS定位代碼實(shí)例
這篇文章主要介紹了Android實(shí)現(xiàn)GPS定位實(shí)例,對(duì)關(guān)鍵操作部份給出代碼示例并做了一定的注釋,需要的朋友可以參考下2014-07-07Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路
這篇文章主要介紹了Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路,需要的朋友可以參考下2018-04-04Android自定義控件開發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼
這篇文章主要介紹了Android自定義控件開發(fā)實(shí)戰(zhàn)之實(shí)現(xiàn)ListView下拉刷新實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04Android判斷用戶2G/3G/4G移動(dòng)數(shù)據(jù)網(wǎng)絡(luò)
這篇文章主要介紹了Android判斷用戶2G/3G/4G移動(dòng)數(shù)據(jù)網(wǎng)絡(luò)的方法,感興趣的小伙伴們可以參考一下2015-12-12Android 使用registerReceiver注冊(cè)BroadcastReceiver案例詳解
這篇文章主要介紹了Android 使用registerReceiver注冊(cè)BroadcastReceiver案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08