国内看网站 优帮云,建设人才信息网是什么网站,博物馆设计,如何自己免费建网站文章目录 Android StateLayout状态页概述源码使用源码下载 Android StateLayout状态页
概述
StateLayout#xff08;状态页#xff09;包含#xff1a;加载中页面#xff0c;错误页面#xff0c;空页面#xff0c;内含状态默认页面#xff0c;支持自定义页面。 源码
… 文章目录 Android StateLayout状态页概述源码使用源码下载 Android StateLayout状态页
概述
StateLayout状态页包含加载中页面错误页面空页面内含状态默认页面支持自定义页面。 源码
全局配置
package com.example.tools.state_layout.widgetsimport android.view.View
import androidx.annotation.LayoutRes/*** StateLayout全局配置*/
object StateConfig {LayoutResJvmStaticvar emptyLayoutRes View.NO_IDLayoutResJvmStaticvar errorLayoutRes View.NO_IDLayoutResJvmStaticvar loadingLayoutRes View.NO_IDLayoutResJvmStaticvar retryIds: IntArray? nullprivate var mOnStateChangeListener: OnStateChangeListener? nullfun setOnStateChangeListener(listener: OnStateChangeListener) {mOnStateChangeListener listener}fun getOnStateChangeListener(): OnStateChangeListener? {return mOnStateChangeListener}
}状态监听
interface OnStateChangeListener {fun showState(status: Int)
}属性
declare-styleable nameStateLayoutattr nameempty_layout formatreference /attr nameloading_layout formatreference /attr nameerror_layout formatreference /
/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// 保存状态private val stateInfoMap ArrayMapInt, View()// 当前状态private var currentState STATE_CONTENT// 需要设置点击事件的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必须只能有一个子View)}if (stateInfoMap.size 0) {val view getChildAt(0)setContentView(view)}}/*** 设置内容布局*/private fun setContentView(contentView: View) {stateInfoMap[STATE_CONTENT] contentView}/*** 设置点击事件*/fun setRetryIds(IdRes vararg ids: Int) {retryIds ids}/*** 设置状态变化监听*/fun setOnStateChangeListener(listener: OnStateChangeListener) {mOnStateChangeListener listener}/*** 显示内容布局*/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}/*** 获取状态视图*/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(请设置errorLayout)STATE_EMPTY - throw Resources.NotFoundException(请设置emptyLayout)STATE_LOADING - throw Resources.NotFoundException(请设置loadingLayout)STATE_CONTENT - throw Resources.NotFoundException(请设置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.findViewByIdView(id).setOnClickListener {showLoading()}}}}container.addView(view)}}
}使用
使用全局配置
com.example.tools.state_layout.widgets.StateLayoutandroid:idid/state_layoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentTextViewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:text加载成功 /
/com.example.tools.state_layout.widgets.StateLayoutStateConfig.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, 显示内容页)}StateLayout.STATE_ERROR - {LogUtils.e(StateLayout, 显示失败页)}StateLayout.STATE_EMPTY - {LogUtils.e(StateLayout, 显示空页)}}}
})使用局部配置
com.example.tools.state_layout.widgets.StateLayoutandroid:idid/state_layoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentapp:empty_layoutlayout/state_emptyapp:error_layoutlayout/state_errorapp:loading_layoutlayout/state_loadingTextViewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:text加载成功 /
/com.example.tools.state_layout.widgets.StateLayoutstateLayout.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, 显示内容页)}StateLayout.STATE_ERROR - {LogUtils.e(StateLayout, 显示失败页)}StateLayout.STATE_EMPTY - {LogUtils.e(StateLayout, 显示空页)}}}
})源码下载