簡體   English   中英

請幫助我理解 kotlin 的代碼行

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

這是界面

interface Callback {
  fun onFilterSelect(filter: Filter)
}

適配器

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

請幫助我理解這行代碼

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

我想在我的 java 項目中使用這個 kotlin 代碼

您可以在文檔中閱讀相關內容。 有一種約定,您應該在括號外指定 lambda。

此 kotlin 代碼的 Java8 代碼如下所示:

這是界面

interface Callback {
    void onFilterSelect(Filter filter);
}

適配器

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

    private FilterAdapter() {};

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

最后初始化適配器:

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