[英]Is it possible to add a variable in the getview adapter function by overriding it?
如何使用此 google 語法在(列表視圖的)getview 中引用適配器:
fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
return (convertView ?: layoutInflater.inflate(R.layout.my_layout, parent, false)).apply {
// … bind content from position to convertView …
//personal comment : "this" does not refer to adapter
}
}
事實上,在.apply 之后,我必須調用適配器才能創建一個adapter.notifyDataSetChanged()
。
使用“經典語法”,可以通過“this”調用適配器,這在 google 語法中是不可能的。
“經典語法”
fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
....
//"this" (refers to adapter)
return convertView
}
那么是否可以覆蓋getview function 在簽名中添加變量? 否則,我的問題的解決方案是什么?
您應該使用.also {... }
而不是.apply {... }
如果我的問題正確,您可以:
編寫與適配器相同的getView(...)
function,並帶有附加參數
在適配器中調用getView()
重寫getView(...)
傳遞你想要的參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.