android - Refactor my viewholder class in kotlin -


i have recycler list holds many different types of item views. quite easy use databinding without necessary declare layout , assignment in viewholder, end many biloplate code create different viewholders databinding, there way rid of them?

class viewholder1 private constructor(     val binding: viewholder1layoutbinding ): recyclerview.viewholder(binding.root) {     companion object {         fun create(parent: viewgroup): recyclerview.viewholder {             val inflater = layoutinflater.from(parent.context)             val binding = viewholder1layoutbinding.inflate(inflater, parent, false)             return viewholder1(binding)         }     }      fun bind(viewmodel: viewholder1viewmodel) {         binding.viewmodel = viewmodel         binding.executependingbindings()     } } 

kotlin supports view binding no need other stuffs binding view. follow steps , able access view id defined in xml layout.

in app level gradle add following

apply plugin: 'kotlin-android-extensions' 

import view

import kotlinx.android.synthetic.main.<layout_file>.view.* 

just check class demo

class notificationholder(itemview: view?, listener: notificationitemlistener) : recyclerview.viewholder(itemview) {     init {         itemview?.setonclicklistener {             listener.onnotificationitemclicked(adapterposition)         }     }      fun bind(notificationmodel: notificationmodel) {         val titlearray = notificationmodel.title.split("#".toregex()).droplastwhile { it.isempty() }.totypedarray()         itemview.tvnotificationtitle.text = titlearray[0]         itemview.tvnotificationdetails.text = notificationmodel.message         itemview.tvnotificationtime.text = notificationmodel.formattedtime         glide.with(itemview.context).load(servicehandler.base_url + notificationmodel.icon).dontanimate().diskcachestrategy(diskcachestrategy.source).error(r.drawable.user_default_logo).into(itemview.imageview)         if (commonutils.lastnotificationtime < notificationmodel.date) {             itemview.card.setcardbackgroundcolor(color.parsecolor("#ffffff"))         } else {             itemview.card.setcardbackgroundcolor(color.parsecolor("#f2f2f2"))         }     } } 

in adapter can override

override fun oncreateviewholder(parent: viewgroup?, viewtype: int): recyclerview.viewholder {         return if (viewtype == 0 || viewtype == 3) {             notificationholder(layoutinflater.from(parent?.context).inflate(r.layout.item_notification, parent, false), this)         } else {             notificationlistheaderholder(layoutinflater.from(parent?.context).inflate(r.layout.item_notification_header, parent, false))         }     }  override fun onbindviewholder(holder: recyclerview.viewholder?, position: int) {         (holder as? notificationholder)?.bind(notificationlist[position])         (holder as? notificationlistheaderholder)?.bind(notificationlist[position])      } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -