onclicklistener on the specific item of the recyclerview in android

18,156

Solution 1

Multiple onClick events inside a recyclerView:

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public ImageView iconImageView;
    public TextView iconTextView;

    public MyViewHolder(final View itemView) {
        super(itemView);

        iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
        iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);
        // set click event
        itemView.setOnClickListener(this);
        iconTextView.setOnClickListener(this);
        // set long click event
        iconImageView.setOnLongClickListener(this);
    }

    // onClick Listener for view
    @Override
    public void onClick(View v) {    
        if (v.getId() == iconTextView.getId()) {
            Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
        }
    }


    //onLongClickListener for view
    @Override
    public boolean onLongClick(View v) {    
        final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
        builder.setTitle("Hello Dialog")
            .setMessage("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition()))
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

        builder.create().show();
        return true;
    }
}

To get which item was clicked you match the view id i.e. v.getId() == yourViewItem.getId()

Solution 2

You have to set onClickListener to the ImageViews inside the onBindViewHolder method, refer the following LOCs for reference(code to be inside onBindViewHolder method)

holder.imageView1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //put your code for first imageview here
    }
});
holder.imageView2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //put your code for second imageView here
    }
});

Solution 3

In Recycle View Holder, Write your onclick listener code inside

    @Override
    public void onBindViewHolder(CardHolder holder, final int position) {
       holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //TODO
            }
        }
   }

Solution 4

implement the View.OnClickListener in your ViewHolder class and implement the onClick method. Then set the click listener for your ImageView to this click listener. Add the required functionality in the onClick method. If you want to implement the click functionality in other class simply create an interface and declare a click method in it. You can implement this method in the activity/fragment that contains this RecycleView. Then from your view holders onClick method you can invoke the interface method.

Share:
18,156
Sarvesh
Author by

Sarvesh

Updated on June 17, 2022

Comments

  • Sarvesh
    Sarvesh almost 2 years

    I am going to ask very basic question, but i am stuck in it for a long time.

    after card view there is an recycleview which has 2 images in each row. now i want to create the click listener on the images rather than the recycleview.

    the corresponding layout(layout_main.xml) of this activity(MainActivity.java) contain only recyclerview. the elements of each row is in another layout(layout_images.xml). i am getting the images from layout_images.xml and inflate them in the adapter class(Adapter.java).

    now how to put action listener on the images only.

    secondly, i want to get the image on which i clicked. how to get that. like, when we click on a view we create some method as

    public void onClick(View view){
        // some code here
    }
    

    where view is the object on which we clicked. in my case how to get the image on which i clicked. using type cast it might be throw an exception when user doesnot click on image.

  • Sarvesh
    Sarvesh about 8 years
    for v.getId() == yourViewItem.getId() can i interpret yourViewItem as R.id.image1? or i have to use findViewById() and then use this variable for id matching ImageView im = (ImageVIew) findViewById(R.id.image1); im.getId() == v.getId or v.getId == R.id.image1 which one is correct? is there any difference between both?
  • Rohitashv jain
    Rohitashv jain about 8 years
    compare ID like R.id.yourView , it is an integer value when click event is occur we can find this value v.getId() method. v.getId == R.id.image1 (use this)
  • Moonis Abidi
    Moonis Abidi almost 6 years
    In adapter there are two ways of doing it either directly setting onclick in onBindViewHolder(...) like holder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { }); or Implementing Item Click Listener by MyViewHolderClass (as in this post)