Skip to content

Instantly share code, notes, and snippets.

@ahndwon
Created April 4, 2021 03:17
Show Gist options
  • Save ahndwon/efc2c887198424fa5b0880a88bbb1991 to your computer and use it in GitHub Desktop.
Save ahndwon/efc2c887198424fa5b0880a88bbb1991 to your computer and use it in GitHub Desktop.
@Suppress("UNCHECKED_CAST")
@BindingAdapter("itemsWithListAdapter")
fun <T, VH : RecyclerView.ViewHolder> setItemsWithListAdapter(
recyclerView: RecyclerView,
items: List<T>?
) {
(recyclerView.adapter as? ListAdapter<T, VH>)?.let { adapter ->
val newList = if (items == null || items.isEmpty()) null else ArrayList(items)
adapter.submitList(newList)
if (newList == null) {
adapter.notifyDataSetChanged()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment