https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

trubesv

08/26/2017, 7:15 PM
Hi all! I'm having trouble calling a function from a
Coroutine
that is inside a custom
ViewHolder
The function is not recognised, and when I put it in the
ViewHolder
class, of course I can't calll the `add`/`remove` functions anymore.. Could you point me in the right direction please? Here is the basic setup:
Copy code
class MyAdapter() : RecyclerView.Adapter<MyViewHolder>()
{
    fun add() {}
    fun remove() {}
    [...]

    /* Custom ViewHolder */
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    {
        fun bind(item: Item) = with(itemView)
        {
            /* Handle long click event on row*/
            isLongClickable = true
            onLongClick { function() } // I'M HAVING TROUBLE HERE (Unresolved reference)
       
            [...]
        }
    }

    /* How to call this? */
    fun function() {
        add()
        remove()
    }
Thanks for the help! 🙂
v

vlastachu

08/27/2017, 10:29 AM
Where is any
Coroutine
-related things?
r

radityagumay

08/27/2017, 12:10 PM
@trubesv where you couroutine? Could you paste you code in online editor? So I can see full code.
t

trubesv

08/27/2017, 12:37 PM
@vlastachu @radityagumay Sorry I might have used the wrong term, the `onLongClick`seems to be considered as a
CoroutineScope
, IntelliJ suggests me to create an extension function it so I can call
function
, like
fun CoroutineScope.function() {}
k

kevinherron

08/27/2017, 1:37 PM
@trubesv make
MyViewHolder
an
inner class
t

trubesv

08/27/2017, 1:39 PM
@kevinherron Wow ty good sir! I'll look more into this concept, thanks 🙂
in Java that would have been an inner class by default. in Kotlin it needs to be made inner explicitly
t

trubesv

08/27/2017, 1:45 PM
I'll keep that in mind! Thanks for the explanation.
3 Views