0

I'm loading images from my server to the recycler view.Now on click of recycler view I'm sending only the clicked image's link but I wanna pass the whole array of images onClick to the view pager in order to display the images side by side. So,can anyone please help me to do this.

My RecyclerView's adapter code is :

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.list_item,parent,false);
    return new ViewHolder(v);
}

public MyAdapter(List<ListItem> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}

@Override
public void onBindViewHolder(final MyAdapter.ViewHolder holder, int position) {
    final ListItem listItem=listItems.get(position);
    Glide.with(context).load(listItem.getImageUrl()).centerCrop().into(holder.iv);
    holder.ln.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String s=listItem.getImageUrl();
                    Intent i=new Intent(context,ImageShow.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.putExtra("Image",s);
                    context.startActivity(i);
                }
            }
    );
}

@Override
public int getItemCount() {
    return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
    public ImageView iv;LinearLayout ln;

    public ViewHolder(View itemView) {
        super(itemView);
        iv=(ImageView)itemView.findViewById(R.id.imageView);
        ln=(LinearLayout)itemView.findViewById(R.id.linLay);
    }
} }

Now in my view pager class now it's showing an array of images from drawable:

    public class CustomAdapter extends PagerAdapter {
    private int[] images = {R.drawable.c,R.drawable.d,R.drawable.e};
    private Context ctx;
    private LayoutInflater inflater;

    public CustomAdapter(Context ctx){
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view ==(RelativeLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.swipe,container,false);
        ImageView img =(ImageView)v.findViewById(R.id.imageView);
        img.setImageResource(images[position]);
        container.addView(v);
        return v;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        container.refreshDrawableState();
    }
}
anonymous
  • 1,604
  • 3
  • 14
  • 37

1 Answers1

0

It is not entirely clear what is your trouble but if you don't know how to pass an array of strings through Intent here's the method you're looking for Intent.putExtra(String name, String[] value. Here's how to convert a list to an array.

I personally would advocate against such a data flow, your RecyclerView.Adapter should signal Fragment/Activity which item was selected and it's that parent components responsibility to pass the data they own to the PagerAdapter. It is called a unidirectional data flow.

charlag
  • 834
  • 10
  • 19