【发布时间】:2020-07-21 07:48:06
【问题描述】:
任务是实现数组适配器的getView方法。每次膨胀一个视图,在膨胀的视图中填充各个视图的内容,然后返回视图。方法实现如图所示
private val inflater: LayoutInflater = LayoutInflater.from(context)
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = inflater.inflate(resource, parent, false)
val tvName : TextView = view.findViewById(R.id.tvName)
val tvArtist : TextView = view.findViewById(R.id.tvArtist)
val tvSummary : TextView = view.findViewById(R.id.tvSummary)
val values = data[position]
tvName.text = values.name
tvArtist.text = values.artist
tvSummary.text = values.summary
return view
}
请解释一下为什么我们使用 LayoutInflater.from(context) 方法。我们不能只使用 LayoutInfater.inflate 吗?我搜索了解释,其中一个答案是“LayoutInflater.from 将从给定的上下文中返回一个 LayoutInflater 对象。”我无法理解。如果有人可以帮助我解决这个问题。
【问题讨论】:
标签: java android android-studio kotlin