Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting anonymous interface in Kotlin [duplicate]

I have an interface in my Adapter class:

interface OnItemClickListener {
    fun onItemClick(view: View)
}

TAKE NOTE that my interface is in Kotlin.

which I want to set via this method:

public fun setItemClickListener(itemClickListener: OnItemClickListener) {
    this.onItemClickListener = itemClickListener
}

How do I use the setItemClickListener method in my Fragment class like what I used to do in Java? i.e.

adapter.setItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(View view) {
    // do something
   }
}

I know that I can use an onItemTouchListener but I am still new to Kotlin and I'm trying to learn some techniques like Lambdas and such.

like image 696
Dale Julian Avatar asked Jun 19 '18 06:06

Dale Julian


1 Answers

Without lambda you can do it like this

adapter.setItemClickListener(object :View.OnItemClickListener{
            override fun onItemClick(p0: View?) {

            }
        })
like image 138
theanilpaudel Avatar answered Oct 04 '22 23:10

theanilpaudel