从Gallery的onItemClick更新Viewpager

时间:2014-06-10 12:41:08

标签: android imageview android-viewpager android-gallery

我已经设置了一个View Pager和一个Gallery。我想从我的图库的OnItemClickListener更新我的View Pager的ImageView,并将它传递给ImageView,以便它会更新,这样就会有更大的图像视图。

我怎样才能做到这一点?如果有人能帮助我,那就太棒了,谢谢。

这就是我设置的内容(参见onClickGallery类的底部):

public void updateUI(final SimpleItemType item) {

    class ImagePagerAdapter extends PagerAdapter {

        private List<String> mImages = item.pictureURL;

        @Override
        public int getCount() {

            if (mImages == null) {

                SuperToast.create(
                        getApplicationContext(),
                        "No image available",
                        SuperToast.Duration.VERY_SHORT,
                        Style.getStyle(Style.GRAY,
                                SuperToast.Animations.SCALE)).show();

            }

            else {

                return mImages.size();
            }
            return 0;

        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = DetailActivity.this;
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(
                    R.dimen.padding_medium);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

            Picasso.with(context).load(item.pictureURL.get(position)).fit()
                    .centerInside().into(imageView);

            ((ViewPager) container).addView(imageView);

            return imageView;
        }

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

    class ImageGalleryAdapter extends BaseAdapter {

        private List<String> mImages = item.pictureURL;

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = (ImageView) convertView;
            Context context = DetailActivity.this;
            if (imageView == null) {
                imageView = (ImageView) getLayoutInflater().inflate(
                        R.layout.item_gallery_image, parent, false);
            }

            Picasso.with(context).load(item.pictureURL.get(position)).fit()
                    .centerInside().into(imageView);
            return imageView;
        }

    }

    gallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            //Here I would like to pass my ImageView from gallery to view pager


        }
    });

1 个答案:

答案 0 :(得分:0)

尝试声明viewpager的imageview全局(如mImages), 然后像这样改变imageview的图像:

imageView.setBackgroundResource(your.image.resource);