Skip to content

Instantly share code, notes, and snippets.

@plusmobileapps
Created December 11, 2018 07:15
Show Gist options
  • Save plusmobileapps/f012519c4b5d383d5f26fcabed528131 to your computer and use it in GitHub Desktop.
Save plusmobileapps/f012519c4b5d383d5f26fcabed528131 to your computer and use it in GitHub Desktop.
class RecyclerViewListAdapter(
private val carouselItemClickListener: (CarouselItem) -> Unit,
private val cardClickListener: (DataWrapper.CardData) -> Unit,
private val cardDeleteListener: (DataWrapper.CardData) -> Unit,
private val glide: RequestManager
) : ListAdapter<DataWrapper, BaseViewHolder<*>>(
RecyclerViewDiffUtil()
) {
override fun getItemViewType(position: Int): Int {
return when (getItem(position)) {
is DataWrapper.CarouselData -> R.layout.carousel_view_holder
is DataWrapper.CardData -> R.layout.card_view_holder
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
return when (viewType) {
R.layout.carousel_view_holder -> CarouselViewHolder(view, carouselItemClickListener)
R.layout.card_view_holder -> CardViewHolder(view, glide, cardClickListener, cardDeleteListener)
else -> throw IllegalStateException("There is no layout file for $viewType in this recycler view")
}
}
override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
val item = getItem(position)
when (item) {
is DataWrapper.CardData -> (holder as CardViewHolder).bind(item)
is DataWrapper.CarouselData -> (holder as CarouselViewHolder).bind(item.items)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment