繁体   English   中英

如何使用Kotlin在Android中为自定义视图创建基类?

[英]How to create base class for custom views in Android using Kotlin?

我想为我的所有自定义视图创建一个基类。 它可以是不同的类型,例如RelativeLayoutNavigationView

所以我用泛型创建了abstract class ,它实现了连接它们的最深层的这些视图View 这就是我所拥有的:

abstract class MyCustomView<VS : ViewState, V : View<VS>, P : Presenter<VS, V, *>>(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) { ... }

但问题是,现在我只能从View继承了。 我怎样才能构建一些必须是View的子类的通用,这样我才能实现它,并且仍然可以在我的基类中overrideonAttachedToWindow这样的方法?

尝试像这样声明你的类:

abstract class MyCustomView<VS : ViewState, V : View<VS>, P : Presenter<VS, V, *>>() : View{ 

 constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)



(...)
 }

暂无
暂无

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

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