简体   繁体   中英

How to use ArrayList in ViewPager?

I am working on an app that fetch images from sdcard and load into viewpager. I am getting the images in grid view.I want to open viewPager with same image on clicking the image. How to load images into viewpager?

I have ArrayList with all the images uri to show in the grid.I am able to open viepager on clicking the image but respective image is not opening.

This is my adapter

public class GalleryAdapter extends BaseAdapter {

    private Context ctx;
    private int pos;
    private LayoutInflater inflater;
    private ImageView ivGallery;
    ArrayList<Uri> mArrayUri;
    public GalleryAdapter(Context ctx, ArrayList<Uri> mArrayUri) {

        this.ctx = ctx;
        this.mArrayUri = mArrayUri;
    }

    @Override
    public int getCount() {
        return mArrayUri.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrayUri.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        pos = position;
        inflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.gv_item, parent, false);
        ivGallery = (ImageView) itemView.findViewById(R.id.ivGallery);
        ivGallery.setImageURI(mArrayUri.get(position));
        return itemView;
    }
}
public class ImagePageAdapter extends PagerAdapter {

    private List<ImageView> images;

    public ImagePageAdapter(List<ImageView> images) {
        this.images = images;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = images.get(position);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }

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

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

Your view pager indexing is not happening properly. Please follow this -

http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/

Instead of drawable ids, you use image url to load the image. Use glide, etc for image loading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM