gpt4 book ai didi

android - 如何从Kotlin的内部类调用 fragment 方法?

转载 作者:行者123 更新时间:2023-12-02 13:17:37 25 4
gpt4 key购买 nike

我在片段中创建了一个方法和一个内部类。但是在Fragment的内部类中无法访问外部类方法?如何在片段的内部类中调用外部类方法或成员。下面是我的代码:

class BlankFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mOrientationListener = OrientationChangeListener(requireContext())

}

fun rotateUI(orientation: Int){
/// something
}

/**
* This callback returns rotation angle of the phone, to make it return orientation angles enable it on onStart and disable it onPause
*/
private var mOrientationListener: OrientationChangeListener? = null

internal class OrientationChangeListener (context: Context?) : OrientationEventListener(context) {
/** portrait
* (0, 359)
*
* (270) (90)
* (land) (land)
*/
override fun onOrientationChanged(orientation: Int) {

// this method is inaccessible
//rotateUI(orientation)
}
}}

最佳答案

对于内部类,关键字应该是内部而不是内部,然后您可以使用它来访问方法:-

this@BlankFragment.rotateUI

关于android - 如何从Kotlin的内部类调用 fragment 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62184776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com