Search code examples
androidandroid-viewpagerlayout-inflaterandroid-pageradapter

PagerAdapter crashing


I have a pageradapter that i am using to display a slideshow of images but it crashes when i set a lower off screen limit.

Here is my code

public class ProjectPicturesAdapter extends PagerAdapter {

private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;

public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
    this.pictures = pictures;
    this.glide = glide;
    this.inflater = inflater;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View image = inflater.inflate(R.layout.slider_image, container, false);
    ImageView myImage = (ImageView) image.findViewById(R.id.image);

    glide.load(pictures.get(position))
            .centerCrop()
            .crossFade()
            .placeholder(R.drawable.project_placeholder)
            .into(myImage);
    container.addView(myImage);
    return myImage;
}

@Override
public void destroyItem(ViewGroup container, int position, Object view) {
    container.removeView((View) view);
}


@Override
public int getCount() {
    return pictures.size();
}

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

}

The way i am initializing the adapter is this

      ProjectPicturesAdapter picturesAdapter = new 
       ProjectPicturesAdapter(Glide.with(this), LayoutInflater.from(this), 
      images);

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(picturesAdapter);
        pager.setOffscreenPageLimit(1);

If there are more than 2 images then it crashes,i could always increase the off screen limit but what happens if there are 20 pictures,i cannot keep the offscreen limit as 20.

My crash log is gives an error saying The specified child already has a parent. You must call removeView() on the child's parent first.


Solution

  • Update ProjectPicturesAdapter as below:

    public class ProjectPicturesAdapter extends PagerAdapter {
    
    private List<String> pictures;
    private LayoutInflater inflater;
    private final RequestManager glide;
    
    public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
        this.pictures = pictures;
        this.glide = glide;
        this.inflater = inflater;
    }
    
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.slider_image, container, false);
        ImageView myImage = (ImageView) view.findViewById(R.id.image);
    
        glide.load(pictures.get(position))
                .centerCrop()
                .crossFade()
                .placeholder(R.drawable.project_placeholder)
                .into(myImage);
    
        container.addView(view);
    
        return view;
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object view) {
        container.removeView((View) view);
    }
    
    @Override
    public int getCount() {
        return pictures.size();
    }
    
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    
    }