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

vnjpjtjt  于 2023-04-28  发布在  Android
关注(0)|答案(1)|浏览(179)

我尝试在我的项目中应用视图绑定,我喜欢适配器类,但我不明白我将如何应用。有什么想法吗?

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

预期变量
用于文本

2g32fytz

2g32fytz1#

使用View Binding时,每个布局XML类都会生成一个以其命名的“binding”类。因此,R.layout.row_center有一个名为RowCenterBinding的类(下划线被删除,单词大写,并以Binding结尾)。
通过调用绑定类的静态inflate方法来创建绑定类的示例,如下所示:

// 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对象。它有一个root属性,这是你膨胀的视图层次结构(你需要在问题中的getView方法的末尾返回)。它还有一个属性,每个在XML文件中有ID的View都有一个属性。(这些属性的重命名方式与row_center.xml -> RowCenter相同。)
所以你最终得到了一个binding对象,它保存了你的View s(binding.root),还引用了那些ID为(binding.textView1binding.saveButton等)的对象。
(If你已经有了一个从XML文件膨胀的视图层次结构,比如在Activity中,你可以用RowCenterBinding.bind(view)从它创建绑定对象)
你一直在编辑问题,夸大不同的东西,所以只要这样做:

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
    }
}

由于在外部Activity中也有一个名为binding的变量,因此您可能希望将其命名为rowBinding或其他名称,以免混淆它们。
(Also对于基本的ArrayAdapter,你不应该需要做任何这些,如果你删除getView方法,它不就可以工作了吗?默认情况下,它应该创建一个视图,并使用你传递的构造函数参数在TextView上设置文本)

相关问题