Psest328
Psest328

Reputation: 6675

Why is ViewPager not returning the correct position?

I set a viewpager as an image selector to change my app background. Currently testing it out with 3 images. The first and second viewpager sets the correct background, but the 3rd sets the 2nd image as the background. Inserted a log to see what position is being called. It should have read: "1", "2", "3". But instead, it's showing "1" "2" "2". How can I get that last page to show to correct position?

Here's the pageradapter class where it's all happening.

    import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class BackgroundChangerAdapter extends PagerAdapter {



        private Context context;
        private int[] images;
        private int mPosition;

        BackgroundChangerAdapter(Context context, int[] images) {
            this.context = context;
            this.images = images;
        }

        @Override
        public int getCount() {
            return 3;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            mPosition = position;

            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(images[mPosition + 1]);
            imageView.setClickable(true);

            imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Log.d(MyActivity, "setBackground - position = " + mPosition);

                    ((BackgroundChangerActivity) context).setDrawable(images[mPosition]);
                    ((BackgroundChangerActivity) context).finish();
                }
            });

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

                return imageView;
        }

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

    }

Upvotes: 2

Views: 1012

Answers (1)

dangVarmit
dangVarmit

Reputation: 5661

mPosition is tripping you up. it's a member of the adapter, so when onclick is called it looks at the most current value of mPosition (from the last call to instantiateItem())

easiest way to fix that problem is to store your position in the tag. in instantiateItem..

imageView.setTag(new Integer(position));

then in imageView.onClickListener.onClick(View v)

int position = (Integer)v.getTag();
...

now you have the position of the imageview that got clicked, not the last one that got instantiated

Upvotes: 3

Related Questions