我在Android上使用ViewPager。
当我从0以外的某个位置启动ViewPager时,我无法向右滑动以返回到先前的位置,但我可以向左滑动以转到下一个位置。
它是默认的ViewPager属性吗?我需要做什么才能向右滑动?
内置OnCreate
viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
在OnCreate之外
private class ImagePagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return imageFull.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((SubsamplingScaleImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ImageGallery.this;
SubsamplingScaleImageView fullImage =
new SubsamplingScaleImageView(ImageGallery.this);
// starting the view pager from position 4
viewPager.setCurrentItem(4);
fullImage.setImage(ImageSource.uri(imageFull.get(position_value)));
return fullImage;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((SubsamplingScaleImageView) object);
}
}
答案 0 :(得分:3)
在为viewpager设置适配器后,您应该 setCurrentItem 而不是在viewpager instantiateItem()方法中。你应该试试这个:
内置OnCreate
viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(4);
答案 1 :(得分:2)
viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(4);
private class ImagePagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return imageFull.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((SubsamplingScaleImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ImageGallery.this;
SubsamplingScaleImageView fullImage =
new SubsamplingScaleImageView(ImageGallery.this);
fullImage.setImage(ImageSource.uri(imageFull.get(position_value)));
return fullImage;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((SubsamplingScaleImageView) object);
}
}