Skip to content

Instantly share code, notes, and snippets.

@plusmobileapps
Created December 11, 2018 07:23
Show Gist options
  • Save plusmobileapps/29b1c6b016f8b81ea52d99c5a2384564 to your computer and use it in GitHub Desktop.
Save plusmobileapps/29b1c6b016f8b81ea52d99c5a2384564 to your computer and use it in GitHub Desktop.
abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(data: T)
}
class CardViewHolder(itemView: View,
private val glide: RequestManager,
private val clickListener: (DataWrapper.CardData) -> Unit,
private val deleteListener: (DataWrapper.CardData) -> Unit) : BaseViewHolder<DataWrapper.CardData>(itemView) {
private val header = itemView.findViewById<TextView>(R.id.header)
private val body = itemView.findViewById<TextView>(R.id.body)
private val image = itemView.findViewById<ImageView>(R.id.image)
private val deleteButton = itemView.findViewById<ImageView>(R.id.delete_button)
override fun bind(data: DataWrapper.CardData) {
header.text = data.header
body.text = data.body
glide.load(data.imageUrl).into(image)
itemView.setOnClickListener { clickListener(data) }
deleteButton.setOnClickListener { deleteListener(data) }
}
}
class CarouselViewHolder(itemView: View, clickListener: (CarouselItem) -> Unit) : BaseViewHolder<List<CarouselItem>>(itemView) {
private val recyclerView = itemView.findViewById<RecyclerView>(R.id.carousel_recyclerview)
private val adapter = CarouselListAdapter(clickListener)
init {
recyclerView.adapter = adapter
}
override fun bind(data: List<CarouselItem>) = adapter.submitList(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment