Equivalent implementing java interface on kotlin


#1

I’m newbie in Kotlin and i’m trying to know how can i implementing java interface on kotlin, i’m using that on android,

public interface OnClickedItemListener {
    void onClick(boolean state);
}

OnClickedItemListener is my custom interface which i want to implementing that, in kotlin i have this class:

class MyProgressView : RelativeLayout {
    constructor(context: Context?) : super(context) {
        init()
    }

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

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.download_progress_layout, this)
        cusotmView.setOnClickListener {
        }
    }
}

in that whats equivalent this cods for example:

class MyProgressView : RelativeLayout {
    constructor(context: Context?) : super(context) {
        init()
    }

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

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.download_progress_layout, this)
        cusotmView.setOnClickListener {
            /*
            if(onitemClickListener!=null) onitemClickListener.onClick()
            */
        }
    }

    /*
    public interface OnitemClickListener{
        void onClick();
    }

    public static void setOnitemClickListener(OnitemClickListener listener){
        onitemClickListener = l;
    }
    */

}

#2

Field:

private var onItemClickListener: OnitemClickListener?

Method for setting listener:

public fun setOnitemClickListener(listener: OnitemClickListener?){
    onItemClickListener = listener
}

Method call:

   onitemClickListener?.onClick()

#3

Why not

onitemClickListener?.onClick()

?


#4

Ahhh right! You are right, I don’t know why I write such stupid code (was thinking more in java than in kotlin). Will edit answer, thx