Last active
May 17, 2020 15:54
-
-
Save fraggjkee/36fed3d6c6de9283b277cfb0040aa6f1 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
class RecyclerViewAdapter : RecyclerView.Adapter<BindingViewHolder>() { | |
private val items = mutableListOf<RecyclerItem>() | |
override fun getItemCount(): Int { | |
return items.size | |
} | |
override fun getItemViewType(position: Int): Int { | |
return getItem(position).layoutId | |
} | |
override fun onCreateViewHolder( | |
parent: ViewGroup, | |
viewType: Int | |
): BindingViewHolder { | |
val inflater = LayoutInflater.from(parent.context) | |
val binding: ViewDataBinding = DataBindingUtil.inflate(inflater, viewType, parent, false) | |
return BindingViewHolder(binding) | |
} | |
override fun onBindViewHolder( | |
holder: BindingViewHolder, | |
position: Int | |
) { | |
getItem(position).bind(holder.binding) | |
holder.binding.executePendingBindings() | |
} | |
fun updateData(newItems: List<RecyclerItem>) { | |
this.items.clear() | |
this.items.addAll(newItems) | |
notifyDataSetChanged() | |
} | |
private fun getItem(position: Int): RecyclerItem { | |
return items[position] | |
} | |
} | |
class BindingViewHolder( | |
val binding: ViewDataBinding | |
) : RecyclerView.ViewHolder(binding.root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment