9

I have just started using kotlin and I have a block of code in java which I have to convert to kotlin. This is the java code:

public class NonSwipeableViewPager extends ViewPager
{
    public NonSwipeableViewPager(Context context) {
        super(context);
        setMyScroller();
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    private void setMyScroller() {
        //some code
    }
}

If there was only one constructor in this code, I could have written like this:

class NonSwipeableViewPager(context: Context): ViewPager(context) {

     init {
         setMyScroller()
     }

     private fun setMyScroller() {
         //some code
     }
 }

But, as there are two constructors and each constructor calls super() method, I couldn't figure out how can I convert this code to kotlin. The closest I have achieved is this:

class NonSwipeableViewPager(context: Context): ViewPager(context) {

     init {
         setMyScroller()
     }

     constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
         setMyScroller()
     }

     private fun setMyScroller() {
         //some code
     }
 }

But, in this code, i am getting the following error in this line super(context!!, attrs): primary constructor call expected So, how can I call super() from the secondary constructor?

Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34

2 Answers2

5
class NonSwipeableViewPager: ViewPager {

    init {
        setMyScroller()
    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    private fun setMyScroller() {
        //some code
    }
}

Notice that neither secondary constructor requires the call to setMyScroller(), since the init block will be called immediately after the super call.

Ryan
  • 948
  • 6
  • 20
  • 2
    1. There's no primary constructor here, both constructors are secondary; 2. Neither of them needs the call, because the `init` block is called before both (but after the `super` calls). – Alexey Romanov Jan 20 '20 at 07:35
  • thanks @AlexeyRomanov, I have updated my answer to reflect this. – Ryan Jan 21 '20 at 23:51
0

Change your code like this

class NonSwipeableViewPager : ViewPager {
    constructor(context: Context?) : super(context!!) {
        setMyScroller()
    }

    constructor(context: Context?, attrs: AttributeSet?) : this(context) {
        setMyScroller()
    }

    private fun setMyScroller() { //some code
    }
}

instead of

 class NonSwipeableViewPager(context: Context): ViewPager(context) {

     init {
         setMyScroller()
     }

     constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
         setMyScroller()
     }

     private fun setMyScroller() {
         //some code
     }
 }
Hasan Khan
  • 554
  • 1
  • 7
  • 23
Arunachalam k
  • 734
  • 1
  • 7
  • 20
  • Thank u for ur answer. I think I have tried this code. But, what about this line `super(context!!, attrs)` ? Even if I use your code, my question still holds. **How can i call `super()` from secondary constructor?** – Jayesh Babu Jan 20 '20 at 05:23
  • @JayeshBabu this might help u [link](https://stackoverflow.com/a/44493549/6816307) – Hasan Khan Jan 20 '20 at 05:32
  • the other answer solved my problem.. thank u anyway – Jayesh Babu Jan 20 '20 at 05:38