简体   繁体   English

Kotlin:如何从 function 访问 LayoutInflater?

[英]Kotlin: How can i access the the LayoutInflater from a function?

I've a fragment with a recyclerview , i need to fill the RecyclerView from a function, but i don't know how to access to the recyclerView from the function, this is my code:我有一个带有recyclerview的片段,我需要从 function 填充RecyclerView ,但我不知道如何从 function 访问recyclerView ,这是我的代码:

class ValcuotaDataFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val root = inflater.inflate(R.layout.fragment_valcuota_data, container, false)

    return root
}
@Subscribe
fun listenItemChange(spinnerData: SpinnerData){   }

i need to do this:我需要这样做:

        val rcView= root.findViewById(R.id.rcViewName) as RecyclerView

from the function listenItemChange从 function listenItemChange

Whatever you return from onCreateView() can be later retrieved with getView() .无论您从onCreateView()返回什么,都可以稍后使用getView()检索。 So you're looking for something like view?.findViewById(...) .所以你正在寻找类似view?.findViewById(...)的东西。

Be cautious with fragment lifecycle so you're only trying to use getView() between onCreateView() and onDestroyView() .请谨慎使用片段生命周期,因此您只尝试在onCreateView()onDestroyView() getView() ) 。 (It will return null otherwise.) (否则它将返回 null。)

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

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