[英]How to create base class for custom views in Android using Kotlin?
I want to create a base class for all of my custom views. 我想为我的所有自定义视图创建一个基类。 It can be a different type, for example, RelativeLayout
or NavigationView
. 它可以是不同的类型,例如RelativeLayout
或NavigationView
。
So I've created abstract class
with generics which is implementing the deepest class of these views which is connecting them - View
. 所以我用泛型创建了abstract class
,它实现了连接它们的最深层的这些视图View
。 This is what I've got: 这就是我所拥有的:
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) { ... }
But the problem is that now I'm able to inherit only from the View
. 但问题是,现在我只能从View
继承了。 How can I build somehow generic that must be a child of the View
so I could implement it and still to be able inside my base class to override
methods like onAttachedToWindow
? 我怎样才能构建一些必须是View
的子类的通用,这样我才能实现它,并且仍然可以在我的基类中override
像onAttachedToWindow
这样的方法?
Try to declare your class like this: 尝试像这样声明你的类:
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.