databinding-samples icon indicating copy to clipboard operation
databinding-samples copied to clipboard

How to binding a RecycleView.Adataper

Open a316375 opened this issue 6 years ago • 1 comments

How to binding a RecycleView.Adataper How to use in RecycleView

a316375 avatar Oct 16 '18 06:10 a316375

@a316375

In my case, I created a BindingViewHolder which has ViewDataBinding of generic T type instead of ViewHolder classes such as HeaderViewHolder, ItemViewHolder and etc.

class BindingViewHolder<T: ViewDataBinding> 
    constructor(val binding: T): RecyclerView.ViewHolder(binding.root)

You can create BindingViewHolder on onCreateViewHolder method.

override fun onCreateViewHolder(parent: ViewGroup, position: Int): 
    BindingViewHolder<ViewDataBinding> {
    return BindingViewHolder(ItemScheduleBinding.inflate(
        LayoutInflater.from(parent.context), parent, false))
}

Set data to Binding class.

override fun onBindViewHolder(
    viewHolder: BindingViewHolder<ViewDataBinding>, position: Int) {
    val binding = viewHolder.binding as ItemScheduleBinding
    binding.schedule = items.get(position)
}

kimkevin avatar Nov 27 '18 05:11 kimkevin