A RecyclerView is different from a ListView because it doesn't offer an onItemCLickListener class to handle click events.
This can be tackled if not a lot is happening behind the scenes when an item is clicked, by defining an onClickListener in the ViewHolder.
What if the text or whatever that has to be passed to the next Activity doesnt really exist in the views that the ViewHolder has access to, but is rather a part of the Activity which has the RecyclerView?
In that case, the onClickListener must be implemented inside the activity so that the text can be forwarded. It is possible.
One way is to add an Invisible View holding that text, and then doing what has been done before; implementing onClickListener in the Adapter.
Pass that text, somehow, to the Adapter.
How can "2." be implemented?
1. We will need a listener interface. 2. In your ViewHolder class in your RecyclerView adapter, implement View.OnClickListener, bind the listener to the view. In the onClick method, call the onClick method of the interface OnItemClickListener.
However, in RecyclerView the onBindViewHolder gets called every time the ViewHolder is bound and the setOnClickListener will be triggered too. Therefore, setting a click listener in onCreateViewHolder which invokes only when a ViewHolder gets created is preferable.
However, in RecyclerView the onBindViewHolder gets called every time the ViewHolder is bound and the setOnClickListener will be triggered too. Therefore, setting a click listener in onCreateViewHolder which invokes only when a ViewHolder gets created is preferable. Here is a diagram of the implementation. A diagram of the implementation.
Update the ViewHolder to take in onClick () as a parameter. In the initializer, call setOnClickListener {} on the itemView. That’s it! Your RecyclerView is now responsive so time to get your click on! Happy coding! The full code sample including onClick () can be found here.
See the example below to create RecyclerView
OnClick
.
Example Code:
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);
}
});
}
}
}
And Use RecyclerView
Adapter using below code:
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
@Override public void onItemClick(ContentItem item) {
Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
See Set a click listener to a RecyclerView
Hope it helped you.
Write a public function in your activity like:
public void onClickCalled(String anyValue) {
// Call another acitivty here and pass some arguments to it.
}
Now in your adapter's onClick funciton
@Override
public void onClick(View view) {
((YouActivityClass) context).onClickCalled("your argument here");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With