做網(wǎng)站單頁視頻互聯(lián)網(wǎng)媒體廣告公司
文章目錄
- Android StateLayout狀態(tài)頁
- 概述
- 源碼
- 使用
- 源碼下載
Android StateLayout狀態(tài)頁
概述
StateLayout(狀態(tài)頁)包含:加載中頁面,錯(cuò)誤頁面,空頁面,內(nèi)含狀態(tài)默認(rèn)頁面,支持自定義頁面。
源碼
全局配置:
package com.example.tools.state_layout.widgetsimport android.view.View
import androidx.annotation.LayoutRes/*** StateLayout全局配置*/
object StateConfig {@LayoutRes@JvmStaticvar emptyLayoutRes = View.NO_ID@LayoutRes@JvmStaticvar errorLayoutRes = View.NO_ID@LayoutRes@JvmStaticvar loadingLayoutRes = View.NO_ID@LayoutRes@JvmStaticvar retryIds: IntArray? = nullprivate var mOnStateChangeListener: OnStateChangeListener? = nullfun setOnStateChangeListener(listener: OnStateChangeListener) {mOnStateChangeListener = listener}fun getOnStateChangeListener(): OnStateChangeListener? {return mOnStateChangeListener}
}
狀態(tài)監(jiān)聽:
interface OnStateChangeListener {fun showState(status: Int)
}
屬性:
<declare-styleable name="StateLayout"><attr name="empty_layout" format="reference" /><attr name="loading_layout" format="reference" /><attr name="error_layout" format="reference" />
</declare-styleable>
代碼:
class StateLayout @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null,defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {companion object {const val STATE_LOADING = 0xA001const val STATE_EMPTY = 0xA002const val STATE_ERROR = 0xA003const val STATE_CONTENT = 0xA004}// empty布局資源@LayoutResprivate var emptyLayoutRes = View.NO_IDget() = if (field == View.NO_ID) StateConfig.emptyLayoutRes else field// loading布局資源@LayoutResprivate var loadingLayoutRes = View.NO_IDget() = if (field == View.NO_ID) StateConfig.loadingLayoutRes else field// error布局資源@LayoutResprivate var errorLayoutRes = View.NO_IDget() = if (field == View.NO_ID) StateConfig.errorLayoutRes else field// 保存狀態(tài)private val stateInfoMap = ArrayMap<Int, View>()// 當(dāng)前狀態(tài)private var currentState = STATE_CONTENT// 需要設(shè)置點(diǎn)擊事件的idprivate var retryIds: IntArray? = nullget() = field ?: StateConfig.retryIdsprivate var mOnStateChangeListener: OnStateChangeListener? = nullget() = field ?: StateConfig.getOnStateChangeListener()init {val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.StateLayout)emptyLayoutRes = a.getResourceId(R.styleable.StateLayout_empty_layout, View.NO_ID)loadingLayoutRes = a.getResourceId(R.styleable.StateLayout_loading_layout, View.NO_ID)errorLayoutRes = a.getResourceId(R.styleable.StateLayout_error_layout, View.NO_ID)a.recycle()}override fun onFinishInflate() {super.onFinishInflate()if (childCount != 1) {throw IllegalStateException("StateLayout必須只能有一個(gè)子View")}if (stateInfoMap.size == 0) {val view = getChildAt(0)setContentView(view)}}/*** 設(shè)置內(nèi)容布局*/private fun setContentView(contentView: View) {stateInfoMap[STATE_CONTENT] = contentView}/*** 設(shè)置點(diǎn)擊事件*/fun setRetryIds(@IdRes vararg ids: Int) {retryIds = ids}/*** 設(shè)置狀態(tài)變化監(jiān)聽*/fun setOnStateChangeListener(listener: OnStateChangeListener) {mOnStateChangeListener = listener}/*** 顯示內(nèi)容布局*/fun showContent() {showState(STATE_CONTENT)}/*** 顯示加載布局*/fun showLoading() {showState(STATE_LOADING)}/*** 顯示失敗布局*/fun showError() {showState(STATE_ERROR)}/*** 顯示空布局*/fun showEmpty() {showState(STATE_EMPTY)}/*** 顯示視圖*/private fun showState(status: Int) {if (currentState == status) {return}val stateView = getStateView(status)for (i in stateInfoMap) {if (i.key != status) {val view = i.valuehideStateView(view)}}showStateView(this, stateView, status)mOnStateChangeListener?.showState(status)currentState = status}/*** 獲取狀態(tài)視圖*/private fun getStateView(status: Int): View {val view = stateInfoMap[status]if (view != null) {return view} else {val layoutRes = when (status) {STATE_EMPTY -> emptyLayoutResSTATE_ERROR -> errorLayoutResSTATE_LOADING -> loadingLayoutResSTATE_CONTENT -> View.NO_IDelse -> View.NO_ID}if (layoutRes == View.NO_ID) {when (status) {STATE_ERROR -> throw Resources.NotFoundException("請?jiān)O(shè)置errorLayout")STATE_EMPTY -> throw Resources.NotFoundException("請?jiān)O(shè)置emptyLayout")STATE_LOADING -> throw Resources.NotFoundException("請?jiān)O(shè)置loadingLayout")STATE_CONTENT -> throw Resources.NotFoundException("請?jiān)O(shè)置contentView")}}val view = LayoutInflater.from(context).inflate(layoutRes, this, false)stateInfoMap[status] = viewreturn view}}/*** 隱藏視圖*/private fun hideStateView(view: View) {view.visibility = View.GONE}/*** 顯示視圖*/private fun showStateView(container: StateLayout, view: View, status: Int) {if (container.indexOfChild(view) != -1) {view.visibility = View.VISIBLE} else {if (status == STATE_EMPTY || status == STATE_ERROR) {if (retryIds != null) {for (id in retryIds!!) {view.findViewById<View>(id).setOnClickListener {showLoading()}}}}container.addView(view)}}
}
使用
使用全局配置:
<com.example.tools.state_layout.widgets.StateLayoutandroid:id="@+id/state_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="加載成功" />
</com.example.tools.state_layout.widgets.StateLayout>
StateConfig.emptyLayoutRes = R.layout.state_empty
StateConfig.errorLayoutRes = R.layout.state_error
StateConfig.loadingLayoutRes = R.layout.state_loading
StateConfig.retryIds = intArrayOf(R.id.state_msg, R.id.state_iv)StateConfig.setOnStateChangeListener(object : OnStateChangeListener {override fun showState(status: Int) {when (status) {StateLayout.STATE_LOADING -> {LogUtils.e("StateLayout", "顯示加載頁")postDelayed({stateLayout.showContent()}, 2000L)}StateLayout.STATE_CONTENT -> {LogUtils.e("StateLayout", "顯示內(nèi)容頁")}StateLayout.STATE_ERROR -> {LogUtils.e("StateLayout", "顯示失敗頁")}StateLayout.STATE_EMPTY -> {LogUtils.e("StateLayout", "顯示空頁")}}}
})
使用局部配置:
<com.example.tools.state_layout.widgets.StateLayoutandroid:id="@+id/state_layout"android:layout_width="match_parent"android:layout_height="match_parent"app:empty_layout="@layout/state_empty"app:error_layout="@layout/state_error"app:loading_layout="@layout/state_loading"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="加載成功" />
</com.example.tools.state_layout.widgets.StateLayout>
stateLayout.setRetryIds(R.id.state_msg, R.id.state_iv)
stateLayout.setOnStateChangeListener(object : OnStateChangeListener {override fun showState(status: Int) {when (status) {StateLayout.STATE_LOADING -> {LogUtils.e("StateLayout", "顯示加載頁")postDelayed({stateLayout.showContent()}, 2000L)}StateLayout.STATE_CONTENT -> {LogUtils.e("StateLayout", "顯示內(nèi)容頁")}StateLayout.STATE_ERROR -> {LogUtils.e("StateLayout", "顯示失敗頁")}StateLayout.STATE_EMPTY -> {LogUtils.e("StateLayout", "顯示空頁")}}}
})