如何将图像或路径从Recyclerview传递到下一个活动onClick,以便我可以显示图像全视图?

时间:2021-10-22 21:20:37

I really don't know how to do this. I have tried some tutorial but not worked for me ... i've already done the this with many Methods but unable to do this.

我真的不知道该怎么做。我已经尝试了一些教程,但没有为我工作...我已经用很多方法完成了这个但是无法做到这一点。

Here is my RecyclerView Holder

这是我的RecyclerView Holder

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

public TextView countryName;
public ImageView countryPhoto;
String s;
Bitmap image;

public RecyclerViewHolders(View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    countryPhoto = (ImageView)itemView.findViewById(R.id.country_photo);
}

@Override    public void onClick(View view) {
    Intent imageIntent  = new Intent(view.getContext(),GalleryFullImage.class);
    imageIntent.putExtra("id",getAdapterPosition());
    view.getContext().startActivity(imageIntent);
    Toast.makeText(view.getContext(), "Clicked Country Position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}}

Here is my Recyclerview Adapter

这是我的Recyclerview适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

private ArrayList<String> arrayList;
private Context context;

public RecyclerViewAdapter(Context context, ArrayList<String> itemList) {
    this.arrayList = itemList;
    this.context = context;
}

@Override
public long getItemId(int position) {
    return position;
}



@Override    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_item, null);
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
    return rcv;
}

@Override    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
    Bitmap bitmap = BitmapFactory.decodeFile(arrayList.get(position));
    holder.countryPhoto.setImageBitmap(bitmap);

}

@Override    public int getItemCount() {
    return this.arrayList.size();
}}

Here is my GalleryActivity

这是我的GalleryActivity

public class GalleryActivity extends AppCompatActivity {
ArrayList<String> stringList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activty_gallery);
    setTitle("Gallery");

    stringList = new ArrayList<>();
    // Be sure that this file is exist !!

    String targetPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Country";
    File targetDirector = new File(targetPath);
    File[] files = targetDirector.listFiles();
    try
    {
        Arrays.sort( files, new Comparator()
        {
            public int compare(Object o1, Object o2) {

                if (((File)o1).lastModified() > ((File)o2).lastModified()) {
                    return -1;
                } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
                    return +1;
                } else {
                    return 0;
                }
            }

        });
        for (File file : files) {
        stringList.add(file.getAbsolutePath());
    }
    }catch (Exception e)
    {
        Log.e("Sort Error", e.getMessage());
    }

    RecyclerView rView = (RecyclerView)findViewById(R.id.my_recycler_view);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
    rView.setHasFixedSize(true);
    rView.setLayoutManager(layoutManager);
    RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getApplicationContext(), stringList);
    rView.setAdapter(rcAdapter);


}

}

2 个解决方案

#1


0  

Since you are loading the bitmap from file in onBindViewHolder, you can just pass the path instead in onClick and decode it again in your next activity. This may require a bit of work in your case since your ViewHolder is defined separately from your RecyclerView so it does not have access to your dataset. For starters, move your ViewHolder so it can be a static nested class of your RecyclerView. Something like this:

由于您是在onBindViewHolder中从文件加载位图,因此您只需在onClick中传递路径,然后在下一个活动中再次对其进行解码。这可能需要您的案例中的一些工作,因为您的ViewHolder是与RecyclerView分开定义的,因此它无权访问您的数据集。对于初学者,移动ViewHolder,使其成为RecyclerView的静态嵌套类。像这样的东西:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolders>
    private ArrayList<String> arrayList;
    ....

    public static class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
    ....

    @Override
    public void onClick(View view) {
        Intent intent  = new Intent(...);
        intent.putExtra("path",arrayList.get(getAdapterPosition());
        /* send intent*/
    }
}

#2


1  

You just have to apply onClickListener in you viewholder, take the clicked adapter position in integer and pass it in next your fullscreen activity

您只需在视图中应用onClickListener,将单击的适配器位置置于整数中,然后将其传递给您的全屏活动

Here is the full code, hope it helps

这是完整的代码,希望它有所帮助

MainActivity

public class MainActivity extends AppCompatActivity {

  private final Integer image_ids[] = {
      R.drawable.image1,
      R.drawable.image2,
      R.drawable.image3,
      R.drawable.image4,
      R.drawable.image5,

};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),gridColumnlayout);
    recyclerView.setLayoutManager(layoutManager);

    ArrayList<CreateList> createLists = prepareData();
    MyAdapter adapter = new MyAdapter(getApplicationContext(), createLists);
    recyclerView.setAdapter(adapter);


}
 private ArrayList<CreateList> prepareData(){

    ArrayList<CreateList> theimage = new ArrayList<>();
    for(int i = 0; i< 5; i++){
        CreateList createList = new CreateList();
        createList.setImage_ID(image_ids[i]);
        theimage.add(createList);
    }
    return theimage;
}

}

CreateList Class

public class CreateList {

 private Integer image_id;

 public Integer getImage_ID() {
    return image_id;
 }

