Do not always create new adapter

This commit is contained in:
LoveSy 2023-03-10 08:00:08 +08:00 committed by GitHub
parent 1a1646795f
commit d130aa02a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
class RvItemAdapter<T: RvItem>( class RvItemAdapter<T: RvItem>(
private val items: List<T>, internal val items: List<T>,
private val extraBindings: SparseArray<*>? private val extraBindings: SparseArray<*>?
) : RecyclerView.Adapter<RvItemAdapter.ViewHolder>() { ) : RecyclerView.Adapter<RvItemAdapter.ViewHolder>() {
@ -113,6 +113,8 @@ inline fun bindExtra(body: (SparseArray<Any?>) -> Unit) = SparseArray<Any?>().al
@BindingAdapter("items", "extraBindings", requireAll = false) @BindingAdapter("items", "extraBindings", requireAll = false)
fun <T: RvItem> RecyclerView.setAdapter(items: List<T>?, extraBindings: SparseArray<*>?) { fun <T: RvItem> RecyclerView.setAdapter(items: List<T>?, extraBindings: SparseArray<*>?) {
if (items != null) { if (items != null) {
if ((adapter as? RvItemAdapter<T>)?.items !== items) {
adapter = RvItemAdapter(items, extraBindings) adapter = RvItemAdapter(items, extraBindings)
} }
} }
}