Mahdi.Pishguy Mahdi.Pishguy - 3 years ago 157
Android Question

equivalent implementing java interface on kotlin

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

}

Answer Source
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:OnClickedItemListenr){
  this.onClickItemListener=onclickItemListener
  }

}

use setOnClickItemListener in your activity or ....

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download