 public void setImage_ID(Integer android_image_url) {
    this.image_id = android_image_url;
 }

}

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<CreateList> galleryList;
private Context context;
public final static String EXTRA_MESSAGE = "FullScreen Image";


public MyAdapter(Context context, ArrayList<CreateList> galleryList) {
    this.galleryList = galleryList;
    this.context = context;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

    return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item, parent, false));
}

@Override
public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, int i) {

    CreateList currentList = galleryList.get(i);
    Glide.with(context).load(currentList.getImage_ID()).centerCrop().crossFade().into(viewHolder.img);

    viewHolder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = viewHolder.getAdapterPosition();
            String stringID = Integer.valueOf(pos).toString();

            Intent intent = new Intent(MyAdapter.this.context, FullScreen.class);
            intent.putExtra(EXTRA_MESSAGE, stringID);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return galleryList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView img;

    public ViewHolder(View view) {
        super(view);
        img = (ImageView) view.findViewById(R.id.image);
    }
}

}

FullScreen Activity

public class FullScreen extends AppCompatActivity {


 private final Integer image_ids[] = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
 };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);


    Intent intent = getIntent();
    String message = intent.getStringExtra(MyAdapter.EXTRA_MESSAGE);
    int position = Integer.parseInt(message);

    ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
    imageView.setImageResource(image_ids[position]);

    } 
}  

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:scaleType="fitXY"
    android:id="@+id/image"/>

</RelativeLayout>

#1


0  

Since you are loading the bitmap from file in onBindViewHolder, you can just pass the path instead in onClick and decode it again in your next activity. This may require a bit of work in your case since your ViewHolder is defined separately from your RecyclerView so it does not have access to your dataset. For starters, move your ViewHolder so it can be a static nested class of your RecyclerView. Something like this:

由于您是在onBindViewHolder中从文件加载位图,因此您只需在onClick中传递路径,然后在下一个活动中再次对其进行解码。这可能需要您的案例中的一些工作,因为您的ViewHolder是与RecyclerView分开定义的,因此它无权访问您的数据集。对于初学者,移动ViewHolder,使其成为RecyclerView的静态嵌套类。像这样的东西:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolders>
    private ArrayList<String> arrayList;
    ....

    public static class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
    ....

    @Override
    public void onClick(View view) {
        Intent intent  = new Intent(...);
        intent.putExtra("path",arrayList.get(getAdapterPosition());
        /* send intent*/
    }
}

#2


1  

You just have to apply onClickListener in you viewholder, take the clicked adapter position in integer and pass it in next your fullscreen activity

您只需在视图中应用onClickListener,将单击的适配器位置置于整数中,然后将其传递给您的全屏活动

Here is the full code, hope it helps

这是完整的代码,希望它有所帮助

MainActivity

public class MainActivity extends AppCompatActivity {

  private final Integer image_ids[] = {
      R.drawable.image1,
      R.drawable.image2,
      R.drawable.image3,
      R.drawable.image4,
      R.drawable.image5,

};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),gridColumnlayout);
    recyclerView.setLayoutManager(layoutManager);

    ArrayList<CreateList> createLists = prepareData();
    MyAdapter adapter = new MyAdapter(getApplicationContext(), createLists);
    recyclerView.setAdapter(adapter);


}
 private ArrayList<CreateList> prepareData(){

    ArrayList<CreateList> theimage = new ArrayList<>();
    for(int i = 0; i< 5; i++){
        CreateList createList = new CreateList();
        createList.setImage_ID(image_ids[i]);
        theimage.add(createList);
    }
    return theimage;
}

}

CreateList Class

public class CreateList {

 private Integer image_id;

 public Integer getImage_ID() {
    return image_id;
 }

 public void setImage_ID(Integer android_image_url) {
    this.image_id = android_image_url;
 }

}

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<CreateList> galleryList;
private Context context;
public final static String EXTRA_MESSAGE = "FullScreen Image";


public MyAdapter(Context context, ArrayList<CreateList> galleryList) {
    this.galleryList = galleryList;
    this.context = context;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

    return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item, parent, false));
}

@Override
public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, int i) {

    CreateList currentList = galleryList.get(i);
    Glide.with(context).load(currentList.getImage_ID()).centerCrop().crossFade().into(viewHolder.img);

    viewHolder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = viewHolder.getAdapterPosition();
            String stringID = Integer.valueOf(pos).toString();

            Intent intent = new Intent(MyAdapter.this.context, FullScreen.class);
            intent.putExtra(EXTRA_MESSAGE, stringID);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return galleryList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView img;

    public ViewHolder(View view) {
        super(view);
        img = (ImageView) view.findViewById(R.id.image);
    }
}

}

FullScreen Activity

public class FullScreen extends AppCompatActivity {


 private final Integer image_ids[] = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
 };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);


    Intent intent = getIntent();
    String message = intent.getStringExtra(MyAdapter.EXTRA_MESSAGE);
    int position = Integer.parseInt(message);

    ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
    imageView.setImageResource(image_ids[position]);

    } 
}  

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:scaleType="fitXY"
    android:id="@+id/image"/>

</RelativeLayout>