Kotlin synthetic in Adapter or ViewHolder

前端 未结 7 658
孤独总比滥情好
孤独总比滥情好 2020-12-13 05:36

I am new in kotlin. I have found and tried to use synthetic method instead of annoying method findViewById in my Activity class, but I have found \

7条回答
  •  清歌不尽
    2020-12-13 05:58

    Kotling 1.1.4 out

    Further information : https://antonioleiva.com/kotlin-android-extensions/

    You need to enable Kotlin Android Extentions by adding this to your build.gradle:

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

    Since this new version of Kotlin, the Android Extensions have incorporated some new interesting features: caches in any class (which interestingly includes ViewHolder)

    Using it on a ViewHolder (or any custom class). Note that this class should implement LayoutContainer interface:

    class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), 
            LayoutContainer {
    
        fun bind(title: String) {
            itemTitle.text = "Hello Kotlin!"
        }
    }
    

提交回复
热议问题