manneohlund / smart-recycler-adapter

Small, smart and generic adapter for recycler view with easy and advanced data to ViewHolder binding.
https://manneohlund.github.io/smart-recycler-adapter/
Apache License 2.0
402 stars 51 forks source link

ViewBinding or Kotlin synthetics #17

Closed P1NG2WIN closed 4 years ago

P1NG2WIN commented 4 years ago

Does this library support Kotlin synthetics or view binding in ViewHolder?

manneohlund commented 4 years ago

Yes that shouldn't be a problem, use kotlin-android-extensions, but afaik it's still experimental.

P1NG2WIN commented 4 years ago

Yes that shouldn't be a problem, use kotlin-android-extensions, but afaik it's still experimental.

As far as I know, the view holder must be configured to work with synthetic, out of the box it does not work and each time use findViewById if we use itemView.'...'

manneohlund commented 4 years ago

Hmm well thats all taken care of with the org.jetbrains.kotlin.android.extensions that is auto generating these properties for all the different view controller types (Activity, Fragment, Dialog etc)

Important is to use RecyclerView.ViewHolder.itemView as the root view for the synthetic property resolving. Pattern for synthetic property import is: import kotlinx.android.synthetic.<variantName>.<layoutName>.view.<viewName>

Try out this:

Gradle

apply plugin: 'org.jetbrains.kotlin.android.extensions'
androidExtensions {
    experimental = true
}

ViewHolder

import kotlinx.android.synthetic.main.layoutName.view.title

class TitleViewHolder(parentView: ViewGroup) : 
    SmartViewHolder<Model>(parentView, R.id.layoutName) {

    override fun bind(item: Model) {
        itemView.title.text = item.title
    }
}
manneohlund commented 4 years ago

I've included some synthetic imports in the sample app so i'm closing this issue for now :)