【问题标题】:kotlin syntaxes change into viewBindingkotlin 语法变为 viewBinding
【发布时间】:2021-05-09 17:01:44
【问题描述】:

我已经在 Android Studio 中进行了 viewBinding 更改。但不幸的是我不知道如何将以下代码更改为viewBinding。请帮帮我

    package com.example.myapplication

     import android.view.LayoutInflater
     import android.view.View
     import android.view.ViewGroup
     import android.widget.BaseAdapter
     import android.widget.TextView


     class Temp(private  val data: List<DataModel>): BaseAdapter() {

override fun getCount(): Int = data.count()

override fun getItem(position: Int): DataModel = data[position]

override fun getItemId(position: Int): Long =position.toLong()

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {





    val view = LayoutInflater.from(parent !!.context).inflate(R.layout.temp,null)
    val data1 = getItem(position)
    view.proImg.setImageResource()
    return view
}

}
    
    

【问题讨论】:

标签: android android-viewbinding


【解决方案1】:

尝试改变

val view = LayoutInflater.from(parent !!.context).inflate(R.layout.temp,null)

val inflater = LayoutInflater.from(parent!!.context)
val view = TempBinding.inflate(inflater, null)

然后

return view.root

别忘了导入 TempBinding

【讨论】:

    【解决方案2】:

    你可以这样做:

    val binding = TempBinding.inflate(
        LayoutInflater.from(context)
    )
    return binding.view
    

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 2020-02-09
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      相关资源
      最近更新 更多