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?

  • And you did not try to check whether all your 6 ImageViewTopCrop without ViewPager are displayed well? Maybe it's not the ViewPager, but in them? - iramm
  • When the viewPager.setOffscreenPageLimit(5); parameter is viewPager.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
  • ImageViewTopCrop without ViewPager works fine - Icecream
  • I think it will help you display pages through fragments instead of View. - Yuriy SPb ♦

1 answer 1

Found a solution to this problem. I rewrote the ImageViewTopCrop class and it all worked:

 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 void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); recomputeImgMatrix(); } @Override protected boolean setFrame(int l, int t, int r, int b) { recomputeImgMatrix(); return super.setFrame(l, t, r, b); } private void recomputeImgMatrix() { final Matrix matrix = getImageMatrix(); float scale; final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight(); final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom(); final int drawableWidth = getDrawable().getIntrinsicWidth(); final int drawableHeight = getDrawable().getIntrinsicHeight(); if (drawableWidth * viewHeight > drawableHeight * viewWidth) { scale = (float) viewHeight / (float) drawableHeight; } else { scale = (float) viewWidth / (float) drawableWidth; } matrix.setScale(scale, scale); setImageMatrix(matrix); }}