简体   繁体   English

请帮助我理解 kotlin 的代码行

[英]Please help me to understand line of code of kotlin

This Is Interface这是界面

interface Callback {
  fun onFilterSelect(filter: Filter)
}

Adapter适配器

class FilterAdapter(
    private val context: Context, 
    val callback: (filter: Filter)->Unit
) : RecyclerView.Adapter<FilterAdapter.ViewHolder>() {}

Please help me to understand this line of code请帮助我理解这行代码

recyclerView.adapter = FilterAdapter(view.context) {
  mCallback?.onFilterSelect(it)
}

I want to use this kotlin code in my java project我想在我的 java 项目中使用这个 kotlin 代码

You can read about this in the documentation .您可以在文档中阅读相关内容。 There is a convention that you should specify a lambda outside of parentheses.有一种约定,您应该在括号外指定 lambda。

Java8 code for this kotlin code can be like as following:此 kotlin 代码的 Java8 代码如下所示:

This is interface这是界面

interface Callback {
    void onFilterSelect(Filter filter);
}

Adapter适配器

class FilterAdapter extends RecyclerView.Adapter<FilterAdapter.ViewHolder> {

    private FilterAdapter() {};

    FilterAdapter(Context context, Function<Filter, Void> callback) {
        // do your stuff
    }
}

And finally initialize adapter:最后初始化适配器:

recyclerView.adapter = new FilterAdapter(context, filter -> {
        callback.onFilterSelect(filter);
        return null;
    });

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

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