inner
class
CommentViewHolder(
private
val binding: ItemCommentBinding,
private
var onCommentClicked: ((Comment) -> Unit)? =
null
) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Comment) {
...
likeBtn.setOnClickListener {
onCommentClicked?.invoke(item)
}
}
}
class
ReplyAdapter: RecyclerView.Adapter<CommentViewHolder> {
var onCommentClicked: ((Comment) -> Unit)? =
null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentViewHolder {
val binding = ....
return
CommentViewHolder(binding, onCommentClicked)
}
}
private
val replyListAdapter: ReplyAdapter
...
override fun onCreate (savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
...
replyListAdapter = ....
replyListAdapter.onCommentClicked = { comment ->
viewModel.commentClicked(comment)
}
viewModel.comments.observe(
this
, ::bindComments)
}
private
fun bindComments(comments: List<Comment>) {
replyListAdapter.setItems(comments);
}