Skip to content

Instantly share code, notes, and snippets.

@hector6872
Last active April 24, 2018 14:53
Show Gist options
  • Select an option

  • Save hector6872/15cc6279e4f75942d2bfb0d5cf460efa to your computer and use it in GitHub Desktop.

Select an option

Save hector6872/15cc6279e4f75942d2bfb0d5cf460efa to your computer and use it in GitHub Desktop.
MultiStateFrameLayout Kotlin Android
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MultiStateFrameLayout">
<attr format="reference" name="ms_contentView"/>
<attr format="reference" name="ms_emptyView"/>
<attr format="reference" name="ms_loadingView"/>
<attr format="reference" name="ms_errorView"/>
</declare-styleable>
</resources>
enum class MultiState {
CONTENT,
EMPTY,
LOADING,
ERROR
}
class MultiStateFrameLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
companion object {
private const val NO_VIEW = -1
}
private var contentViewId: Int = NO_VIEW
private var emptyViewId: Int = NO_VIEW
private var loadingViewId: Int = NO_VIEW
private var errorViewId: Int = NO_VIEW
private var contentView: View? = null
private var emptyView: View? = null
private var loadingView: View? = null
private var errorView: View? = null
private var currentState = MultiState.CONTENT
init {
attrs?.let {
val typedArray = context.obtainStyledAttributes(it, R.styleable.MultiStateFrameLayout)
contentViewId = typedArray.getResourceId(R.styleable.MultiStateFrameLayout_ms_contentView, NO_VIEW)
emptyViewId = typedArray.getResourceId(R.styleable.MultiStateFrameLayout_ms_emptyView, NO_VIEW)
loadingViewId = typedArray.getResourceId(R.styleable.MultiStateFrameLayout_ms_loadingView, NO_VIEW)
errorViewId = typedArray.getResourceId(R.styleable.MultiStateFrameLayout_ms_errorView, NO_VIEW)
typedArray.recycle()
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (contentViewId != NO_VIEW) contentView = findViewById(contentViewId)
if (emptyViewId != NO_VIEW) emptyView = findViewById(emptyViewId)
if (loadingViewId != NO_VIEW) loadingView = findViewById(loadingViewId)
if (errorViewId != NO_VIEW) errorView = findViewById(errorViewId)
showState(currentState)
}
fun showContent() {
currentState = CONTENT
showView(contentView)
}
fun showEmpty() {
currentState = EMPTY
showView(emptyView)
}
fun showLoading() {
currentState = LOADING
showView(loadingView)
}
fun showError() {
currentState = ERROR
showView(errorView)
}
fun showState(state: MultiState) {
when (state) {
MultiState.CONTENT -> showContent()
MultiState.EMPTY -> showEmpty()
MultiState.LOADING -> showLoading()
MultiState.ERROR -> showError()
}
}
private fun showView(view: View?) {
view?.run {
contentView?.visibility = View.GONE
emptyView?.visibility = View.GONE
loadingView?.visibility = View.GONE
errorView?.visibility = View.GONE
visibility = View.VISIBLE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment