0

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;
    }
    */

}

1 Answer 1

0

use setOnClickItemListener in your Activity.

class MyProgressView : RelativeLayout, OnClickedItemListener  {

    var onClickItemListener: OnClickedItemListener? = null

    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)
    }

    override fun onClick(state: Boolean) {
        //do something on onclick
    }

    fun setOnClickedItemListener(onclickItemListener: OnClickedItemListener) {
        this.onClickItemListener = onclickItemListener
    }

}

I hope this may help you.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, now how can i use this implemented listener inside activity? for example: MyProgressView.setOnitemClickListener(new OnitemClickListener(){}};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.