Reputation: 84
I am using ViewPager to switch images and it works. I have grid view and when i click an image on it. that same image opens OKay. But when i switch between images it starts from the beginning leaving the first.
for example. I have int[5] images. If id = 3 then it displays image 3 1 2 3 4. the image is 3 is repeated and image[0] is not displayed.
I want it to 4 if i swipe right and 2 if i swipe left.
If you still can't understand. here is the video link of my app. https://youtu.be/HI-BYz4M8ms
@Override
public Object instantiateItem(ViewGroup viewGroup, int position)
{
if(position == 0) {
position = id;
}
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(images[position]);
viewGroup.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
Upvotes: 0
Views: 1681
Reputation: 3869
You need to display your ViewPager as normal and call viewpager.setCurrentItem(itemIndex)
to go to the position of your selected image.
What you're doing at the moment is replacing the first image by the selected one. So if the selected one is not the first one, the image 0 is not displayed and the selected one is displayed twice.
Upvotes: 1