简体   繁体   English

Android MVVM RecyclerView ClickListener Kotlin 到 Java 的转换

[英]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.我正在尝试在 Android RecyclerView 中实现一个 clickListener,带有片段、数据绑定、ViewModel/LiveData,我的代码是根据 Udacity 的“使用 Kotlin 开发 Android 应用程序”课程中的实践构建的。 However, my project is in Java as all the backend is already written.但是,我的项目是用 Java 编写的,因为所有后端都已经编写好了。 I don't know enough Kotlin and can not work out how to implement the following in Java!我对 Kotlin 了解得不够多,无法弄清楚如何在 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.我在网上看过其他人在 Java 中实现了这种类型的结构,但它总是以不同的方式完成,这会导致我的 ViewAdapter 的结构方式出现问题。

Thanks in advance.提前致谢。

So, the Kotlin snipped you have posted in your question can be implemented in Java as:因此,您在问题中发布的Kotlin片段可以在Java实现为:

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());    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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