如何在Kotlin中扩展android.widget.FrameLayout(RequiresApi 21,但是我需要使用minSdk 19)

我试图扩展在我的课kotlin android.widget.FrameLayout。 问题是,因为我升级到kotlin 1.2.21我无法编译,因为FrameLayout的最长(4参数)构造函数需要minSdk 21,但我的库也需要工作在api级别19。 我已经放弃了使用@JvmOverloads(它似乎是越野车和即时运行崩溃),所以我试图写“长”的代码。 就像是:

我尝试的第一种方法:

class PullDownAnimationLayout : FrameLayout, Animator.AnimatorListener, PullDownAnimation { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context) : this(context, null) override val MAX_PULL_HEIGHT_PX: Int override val REFRESH_TRIGGER_HEIGHT_PX: Int init { initFoo(attrs) // ERROR: Unresolved reference: attrs MAX_PULL_HEIGHT_PX = dpToPx(MAX_PULL_HEIGHT_DP) REFRESH_TRIGGER_HEIGHT_PX = dpToPx(REFRESH_TRIGGER_HEIGHT_DP) } 

在这个版本的问题是,init没有看到attrs:未解决的参考:attrs

所以这里有第二种方法,我尝试添加3-args构造函数作为主构造函数:

 class PullDownAnimationLayout(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : FrameLayout(context, attrs, defStyleAttr), Animator.AnimatorListener, PullDownAnimation { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) // ERROR: Primary constructor call expected constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context) : this(context, null) override val MAX_PULL_HEIGHT_PX: Int override val REFRESH_TRIGGER_HEIGHT_PX: Int init { initFoo(attrs) MAX_PULL_HEIGHT_PX = dpToPx(MAX_PULL_HEIGHT_DP) REFRESH_TRIGGER_HEIGHT_PX = dpToPx(REFRESH_TRIGGER_HEIGHT_DP) }