Monday, May 29, 2017

How to implement click listener to Recycler View

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:
1public interface OnItemClickListener {
2    void onItemClick(ContentItem item);
3}
The constructor will receive an object that implements this interface, along with the items to be rendered:
1private final List<ContentItem> items;
2private final OnItemClickListener listener;
3 
4public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
5    this.items = items;
6    this.listener = listener;
7}
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);
3}
This is how this bind method looks:
1public void bind(final ContentItem item, final OnItemClickListener listener) {
2    ...
3    itemView.setOnClickListener(new View.OnClickListener() {
4        @Override public void onClick(View v) {
5            listener.onItemClick(item);
6        }
7    });
8}
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:
1recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
2    @Override public void onItemClick(ContentItem item) {
3        Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
4    }
5}));
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.
1public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
2 
3    public interface OnItemClickListener {
4        void onItemClick(ContentItem item);
5    }
6 
7    private final List<ContentItem> items;
8    private final OnItemClickListener listener;
9 
10    public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
11        this.items = items;
12        this.listener = listener;
13    }
14 
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);
18    }
19 
20    @Override public void onBindViewHolder(ViewHolder holder, int position) {
21        holder.bind(items.get(position), listener);
22    }
23 
24    @Override public int getItemCount() {
25        return items.size();
26    }
27 
28    static class ViewHolder extends RecyclerView.ViewHolder {
29 
30        private TextView name;
31        private ImageView image;
32 
33        public ViewHolder(View itemView) {
34            super(itemView);
35            name = (TextView) itemView.findViewById(R.id.name);
36            image = (ImageView) itemView.findViewById(R.id.image);
37        }
38 
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);
45                }
46            });
47        }
48    }
49}
Good luck!

Share:

0 comments:

Post a Comment

Total Pageviews