Home > Net >  How to move click item RecycelerView in MainActivity
How to move click item RecycelerView in MainActivity

Time:02-23

this my adapter When I press it, the value appears correctly, how to make the code in main Activity on create

So I can take the value in main activity and add it code.

public void onBindViewHolder(CustomViewHolder holder, int position) {
        Glide
                .with(mActivity)
               // .asGif()

                .load((android_image_urls[position]))
                .override(200, 200)
                .centerCrop()
                .into(holder.imageResource);

        final int itemPosition = holder.getBindingAdapterPosition();
        holder.imageResource.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




          Toast.makeText(mActivity, android_image_urls[itemPosition], Toast.LENGTH_SHORT).show();




            }
        });

    }

CodePudding user response:

First Make One interface class as name ButtonClick

public interface ButtonClick {
    void onClick(int pos,String s);
}

Call in Adapter Constructor As Below

 ButtonClick buttonclicked;

 public Adapter(Context context, ..., ButtonClick buttonclicked) {
        this.context = context;
        ...
        this.buttonclicked = buttonclicked;
    }

In onBindViewHolder Method Just Put setOnClickListener As Below

holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonclicked.onClick(pos, "click");
            }
        });

And In Main Activity Just Add

public class classname implements ButtonClick{
...
...
 @Override
    public void onClick(int pos, String s) {
        // You Take Anything From This
    }
}

In OnClick Method You get All Data At Clicked Position

CodePudding user response:

Use interface for it

check out this...

How to add Onclick listener to recycler view

  • Related