Issue
This is the code:
binding.commentsRecyclerview.setOnTouchListener { view, _ ->
Log.d(tagg, "touched")
}
It only works when I don't touch a view inside a RecyclerView item.
So how can I include just everything that is inside the RecyclerView?
Solution
I don't really understand whether you want to make the whole recyclerView view clickable or make to it's items clickable. So I describe both solutions below.
If you need to create touch listener for recyclerView item then do these steps.
FIRST CASE
- Create an interface
internal interface OnCommentTouchListener{
fun onClick(comment: Comment, position: Int) //or wherever you want
}
- Require an instance of interface in your adapter constructor
class CommentAdapter internal constructor(val onClickListener: OnCommentTouchListener)
- Apply it for holder.itemView in onBindViewHolder or onBind(for ListAdapter)
holder.itemView.setOnClickListener {
onClickListener.onClick(
comments.get(position),
position
)
}
- Use this adapter in your Fragment/Activity and override onClick from your listener the way you want to.
SECOND CASE
If you want to make the whole RecyclerView a clickable item, try to make a separate layout which includes only RecyclerView. As for example, FrameLayout with RecyclerView. Then include it
<include layout=... />
in your main layout and give it an id. All you have to do then is to call
binding.frameLayoutId.root.setOnClickListener {...}
Answered By - Yana Glad