简体   繁体   English

是否可以通过覆盖在 getview 适配器 function 中添加变量?

[英]Is it possible to add a variable in the getview adapter function by overriding it?

How to refer to adapter in the getview (of a listview) with this google syntax:如何使用此 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
    }
}

Indeed, after the.apply, I have to call adapter in order to make an adapter.notifyDataSetChanged() .事实上,在.apply 之后,我必须调用适配器才能创建一个adapter.notifyDataSetChanged()

With a "classic syntax", adapter could be called thanks to "this", that is impossible with google syntax.使用“经典语法”,可以通过“this”调用适配器,这在 google 语法中是不可能的。

"Classic syntax" “经典语法”

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

....

//"this" (refers to adapter)

return convertView
}

So is it possible to overriding the getview function to add variable in the signature?那么是否可以覆盖getview function 在签名中添加变量? Else, what is the solution of my problem?否则,我的问题的解决方案是什么?

You should use .also {... } instead of .apply {... }您应该使用.also {... }而不是.apply {... }

If I got your question right, you can:如果我的问题正确,您可以:

Write your getView(...) function same as adapters, with additional parameter编写与适配器相同的getView(...) function,并带有附加参数

call your getView() in adapter overridden getView(...) pass your argument what you want.在适配器中调用getView()重写getView(...)传递你想要的参数。

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

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