Amrutha
Amrutha

Reputation: 575

Recyler view listitem image onclick

I am working on xamarin android apps. I used recycler view for displaying the images in each row. Now for each item image there is some link which I need to redirect. How can I achieve this onclick action for each image in recycler view. Please help me in this regard. Thanks

Upvotes: 0

Views: 428

Answers (1)

ryguy
ryguy

Reputation: 481

When creating a recycler view, you need to create a RecyclerView adapter which (among other things) implements methods for creating and binding a viewholder to the item in the recycler view. Somewhere in your code (oftentimes within this recycler view adapter class), you need to define the viewholder that you will use for your recyclerview items. This is where you should assign the onClickListener to your imageView.

Here is an example of a viewholder definition that I think may help you:

public class YourViewHolder extends RecyclerView.ViewHolder {

    protected ImageView yourImage;

    public YourViewHolder(View v) {
        super(v);
        final View theView = v;

        yourImage = (ImageView) v.findViewById(R.id.yourImage);

        yourImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // image clicked... do something

            }
        });       
    }
}

Let me know if you need more information on how to set up the recycler view adapter class (I have assumed that you already have done this, but I could be mistaken).

Upvotes: 2

Related Questions