首页 > 解决方案 > 有没有办法在 Kotlin 中继承 Fragment(@LayoutRes contentLayoutId: Int) ?

问题描述

我想制作自定义片段类

抽象类 MyBaseFragment:Fragment()

但是当我继承它时,我得到了太多参数的错误

类 ExampleFragment:MyBaseFragment(R.layout.fragment_example)

有没有办法继承 (@LayoutRes contentLayoutId: Int) 而不是 ()

标签: androidkotlinandroid-fragments

解决方案


使用以下代码

abstract class MyBaseFragment : Fragment {
    constructor() : super()
    constructor(@LayoutRes res: Int) : super(res)
}

推荐阅读