简体   繁体   中英

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

You can read about this in the documentation . There is a convention that you should specify a lambda outside of parentheses.

Java8 code for this kotlin code can be like as following:

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

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