Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclicklistener on the specific item of the recyclerview in android

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.

like image 418
Sarvesh Avatar asked Apr 21 '16 04:04

Sarvesh


People also ask

Where do you add the Android onClick attribute to make items in a RecyclerView respond to clicks?

Where do you add the android:onClick attribute to make items in a RecyclerView respond to clicks? In the layout file that displays the RecyclerView, add it to the element. Add it to the layout file for an item in the row.

What is the difference between onCreateViewHolder and onBindViewHolder?

This method internally calls onBindViewHolder to update the ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView. This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView.


1 Answers

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()

like image 100
Rohitashv jain Avatar answered Nov 08 '22 15:11

Rohitashv jain