Hello. There was a problem, ViewPager does not display pictures when scrolling. When the activator appears with the viewpager, it displays the first picture + 1 in the container (the next one, the second one). When I leaf through it, the second one and the third one appear to be loading. But when scrolling on the 3rd, a gray screen is displayed. There are 6 pictures in total. When prescribing the parameter viewPager.setOffscreenPageLimit(5); All images are displayed correctly.
Adapter code:
public class SwipeAdapter extends PagerAdapter { private int[] imageCount = {R.drawable.pres0,R.drawable.pres1,R.drawable.pres2,R.drawable.pres3,R.drawable.pres4,R.drawable.pres5}; private LayoutInflater layoutInflater; public SwipeAdapter(Context context) { layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return imageCount.length; } @Override public Object instantiateItem(ViewGroup container, int position) { View item_view = layoutInflater.inflate(R.layout.swipe_layout, null); ImageViewTopCrop imageView = (ImageViewTopCrop) item_view.findViewById(R.id.image); imageView.setImageResource(imageCount[position]); container.addView(item_view); return item_view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; }} swipe_layout.xml
<?xml version="1.0" encoding="utf-8"?> <com.companyname.projectname.ImageViewTopCrop xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"/> And it seems the whole problem rests on the custom ImageViewTopCrop
ImageViewTopCrop.java
public class ImageViewTopCrop extends ImageView { public ImageViewTopCrop(Context context) { super(context); setScaleType(ScaleType.MATRIX); } public ImageViewTopCrop(Context context, AttributeSet attrs) { super(context, attrs); setScaleType(ScaleType.MATRIX); } public ImageViewTopCrop(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setScaleType(ScaleType.MATRIX); } @Override protected boolean setFrame(int l, int t, int r, int b) { Matrix matrix = getImageMatrix(); float scaleFactor = getWidth()/(float)getDrawable().getIntrinsicWidth(); matrix.setScale(scaleFactor, scaleFactor, 0, 0); setImageMatrix(matrix); return super.setFrame(l, t, r, b); } }
Without the use of a custom ImageView, everything works, but not with it. But without it, I don’t have the image as I need, so I can’t remove it either. Any ideas?
viewPager.setOffscreenPageLimit(5);parameter isviewPager.setOffscreenPageLimit(5);All 6 ImageViewTopCrop displayed correctly, currently only the first two are displayed. I think, without ViewPager, ImageViewTopCrop will also be displayed correctly, but just in case I’ll check it out. - Icecream