简体   繁体   中英

Android MVVM RecyclerView ClickListener Kotlin to Java Conversion

I'm trying to implement a clickListener in an Android RecyclerView, with fragments, data binding, ViewModel/LiveData, and my code has been constructed based on practices in the Udacity 'Developing Android Apps with Kotlin' course. However, my project is in Java as all the backend is already written. I don't know enough Kotlin and can not work out how to implement the following in Java!

class SleepNightListener(val clickListener: (sleepId: Long) -> Unit)
    fun onClick(night: SleepNight) = clickListener(night.nightId)
}

I have looked online at other people who've implemented this type of structure in Java, but it's always been done in a different way that causes problems with the way my ViewAdapter is structured.

Thanks in advance.

So, the Kotlin snipped you have posted in your question can be implemented in Java as:

interface ClickListener {
    void onCLick(Long sleepId);
}


class SleepNightListener {
    private ClickListener clickListener;

    public SleepNightListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void onClick(SleepNight night) {
        if (clickListener != null) clickListener.onCLick(night.getNightId());    
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM