Last active
February 1, 2021 22:52
-
-
Save longv/fc1ede0773e71214d13ac6ab2a23e816 to your computer and use it in GitHub Desktop.
DataBindingAdapter for data binding RecyclerView
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
class DataBindingAdapter(private var bindingEntries: List<DataBindingEntry> = emptyList()) | |
: RecyclerView.Adapter<DataBindingViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBindingViewHolder { | |
val layoutInflater = LayoutInflater.from(parent.context) | |
val viewBinding = DataBindingUtil.inflate<ViewDataBinding>(layoutInflater, viewType, parent, false) | |
return DataBindingViewHolder(viewBinding) | |
} | |
override fun onBindViewHolder(holder: DataBindingViewHolder, position: Int) { | |
holder.bind(bindingEntries[position]) | |
} | |
override fun getItemCount(): Int = bindingEntries.size | |
override fun getItemViewType(position: Int): Int = bindingEntries[position].layoutId | |
fun update(entries: List<DataBindingEntry>) { | |
bindingEntries = entries | |
notifyDataSetChanged() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment