-
-
Save opengamer29/9e9c3cc46b1013a5ff07d7c0d4999963 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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