In this article, I will show you how to set a implement click listener to Recycler View.
You’ll first need an interface that specifies listener’s behaviour. In this example, there is a sample model called ContentItem
, so the click will return an item of that type:
1 | public interface OnItemClickListener { |
2 | void onItemClick(ContentItem item); |
The constructor will receive an object that implements this interface, along with the items to be rendered:
1 | private final List<ContentItem> items; |
2 | private final OnItemClickListener listener; |
4 | public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) { |
6 | this .listener = listener; |
You could alternatively create a setOnItemClickListener
method and assign it that way. Now, in onBindViewHolder
the ViewHolder
will receive the constructor in the custom bind
method:
1 | @Override public void onBindViewHolder(ViewHolder holder, int position) { |
2 | holder.bind(items.get(position), listener); |
This is how this bind
method looks:
1 | public void bind( final ContentItem item, final OnItemClickListener listener) { |
3 | itemView.setOnClickListener( new View.OnClickListener() { |
4 | @Override public void onClick(View v) { |
5 | listener.onItemClick(item); |
Use it whenever you need it by creating a new adapter and the listener that will implement the behaviour when an item is clicked. A simple example:
1 | recycler.setAdapter( new ContentAdapter(items, new ContentAdapter.OnItemClickListener() { |
2 | @Override public void onItemClick(ContentItem item) { |
3 | Toast.makeText(getContext(), "Item Clicked" , Toast.LENGTH_LONG).show(); |
Take a look at the whole code of this adapter. Of course there are many alternatives here. In the end, the implementation is left to the developer when using RecyclerView
, so choose whatever fits better in your situation.
1 | public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> { |
3 | public interface OnItemClickListener { |
4 | void onItemClick(ContentItem item); |
7 | private final List<ContentItem> items; |
8 | private final OnItemClickListener listener; |
10 | public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) { |
12 | this .listener = listener; |
15 | @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
16 | View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item, parent, false ); |
17 | return new ViewHolder(v); |
20 | @Override public void onBindViewHolder(ViewHolder holder, int position) { |
21 | holder.bind(items.get(position), listener); |
24 | @Override public int getItemCount() { |
28 | static class ViewHolder extends RecyclerView.ViewHolder { |
30 | private TextView name; |
31 | private ImageView image; |
33 | public ViewHolder(View itemView) { |
35 | name = (TextView) itemView.findViewById(R.id.name); |
36 | image = (ImageView) itemView.findViewById(R.id.image); |
39 | public void bind( final ContentItem item, final OnItemClickListener listener) { |
40 | name.setText(item.name); |
41 | Picasso.with(itemView.getContext()).load(item.imageUrl).into(image); |
42 | itemView.setOnClickListener( new View.OnClickListener() { |
43 | @Override public void onClick(View v) { |
44 | listener.onItemClick(item); |
Good luck!
0 comments:
Post a Comment