I use RecyclerView
adapter to display data inside an activity, I want to implement onClickListener
inside the activity, currently, I am setting onClickListener
inside adapter as usual which works fine.
public void onBindViewHolder(MyHolder holder, final int position) { final Listdata data = listdata.get(position); holder.vname.setText(data.getName()); holder.vname.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(activity, "clicked on " +position, Toast.LENGTH_SHORT).show(); } }); }
However I want to implement it inside activity so I have greater control. This doesn’t serve my purpose. I think it’ll be useful for a lot of us.
Advertisement
Answer
You need to check this tutorial here for better understanding on how you can achieve the behaviour that you want.
In case of handling the onClickListener
from your activity you need to work based on a callback implementation with an interface. Pass the interface from the activity to your adapter and then call the callback function from your adapter when some items are clicked.
Here’s a sample implementation from the tutorial.
Let us first have the interface.
public interface OnItemClickListener { void onItemClick(ContentItem item); }
You need to modify your adapter to take the listener as the parameter like the one stated below.
private final List<ContentItem> items; private final OnItemClickListener listener; public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) { this.items = items; this.listener = listener; }
Now in your onBindViewHolder
method, set the click listener.
@Override public void onBindViewHolder(ViewHolder holder, int position) { holder.bind(items.get(position), listener); } public void bind(final ContentItem item, final OnItemClickListener listener) { ... itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onItemClick(item); } }); }
Now setting the adapter in your RecyclerView
.
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() { @Override public void onItemClick(ContentItem item) { Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show(); } }));
So the whole adapter code looks like the following.
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> { public interface OnItemClickListener { void onItemClick(ContentItem item); } private final List<ContentItem> items; private final OnItemClickListener listener; public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) { this.items = items; this.listener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.bind(items.get(position), listener); } @Override public int getItemCount() { return items.size(); } static class ViewHolder extends RecyclerView.ViewHolder { private TextView name; private ImageView image; public ViewHolder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.name); image = (ImageView) itemView.findViewById(R.id.image); } public void bind(final ContentItem item, final OnItemClickListener listener) { name.setText(item.name); Picasso.with(itemView.getContext()).load(item.imageUrl).into(image); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onItemClick(item); } }); } } }