title: Deleting Image from a ViewPager
body: <p>I am using a ViewPager to show my Images.</p>
  
  <p>These Images are sent as a List from One Activity to another &amp; then I show them in the ViewPager.</p>
  
  <p>But the Issue is that if I click on a Delete Button them the Image is Deleted &amp; Removed from the List but it still show on the Next Image.</p>
  
  <p>e.g. 1st Image's height is fullscreen > I delete it, viewpager moves to next Image, this image has a height of 350 then the background above &amp; below 350 shows the old Image...</p>
  
  <p>Also, I get a IndexOutOfBoundsExceptiom when I try to delete the 2nd Last Item...</p>
  
  <p>Heres my Code - </p>
  
  <pre><code>public class ImageViewer extends AppCompatActivity {
  
  public int position;
  private ArrayList&lt;String&gt; list;
  private HackyViewPager pager;
  private ImagePagerAdapter ipa;
  private List&lt;PhotoView&gt; photoViewList;
  private HelperMethods helperMethods;
  private static boolean sendResult = false;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.img);
  
      sendResult = false;
      helperMethods = new HelperMethods(ImageViewer.this);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
      getSupportActionBar().hide();
  
      Intent i = getIntent();
      position = i.getExtras().getInt("position");
      list = i.getStringArrayListExtra("list");
  
      pager = (HackyViewPager) findViewById(R.id.pager);
      photoViewList = new ArrayList&lt;PhotoView&gt;();
  
      for(int val = 0; val &lt; list.size(); val++) {
          PhotoView photoView = new PhotoView(this);
          photoView.setZoomable(true);
  
          Glide.with(ImageViewer.this)
              .load(new File(list.get(val)))
              .into(photoView);
  
          photoViewList.add(photoView);
      }
  
      ipa = new ImagePagerAdapter(photoViewList);
      pager.setAdapter(ipa);
      pager.setCurrentItem(position);
  
      FloatingActionButton dlt = (FloatingActionButton) findViewById(R.id.dlt);
      dlt.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  AlertDialog.Builder builder = new AlertDialog.Builder(ImageViewer.this);
  
                  builder.setMessage("Sure to Delete this Image?")
                      .setNegativeButton("Nope", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              dialog.dismiss();
                          }
                      })
  
                      .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
  
                              getFilePos().delete();
                              ipa.remove(pager.getCurrentItem());
  
                              Toast.makeText(ImageViewer.this, "Image Deleted", Toast.LENGTH_SHORT)
                              .show();
                          }
                      });
                  builder.create()
                      .show();
  
              }
          });
  }
  
  
  public File getFilePos() {
      return new File(list.get(pager.getCurrentItem()));
  }
  
  
  
  
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()){
          case android.R.id.home:
              onBackPressed();
              break;
          }
          return true;
      }
  
  
  
  
  static class ImagePagerAdapter extends PagerAdapter {
  
      private List&lt;PhotoView&gt; images;
  
      public ImagePagerAdapter(List&lt;PhotoView&gt; images) {
          this.images = images;
      }
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) {
          PhotoView imageView = images.get(position);
          container.addView(imageView);
          return imageView;
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          container.removeView(images.get(position));
      }
  
      @Override
      public int getCount() {
          return images.size();
      }
  
      void remove(int pos) {
          images.remove(pos);
          notifyDataSetChanged();
      }
  
      @Override
      public int getItemPosition(Object object) {
          return POSITION_NONE;
      }
  
      @Override
      public boolean isViewFromObject(View view, Object o) {
          return view == o;
      }
  }
  </code></pre>
  
  <p>}</p>