首先,viewpage预加载下一页和上一页。所以如果我现在查看pageb,它实际上也存储了page a和page c视图。
它引起了这个问题。当我在pageb中时,我按下一个onclicklistener键,它得到pagec的位置,同时,我想在pageb中更改一个image view,但是当我按下按钮后,它会在pagec中更改图像视图。
这是代码片段,我扩展了pageradapter

  public class CustomPagerAdapter extends PagerAdapter {
        private ArrayList<Item> items;
        private LinearLayout rootView;
        private NetworkImageView album;
        private ImageView left;
        private ImageView right;
        private int imagePos;
        private String[] img;
        private WebView mWebView;
        private ImageView i;
        private ImageView i2;
        private WebView fbLikeBtn;
        private WebView sinaLikeBtn;
        private String type;
        private TextView report;
        private TextView id;
        private TextView name;
        private TextView age_brand;
        private TextView gender_model;
        private TextView feature;
        private TextView remarks;
        private TextView location;
        private TextView date;
        private TextView reward;
        private ImageView find;
        private ImageView email;
        private ImageView call;
        private ImageView share;
        private ImageView gps;
        private ImageView play_record;
        private Item item;
        private String isComplete;
        private LinearLayout reward_layout;
        private DownloadManager mgr;
        private boolean isStartPlay;
        private MediaPlayer myPlayer;
        private BroadcastReceiver onComplete;
        private String intent_type_string;
        private String intent_type;
        private String intent_msg;

        public CustomPagerAdapter(ArrayList<Item> items) {
            this.items = items;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            rootView = (LinearLayout) LayoutInflater.from(ctx).inflate(R.layout.item_detail, null);

            item = (Item) items.get(position);

            album = (NetworkImageView) rootView.findViewById(R.id.img);
            left = (ImageView) rootView.findViewById(R.id.left);
            right = (ImageView) rootView.findViewById(R.id.right);
            share = (ImageView) rootView.findViewById(R.id.share);
            find = (ImageView) rootView.findViewById(R.id.find);
            email = (ImageView) rootView.findViewById(R.id.email);
            call = (ImageView) rootView.findViewById(R.id.call);
            gps = (ImageView) rootView.findViewById(R.id.gps);
            report = (TextView) rootView.findViewById(R.id.report);
            id = (TextView) rootView.findViewById(R.id.id);
            name = (TextView) rootView.findViewById(R.id.name);
            age_brand = (TextView) rootView.findViewById(R.id.age_brand);
            gender_model = (TextView) rootView.findViewById(R.id.gender_model);
            feature = (TextView) rootView.findViewById(R.id.feature);
            remarks = (TextView) rootView.findViewById(R.id.remarks);
            location = (TextView) rootView.findViewById(R.id.location);
            date = (TextView) rootView.findViewById(R.id.date);
            reward = (TextView) rootView.findViewById(R.id.reward);
            play_record = (ImageView) rootView.findViewById(R.id.play_record);
            reward_layout = (LinearLayout) rootView.findViewById(R.id.reward_layout);

            name.setText(getResources().getString(R.string.name) + item.name);

            imagePos = 0;

            if (item.image_url != null) {

                img = new String[item.image_url.length];
                for (int i = 0; i < item.image_url.length; i++) {
                    img[i] = item.image_url[i];
                }

                if (img.length == 1) {
                    album.setImageUrl(Constant.request_imageFolder + item.user_id + "/" + item.image_url[0], mImageLoader);
                    left.setVisibility(View.GONE);
                    right.setVisibility(View.GONE);
                } else {
                    album.setImageUrl(Constant.request_imageFolder
                            + item.user_id + "/" + item.image_url[0],
                            mImageLoader);
                    left.setVisibility(View.GONE);
                    left.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (imagePos != 0) {
                                imagePos--;
                                right.setVisibility(View.VISIBLE);
                                album.setImageUrl(Constant.request_imageFolder
                                        + item.user_id + "/"
                                        + item.image_url[imagePos],
                                        mImageLoader);
                                if (imagePos == 0)
                                    left.setVisibility(View.GONE);
                            }
                        }
                    });
                    right.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (imagePos != item.image_url.length - 1) {
                                imagePos++;
                                left.setVisibility(View.VISIBLE);
                                album.setImageUrl(Constant.request_imageFolder
                                        + item.user_id + "/"
                                        + item.image_url[imagePos],
                                        mImageLoader);
                                if (imagePos == item.image_url.length - 1)
                                    right.setVisibility(View.GONE);
                            }
                        }
                    });
                }
                album.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item = (Item) items.get(position);
                        Intent intent = new Intent(ctx, Zoom.class);
                        intent.putExtra("photo_list", item.image_url);
                        intent.putExtra("pos", imagePos);
                        intent.putExtra("user_id", item.user_id);
                        startActivity(intent);
                    }
                });
            } else {
                album.setDefaultImageResId(R.drawable.image_null);
                album.setImageResource(R.drawable.image_null);
                left.setVisibility(View.GONE);
                right.setVisibility(View.GONE);
            }

            gps.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    item = (Item) items.get(position);
                    Intent intent = new Intent(ctx, MapSingle.class);
                    intent.putExtra("msg", "ID:" + item.id + " " + intent_type_string + " - " + item.name);
                    intent.putExtra("lat", item.lat);
                    intent.putExtra("lng", item.lng);
                    intent.putExtra("type", intent_type_string);
                    startActivity(intent);
                }
            });

            container.addView(rootView);
            return rootView;
        }

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

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

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

在代码中,变量album是更改图像的目标,而变量left和right是开关按钮。谢谢

最佳答案

您的问题是,viewpager一次将创建多个页面(默认情况下,一个在当前查看的页面之前,一个在当前查看的页面之后)。因此,您的引用将指向创建的最后一页。
理想情况下,每个页面都应该有一个custom view,它将有自己的引用。

09-11 22:08