I’m trying to extend android.widget.FrameLayout in my class in kotlin. The problem is that the longest(4 arguments) constructor of FrameLayout requires minSdk 21, but my lib needs to work on api level 19 as well. So I’m trying to do the following but in kotlin:
class PullDownAnimationLayout extends FrameLayout
implements Animator.AnimatorListener, PullDownAnimation {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
PullDownAnimationLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initializeStyledAttributes(attrs);
}
PullDownAnimationLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr)
initializeStyledAttributes(attrs);
}
PullDownAnimationLayout(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0)
}
PullDownAnimationLayout(Context context, @Nullable AttributeSet attrs) {
this(context, null)
}
private void initializeStyledAttributes(@Nullable AttributeSetattrs) {
// ...
}
Here’s my 1st way I tried:
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)
init {
initializeStyledAttributes(attrs) // ERROR: Unresolved reference: attrs
}
private fun initializeStyledAttributes(attrs) {...}
Unfortunately init doesn’t see attrs because there’s no primary constructor. So I tried to add one:
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)
init {
initializeStyledAttributes(attrs)
}
private fun initializeStyledAttributes(attrs) {...}
The problem here is that I need to call the 4-args constructor’s super that is only available in minSdk 21, but I get an error that I need to call the primary constructor.