You are on page 1of 1

Let's do that. Open ListSelectionRecyclerViewAdapter.

kt and add the following


new interface above onCreateViewHolder:

In the class declaration above that, update the constructor to allow passing in a
ListSelectionRecyclerViewClickListener:

class ListSelectionRecyclerViewAdapter(val lists:


MutableList<TaskList>, val clickListener:
ListSelectionRecyclerViewClickListener) :
RecyclerView.Adapter<ListSelectionViewHolder>() {

Finally, edit onBindViewHolder to add an onClickListener to the View of


itemHolder:

override fun onBindViewHolder(holder: ListSelectionViewHolder,


position: Int) {
holder.binding.itemNumber.text = (position + 1).toString()
holder.binding.itemString.text = lists[position].name
holder.itemView.setOnClickListener {
clickListener.listItemClicked(lists[position])
}
}

Open MainFragment.kt and update the class declaration to state that it conforms to
the ListSelectionRecyclerViewClickListener interface you just created:

class MainFragment : Fragment(),


ListSelectionRecyclerViewAdapter.ListSelectionRecyclerViewClickL
istener {

Then, at the bottom of the class, implement the method to conform to the interface:

override fun listItemClicked(list: TaskList) {


clickListener.listItemTapped(list)
}

Now, whenever a tap happens on a list item in the recyclerView, the Fragment is
informed about it and calls clickListener.listItemTapped(), passing in the list
the user taps on. Now pass in our new
ListSelectionRecyclerViewClickListener() (MainFragment) to the adapter. Go
to onActivityCreated() and update the adapter creation to:

val recyclerViewAdapter =

You might also like