DolDurma
DolDurma

Reputation: 17321

Kotlin add custom listener for clicked on widgets on android

I'm newbie in Kotiln and this code is simple class which I'm using on Android. In this class I want to make simple listener to detect clicks on item inside of a class such as activity, fragment or adapter. My problem is that I can define this listener for circularProgressView widget.

class DownloadProgressView : RelativeLayout {

    var progressListener: ((downloading: Boolean) -> Unit)? = null
    private var downloading = false

    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)
        circularProgressView.setOnClickListener {
            downloading = !downloading
            setProgressViews(downloading)
            progressListener?.invoke(downloading)

            //if listener!=null then onItemClicked.onClick();
        }
    }
}

How can I know if user clicked on circularProgressView inside activity?

I'm trying to implement this interface and use it inside Kotlin:

public interface OnItemClickListener {
    void onClick(int position);
}

Upvotes: 0

Views: 6860

Answers (1)

Vova
Vova

Reputation: 1091

For init progreessListener use following code

var listener: () -> Unit = {}

For call onClick use : listener()

For implement your interface use following code :

var onItemClickInterface : () -> Unit = {
  //TODO
}

Upvotes: 6

Related Questions