繁体   English   中英

如何在 Kotlin 的适配器 class 中应用视图绑定?

[英]How can I apply the view binding in adapter class in Kotlin?

我正在尝试在我的项目中应用视图绑定,我喜欢那个适配器 class,但我不明白我将如何应用。 任何想法?

 class Center : AppCompatActivity() {
private lateinit var binding: ActivityCenterBinding
var listView: ListView? = null
private var mTitle = arrayOf("Help", "Help2", "Help3")

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityCenterBinding.inflate(layoutInflater)

    val view = binding.root
    setContentView(view)

    listView =   binding.help_listView
    val adapter = MyAdapter(this, mTitle)
    listView!!.adapter = adapter

  
internal inner class MyAdapter(
    context: Center,
    private var rTitle: Array<String>,

) : ArrayAdapter<String?>(context, R.layout.row_center, R.id.textView_center, rTitle) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val layoutInflater =
            applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val binding = ActivityCenterBinding.inflate(layoutInflater, parent, false)

        val myTitle =   binding.textView_center


        myTitle.text = rTitle[position]
        return binding.root
    }
}

问题是

未解决的参考:textView__center

预期变量

对于文本

当您使用视图绑定时,每个布局 XML class 都会生成一个以它命名的“绑定” class。 所以R.layout.row_center有一个名为 RowCenterBinding 的RowCenterBinding (删除了下划线,单词大写,并以Binding结尾。

您可以通过调用 static inflate方法来创建绑定 class 的实例,如下所示:

// in an Activity
val binding = RowCenterBinding.inflate(layoutInflater)
// in anything else - you pass the thing the View is being inflated into (parent)
// and whether it should actually be added to that parent (no)
val binding = RowCenterBinding.inflate(layoutInflater, parent, false)

现在你有一个RowCenterBinding object。 它有一个root属性,它是您膨胀的视图层次结构(您需要在问题中的getView方法的末尾返回它)。 它还具有 XML 文件中 ID 的每个View的属性。 (这些重命名方式与row_center.xml -> RowCenter相同。)

所以你最终得到了一个binding object ,它包含你的View```s ( ) and also references to the ones with IDs ( ( binding.textView1 , binding.saveButton 等) 就是这样!

(如果您已经拥有从 XML 文件扩展的视图层次结构,例如在Activity中,则可以使用RowCenterBinding.bind(view)创建绑定 object )


你一直在编辑问题并夸大不同的东西,所以这样做:

internal inner class MyAdapter(
    context: Center,
    private var rTitle: Array<String>,

) : ArrayAdapter<String?>(context, R.layout.row_center, R.id.textView_center, rTitle) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val layoutInflater =
            applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater

        // You're inflating row_center.xml, so RowCenterBinding is the class you want
        // Don't specify a type here unless you know what you're doing - this is fine!
        val binding = RowCenterBinding.inflate(layoutInflater, parent, false)

        // this requires a view in the XML with an android:id of textViewCenter,
        // text_view_center, or something like that. Let autocomplete help you here!
        val myTitle =   binding.textViewCenter

        // this should work fine once myTitle is assigned to a TextView
        myTitle.text = rTitle[position]

        // this method requires an inflated View, so we return the root view
        return binding.root
    }
}

由于您的外部活动中也有一个名为binding的变量,因此您可能希望将其称为rowBinding或其他名称,以免混淆它们。

(此外,您不需要对基本的ArrayAdapter执行任何此操作,如果您删除getView方法,它不就可以工作吗?默认情况下,它应该创建一个视图并使用您的构造函数参数在 TextView 上设置文本'重新通过)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM