Skip to content

Instantly share code, notes, and snippets.

@trunghq3101
Created November 28, 2019 15:18
Show Gist options
  • Select an option

  • Save trunghq3101/20833a25c68a3ec31b40bd51289cb9e3 to your computer and use it in GitHub Desktop.

Select an option

Save trunghq3101/20833a25c68a3ec31b40bd51289cb9e3 to your computer and use it in GitHub Desktop.
example RecyclerView Adapter using view type
abstract class BaseRecyclerAdapter<Item, ViewBinding : ViewDataBinding>(
callBack: DiffUtil.ItemCallback<Item>
) : ListAdapter<Item, BaseViewHolder<ViewBinding>>(
AsyncDifferConfig.Builder<Item>(callBack)
.setBackgroundThreadExecutor(Executors.newSingleThreadExecutor())
.build()
) {
override fun submitList(list: List<Item>?) {
super.submitList(ArrayList<Item>(list ?: listOf()))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ViewBinding> {
return BaseViewHolder(
DataBindingUtil.inflate<ViewBinding>(
LayoutInflater.from(parent.context),
getLayoutRes(viewType),
parent, false
).apply {
bindFirstTime(this)
})
}
override fun onBindViewHolder(holder: BaseViewHolder<ViewBinding>, position: Int) {
try {
val item: Item = getItem(position)
holder.binding.setVariable(BR.item, item)
bindView(holder.binding, item, position)
} catch (e: IndexOutOfBoundsException) {
bind(holder.binding, position)
}
holder.binding.executePendingBindings()
}
/**
* get layout res based on view type
*/
protected abstract fun getLayoutRes(viewType: Int): Int
/**
* override if need
* bind first time
* use for set item onClickListener, something only set one time
*/
protected open fun bindFirstTime(binding: ViewBinding) {}
/**
* override if need
* bind view
*/
protected open fun bindView(binding: ViewBinding, item: Item, position: Int) {}
protected open fun bind(binding: ViewBinding, position: Int) {}
}
open class BaseViewHolder<ViewBinding : ViewDataBinding> constructor(
val binding: ViewBinding
) : RecyclerView.ViewHolder(binding.root)
class EventDetailAdapter(
private val headerViewModel: EventDetailHeaderViewModel
) : BaseRecyclerAdapter<Any?, ViewDataBinding>(eventDetailDiffCallback) {
companion object {
const val TYPE_INFO = 1
const val TYPE_MY_DISCUSSION = 2
const val TYPE_DISCUSSIONS = 3
}
override fun getLayoutRes(viewType: Int): Int {
return when (viewType) {
TYPE_INFO -> R.layout.section_event_detail_info
TYPE_MY_DISCUSSION -> R.layout.item_my_discussion
else -> R.layout.item_discussion
}
}
override fun getItemViewType(position: Int): Int {
return when (position) {
0 -> TYPE_INFO
1 -> TYPE_MY_DISCUSSION
else -> TYPE_DISCUSSIONS
}
}
override fun onBindViewHolder(holder: BaseViewHolder<ViewDataBinding>, position: Int) {
when (position) {
0 -> holder.binding.setVariable(BR.headerViewModel, headerViewModel)
}
super.onBindViewHolder(holder, position)
}
}
val eventDetailDiffCallback = object : DiffUtil.ItemCallback<Any?>() {
override fun areItemsTheSame(oldItem: Any, newItem: Any): Boolean {
return when (oldItem) {
is Event -> oldItem == newItem
else -> true
}
}
override fun areContentsTheSame(oldItem: Any, newItem: Any): Boolean {
return when (oldItem) {
is Event -> (oldItem as Event) == newItem
else -> true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment