0

This is RecyclerView Holder class to get the image from RecyclerView item clicked.

class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
   ImageView gAvatar_photo;
   View layoutView;

    public RecyclerViewHolders(View layoutView) {
        super(layoutView);
        this.layoutView=layoutView;
        layoutView.setOnClickListener(this);
        gAvatar_photo=(ImageView) layoutView.findViewById(R.id.gAvatar_photo);
        gAvatar_photo.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
       if(v.getId()==R.id.gAvatar_photo) {
           Integer img = BoyAvatarAdapter.url[getAdapterPosition()];
           SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
           SharedPreferences.Editor editor = sharedPreferences.edit();
           editor.putInt("imgRes", img);
           editor.apply();
           Intent intent = new Intent(v.getContext(), ProfileActivity.class);
           ProfileActivity pa = new ProfileActivity();
           pa.avatar(img);
           v.getContext().startActivity(intent);
           Toast.makeText(v.getContext(), "" + img, Toast.LENGTH_SHORT).show();
       }

    }
}
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
vsking
  • 1
  • 3
  • 1
    Check this https://stackoverflow.com/questions/50127376/view-full-screen-image-when-item-from-recycler-view-clicks/50127439#50127439 – AskNilesh Aug 14 '18 at 05:18
  • create an interface in your adapter and call it fro wherever you want to and get the int out of it [if u dont get it leme know i ll post some code] – Harkal Aug 14 '18 at 05:26

3 Answers3

0

You can pass url to the ProfileActivity and get that extra in your ProfileActivity.

Intent intent = new Intent(v.getContext(), ProfileActivity.class);
intent.putExtra("image", BoyAvatarAdapter.url[getAdapterPosition()];); 
v.getContext().startActivity(intent);

And get passed resource id in ProfileActivity onCreate()

int image = getIntent().getIntExtra("image", 0); // or use getStringExtra() if you have url instead of resource id.
if(image!=0){
 //  setImageHere.
}
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

Here is your adapter

abstract public class Adapter extends RecyclerView.Adapter<Adapter.RecyclerViewHolders>{

       abstract void onItemClicked(Your data to pass);

       class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
               ImageView gAvatar_photo;
               View layoutView;

               public RecyclerViewHolders(View layoutView) {
                super(layoutView);
                ...
                @Override
                public void onClick(View v) {
                     if(v.getId()==R.id.gAvatar_photo)
                     // HERE IS THE MAGIC
                     onItemClicked(BoyAvatarAdapter.url[getAdapterPosition()])
                }
...
        }

And the activity part here that catches your click events

public class Dummy extends AppcompatActivity{

    ...
    RecyclerAdapter adapter = new RecyclerAdapter(){

    @Override
    public void onItemClicked(Your data){
        //Now got your data. You can do what do you want with it.
    }

}

Hope it will helps you. Cheers

0

try it : i have Adapter get Image from ImageVIew and send ImageView to Other Fragment i u need to Other Activity just Dont use getArgument(); or you can search about passing data to intent

in Your holder onclick() add this code

        Drawable myDrawable = mImageView.getDrawable();
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) myDrawable);
        mBitmap = bitmapDrawable.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageBytes = stream.toByteArray();
        mBundle.putByteArray("ResID", imageBytes);


        Fragment fragment = new Edit_Profile_Fragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.addToBackStack(null);
        fragment.setArguments(mBundle);
        fragmentTransaction.commit();

and Use to Other class acitivty/fragment

    byte[] byteArray = getArguments().getByteArray("ResID");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    mImage_profile.setImageBitmap(bmp);   

it will work as well