I am confuse about what kind of parameter should I pass from the main class to the recycler adapter class that have parameter type like this
class RecyclerAdapter(private val clickListener:(view: View, movieName: String)
->Unit):ListAdapter<MovieInfo,RecyclerAdapter.ViewHolder>(Diff())
In the main class, my plan is to declare the adapter like below
val adapter = RecyclerAdapter() //missing parameter inside the ()
I have never seen a class parameter like that before. Usually, it is a starightforward list data type such as RecyclerAdapter(private val dressList: List<DressList.DressName>)
. Maybe if I know some examples about that kind of parameter, it can give me some enlightenment about what parameter should I pass. Thanks.
Just to break this down
val name: String
That's simple enough - the variable is called name, and its type is String
. If that's a parameter, you need to pass a String
there.
val clickListener: () -> Unit
The type here is () -> Unit
, which is a function. You know how you call a function by putting ()
after the name, like toString()
? That's what that represents in the type, a function that takes no parameters. The ->
is showing the type it produces (or returns if you like). This is a function that takes no parameters, and produces no value (aka Unit
).
val clickListener: (view: View, movieName: String) -> Unit
// or more typically with just the types
val clickListener: (View, String) -> Unit
This is similar, except this type is a function that does take parameters - a View
and a String
, and returns nothing. When you call this function, you're expected to pass those values into it, and the function does something with them.
The setup you have here is basically an Adapter that's displaying MovieInfo
objects, and in the constructor you pass in a listener function. So when the user clicks an item, that function gets called with the relevant values. It means a component outside of the adapter can decide what actually happens when something gets clicked, by providing a function that does the necessary stuff. The adapter doesn't need to know any of those details - it just needs to pass the listener function the data it requires.
I don't know what you're doing with this:
val clickListener = movieList.fold(Unit, { view, movieName -> Unit })
but if you hover over clickListener
you'll see its inferred type is Unit
. This isn't a function object, this is the result of calling the fold
function. You've run fold
over your movieList
, basically returning Unit
at each step, and the result is Unit
. That's what you're trying to pass as your clickListener
parameter.
A function would be in { }
braces (or a function reference like ::handleClick
), like this:
val clickListener = { view, movieName -> // do stuff that returns unit }
and if you really wanted to do a fold for some reason, you can do it in there! But it's usually a function that does something like display a movie, add it to some list or a database, that kind of thing. An action in response to a click