Onclicklistener on specific recyclerview element in android

I will ask a very simple question, but I have been stuck in it for a long time.

after viewing the map there is a recycleview that has 2 images in each row. Now I want to create a clip listener on images, not a recycleview.

the corresponding layout (layout_main.xml) of this action (MainActivity.java) contains only recyclerview. the elements of each row are in a different layout (layout_images.xml). I get images from layout_images.xml and inflate them in the adapter class (Adapter.java).

Now, how to set the action performer only on images.

Secondly, I want to get the image I clicked on. how to get it. for example, when we click on a view, we create some kind of method like

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

where view is the object we clicked on. in my case, how to get the image I clicked on. using type casting, this may be an exception if the user does not click on the image.

+6
source share
6 answers

Several onClick events inside 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 find out which element was clicked, you match the view identifier ievgetId () == yourViewItem.getId ()

+6
source

You must set the onClickListener for the ImageView inside the onBindViewHolder method, refer to the following LOCs for reference (the code should be inside the 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 } }); 
+4
source

implement View.OnClickListener in your ViewHolder class and implement the onClick method. Then install the click listener for your ImageView on this click listener. Add the necessary functions to the onClick method. If you want to implement the click function in another class, just 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 onClick view method, you can call the interface method.

+1
source

In the Recycle View Holder , write your onclick listen code inside

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

You can check with a tag or its element as follows:

 public void onClick(View view){ if(view.getId() == image1.getId()) { }else if(view.getId() == image2.getId()) {} } 
0
source

First of all, set onclickListener for each view you want to click on. A good place to do this is in the viewHolderConstructor. eg,

  public class GalleryManyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.im_img) RoundedCornersImageView imImg; @BindView(R.id.tv_title) MyTextView tvTitle; @BindView(R.id.tv_label) MyTextView tvLabel; @BindView(R.id.tv_date) MyTextView tvDate; @BindView(R.id.im_gallery_one) RoundedCornersImageView imGalleryOne; @BindView(R.id.im_gallery_two) RoundedCornersImageView imGalleryTwo; @BindView(R.id.im_gallery_three) RoundedCornersImageView imGalleryThree; @BindView(R.id.im_gallery_four) RoundedCornersImageView imGalleryFour; @BindView(R.id.tv_more) MyTextView tvMore; @BindView(R.id.root) RelativeLayout root; public GalleryManyViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(onClickListener); imGalleryOne.setOnClickListener(onClickListener); imGalleryTwo.setOnClickListener(onClickListener); imGalleryThree.setOnClickListener(onClickListener); imGalleryFour.setOnClickListener(onClickListener); view.setTag(this); } 

As a rule, you don’t need to do anything specific with this view, for example, set tags (also some useful libraries, such as Glied, which set their own tags in images, will not work correctly if you set your own tag. clickListener find the position of the adapter view to be able to retrieve the corresponding data

 View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { View view = v; View parent = (View) v.getParent(); while (!(parent instanceof RecyclerView)){ view=parent; parent = (View) parent.getParent(); } int position = recyclerView.getChildAdapterPosition(view); } 

as described here Then, but by checking the id, evaluate what you want to do

 switch (v.getId()) { case R.id.im_gallery_one: { p = 0; } break; case R.id.im_gallery_two: { p = 1; } break; case R.id.im_gallery_three: { p = 2; } break; case R.id.im_gallery_four: { p = 3; } break; } 
0
source

Source: https://habr.com/ru/post/1247575/


All Articles