android – Incorrect display when swiping in ViewPager

Question:

Please tell me what I'm doing wrong. I have a GridView that draws images from resources in a grid. I store them in an array in the main activity like this:

public static   int[] mThumbIds = { R.drawable.card1, R.drawable.card2,
            R.drawable.card3, R.drawable.card4, R.drawable.card5,
            R.drawable.card6, R.drawable.card7, R.drawable.card8,
            R.drawable.card9, R.drawable.card10, R.drawable.card11,
            R.drawable.card12, R.drawable.card13, R.drawable.card14,
            R.drawable.card15, R.drawable.card16, R.drawable.card17,
            R.drawable.card18, R.drawable.card19, R.drawable.card20,
            R.drawable.card21 };

By clicking on the picture, I get its number in the array (the number is correct, I checked it with logs), put this number in the Intent and open a new activity:

  Intent i = new Intent(getApplicationContext(),
                    FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);

In the new asset, I get the number, initialize the ViewPager, indicate the adapter to it and pass this position as one of the parameters:

viewPager = (ViewPager) findViewById(R.id.myViewPager);
        adapter = new SlideImageAdapter(this, position);
        viewPager.setAdapter(adapter);

Further, the actual problem itself: In the adapter, I get the desired position, when I click on the image, the correct image is displayed, but when swiping left and right, the image does not change. If I try to get an image directly from the adapter, then by clicking on any image, the first in the array is always displayed, although in this case the swiping works correctly. What is the best way to implement my idea? Here is the adapter itself:

public class SlideImageAdapter extends PagerAdapter {

    private Context mContext;
    private LayoutInflater layoutInflater;
    int number;

    public SlideImageAdapter(Context mContext, int number){

        this.mContext = mContext;
        this.number = number;
    }


    @Override
    public int getCount() {
        return MainActivity.mThumbIds.length;
    }

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

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

        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view =  layoutInflater.inflate(R.layout.swipe_layout, container, false);
        ImageView imageView = (ImageView) item_view.findViewById(R.id.full_image_view);

        imageView.setImageResource(MainActivity.mThumbIds[position]);
        container.addView(item_view);

        return item_view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((LinearLayout)object);
    }
}

Answer:

Your int number not used inside the adapter.

If I try to get an image directly from the adapter, then by clicking on any image, the first in the array is always displayed, although in this case the swiping works correctly.

That is why the first in the array is displayed. Need to call viewPager.setCurrentItem(position) after setAdapter

Scroll to Top