Reputation: 859
I am using this library for general adapter -> Smart-Recycler-Adapter
This is my code
SmartRecyclerAdapter
.items(listItems)
.map(Header::class, HeaderViewHolder::class)
.map(KeyVal::class, KeyValViewHolder::class)
.map(KeyLink::class, KeyLinkViewHolder::class)
.map(Picture::class, PictureViewHolder::class)
.add(StickyHeaderItemDecorationExtension(
headerItemType = Header::class
))
.add(OnCustomViewEventListener { event ->
showToast(event)
})
.into<SmartRecyclerAdapter>(binding.recyclerview)
I am able to add click listener to the whole item of recyclerview. But I want to add click listener on a child view of one of my view-holder. I have done this many times on my own custom adapters but I don't know how to add this functionality by using this library. Thankyou
Upvotes: 2
Views: 414
Reputation: 444
In the recycler adapter:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
public TextView text_view_1;
public TextView text_view_2;
public ViewHolder(View itemView)
{
super(itemView);
text_view_1 = (TextView)itemView.findViewById(R.id.text_view_in_layout_1);
text_view_2 = (TextView)itemView.findViewById(R.id.text_view_in_layout_2);
text_view_1.setOnClickListener(this);
text_view_2.setOnClickListener(this);
}
}
In the activity:
recyclerViewClickListener = new TheNameOfYourRecyclerAdapter.RecyclerViewClickListener()
{
@Override
public void recyclerViewListClicked(View view, int position)
{
if (view.getId() == findViewById(R.id.text_view_in_layout_1).getId())
{
// Do something here based on text_view_1
}
else if (view.getId() == findViewById(R.id.text_view_in_layout_2).getId())
{
// Do something here based on text_view_2
}
}
}
Upvotes: 0
Reputation: 180
You can add listeners to the itemView (View of the particular item of the list) in the respective Viewholders.
For example, in your PictureViewHolder class, you must have an instance of view which represents the view of the individual item of the list. You can just set on click listener to this view, and do whatever you wish to perform on click.
For instance, if you Viewholder class looks something like this
inner class PictureViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
fun bind(item: PictureClass) {
itemView.setOnClickListener {
// Do something
}
}
}
Upvotes: 1