如何在ViewPager中使用ArrayList?

时间:2019-06-24 18:23:31

标签: android android-fragments android-viewpager

我正在开发一个从sdcard获取图像并将其加载到viewpager中的应用程序。 我正在网格视图中获取图像。我想在单击图像时打开具有相同图像的viewPager。如何将图像加载到viewpager中?

我有ArrayList,其中所有图像uri都显示在网格中。我可以在单击图像时打开viepager,但相应的图像没有打开。

这是我的适配器

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;
    }
}

1 个答案:

答案 0 :(得分:0)

您的视图寻呼机索引编制不正确。请遵循此-

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

您使用图像URL代替可绘制的ID来加载图像。使用滑片等进行图像加载。