Skip to content

Instantly share code, notes, and snippets.

@opengamer29
Forked from dkachan1941/AsyncCell
Last active March 31, 2021 06:40
Show Gist options
  • Save opengamer29/9e9c3cc46b1013a5ff07d7c0d4999963 to your computer and use it in GitHub Desktop.
Save opengamer29/9e9c3cc46b1013a5ff07d7c0d4999963 to your computer and use it in GitHub Desktop.
import android.content.Context
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.FrameLayout
import androidx.asynclayoutinflater.view.AsyncLayoutInflater
open class AsyncCell(context: Context) : FrameLayout(context, null, 0, 0) {
init {
layoutParams = LayoutParams(MATCH_PARENT, WRAP_CONTENT)
}
open val layoutId = -1 // override with your layout Id
private var isInflated = false
private val bindingFunctions: MutableList<AsyncCell.() -> Unit> = mutableListOf()
fun inflate() {
AsyncLayoutInflater(context).inflate(layoutId, this) { view, _, _ ->
isInflated = true
addView(createDataBindingView(view))
bindView()
}
}
private fun bindView() {
with(bindingFunctions) {
forEach { it() }
clear()
}
}
fun bindWhenInflated(bindFunc: AsyncCell.() -> Unit) {
if (isInflated) {
bindFunc()
} else {
bindingFunctions.add(bindFunc)
}
}
open fun createDataBindingView(view: View): View? = view // override for usage with dataBinding
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment