适配器或ViewHolder中的Kotlin合成

时间:2015-10-23 14:16:18

标签: kotlin kotlin-android-extensions

我是kotlin的新人。我发现并尝试在我的findViewById类中使用合成方法而不是恼人的方法Activity,但我发现“如果我们想在View上调用合成属性(在适配器类中很有用),我们还应该导入kotlinx.android.synthetic.main.view。*。“但我无法弄清楚它究竟是如何起作用的?有没有例子?

7 个答案:

答案 0 :(得分:75)

来自https://github.com/antoniolg/Kotlin-for-Android-Developers

的简单示例
import kotlinx.android.synthetic.item_forecast.view.*

class ForecastListAdapter() : RecyclerView.Adapter<ForecastListAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        fun bindForecast(forecast: Forecast) {
            itemView.date.text = forecast.date.toDateString()
        }
    }
}

无需撰写

val view = itemView.findViewById(R.id.date) as TextView
view.text = forecast.date.toDateString()

只需

itemView.date.text = forecast.date.toDateString()

简单有效!

答案 1 :(得分:22)

Kotling 1.1.4 out

更多信息:https://antonioleiva.com/kotlin-android-extensions/

因此,您需要启用它们 build.gradle:

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

自从这个新版本的Kotlin以来,Android Extensions已经集成了一些新的有趣功能:任何类中的缓存(有趣的是包含ViewHolder)

在ViewHolder(或任何自定义类)上使用它

class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), 
        LayoutContainer {

    fun bind(title: String) {
        itemTitle.text = "Hello Kotlin!"
    }
}

答案 2 :(得分:6)

你需要

import kotlinx.android.synthetic.row_wall.view.*

后来出现了以下问题:

convertView.titleText.text = item.title

重点是视图。*引入了View类的扩展。

答案 3 :(得分:6)

尝试

class CustomViewModel(val baseView: View) { val firstName = baseView.firstName val lastName = baseView.lastName }

View对象公开视图 参考:https://discuss.kotlinlang.org/t/unable-to-use-kotlin-android-extension-in-adapter-class/2890

答案 4 :(得分:3)

如果您使用的是最新版本l ;,则无需在其中添加实验性= true。

在项目级别Gradle中

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21'

在应用程序级别Gradle中

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' //These should be on the top of file.

和依赖项。

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21'

并在下面导入为

import kotlinx.android.synthetic.main.your_layout_file_name.view.*

和示例

import kotlinx.android.synthetic.main.item_animal.view.*

class AnimalVH(parent: ViewGroup, layoutID: Int) : BaseViewHolder<Animal>(parent, layoutID) {

    override fun bindData(animal: Animal) {
        itemView.tv_animal.text = animal.title
    }
}

BaseViewHolder在

abstract class BaseViewHolder<T>(parent: ViewGroup, layoutID: Int) : RecyclerView.ViewHolder(
    LayoutInflater.from(parent.context).inflate(layoutID, parent, false)
) {
    abstract fun bindData(model: T)
}

答案 5 :(得分:1)

这意味着您必须将此行放在源文件的开头:

import kotlinx.android.synthetic.main.view.*

所以现在代替findView(R.id.textView) as TextView,而不是textView。后者是位于包kotlinx.android.synthetic.main.view中的合成扩展属性,这就是您必须从中导入所有内容的原因。

有一个tutorial on the official site,请看一下。

答案 6 :(得分:0)

仅供参考:对于视图查找,建议在合成数据上绑定数据。

Comment from a DA for Android from Google在Reddit上

  

嘿! Google的Android开发者倡导者在这里!

     

我想在这里添加一些背景。 Kotlin扩展与   尽管从来没有刻意“推荐”综合观点   不建议您不要使用它们。如果他们是   为您工作,请随时在您的应用中继续使用它们!

     

我们一直在远离他们(例如,我们没有在   Udacity课程),因为它们公开了ID的全局名称空间   与实际上没有检查的夸张的布局无关   反对无效查找,仅适用于Kotlin,并且不暴露可空性   当视图仅在某些配置中存在时。在一起,这些   问题会导致API增加Android应用程序的崩溃次数。

     

另一方面,他们确实提供了轻量级的API,可以帮助   简化视图查找。在这个空间中,也值得一看   数据绑定还可以自动查看视图-以及   与LiveData集成以自动将视图更新为数据   变化。

     

今天,该空间中有一些可行的选择:

     

数据绑定是视图查找和绑定的建议,   但与Android Kotlin相比,确实增加了一些开销   扩展程序。值得一看,看看是否适合   您的应用。数据绑定还允许您观察LiveData进行绑定   数据更改时自动查看。与Kotlin扩展程序相比,   它增加了视图查找和类型安全性的编译时检查。安卓系统   不正式推荐Kotlin Extensions(这是不一样的   作为反对的建议。它确实带有提到的问题   以上,因此对于我们的代码,我们不使用它们。黄油刀是另一种   此解决方案非常受欢迎,并且适用于Kotlin和   Java编程语言。阅读评论,这里有一个   许多对Kotlin Extensions感到幸运的开发人员。   太好了-在寻找方法时我们会牢记   继续改进我们的API。如果您还没有看过数据   绑定,一定要试一下。

     

顺便说一句,我们的内部代码样式指南并非旨在   直接应用于我们的代码库之外。例如,我们使用   mPrefixVariables,但是没有理由每个应用都应该遵循   这种风格。