title: ViewPager InstantiateItem() Return Wrong Value of Position
body: <p>I am a beginner in android programming.
  I am using button onclick to add new page to the viewpager using the codes below. However, the viewpager return wrong position value when instantiate item on swipe activity. </p>
  
  <p><strong>Swipe to the right</strong></p>
  
  <p>Correct position: 1, 2, 3 (page 2, 3 and 4)</p>
  
  <p>Return wrong position instead: 2, 3, 4</p>
  
  <p><strong>Swipe to the left</strong></p>
  
  <p>Correct position: 3, 2, 1 (page 4, 3 and 2)</p>
  
  <p>Return wrong position instead: 2, 1, 0</p>
  
  <p>I want to get position value from instantiateItem() to get view from a specific position from the "views" array list to be displayed on the viewpager. The wrong position causes the wrong view to be displayed. Hope someone can help me!!! Thanks in advance.</p>
  
  <p><strong>Add Button:</strong></p>
  
  <pre><code>findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {    
              @Override
              public void onClick(View v) {
                  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  View itemView = inflater.inflate(R.layout.image_text_1, null);
  
                  // the array list
                  views.add(itemView);
  
                  pagerAdapter.notifyDataSetChanged();
  
              }
          });
  </code></pre>
  
  <p><strong>ViewPager:</strong></p>
  
  <pre><code>private class SlideAdapter extends PagerAdapter {
                ArrayList&lt;View&gt; views = new ArrayList&lt;View&gt;();
  
                @Override
                public int getItemPosition (Object object)
                {
                    int index = views.indexOf((View)object);
                    if(views.contains((View)object)){
                        return index;
                    }else{
                        return POSITION_NONE;
                    }     
                }
  
                @Override
                public Object instantiateItem (ViewGroup container, final int position)
                {
                      //for example, when I swipe to the right from page 1 to page 2, the int position return value of 2 instead of 1.
                      View v = views.get(position);
  
                      imagePage = (ImageView) v.findViewById(R.id.imgSlide);
                      imagePage.setOnLongClickListener(null);
                      imagePage.setLongClickable(true);
                      imagePage.setOnLongClickListener(btnImageChoose);
                      ((ViewPager)container).addView(v, 0);
                      return v;
                }
  
                @Override
                public void destroyItem (ViewGroup container, int position, Object object)
                {
                  container.removeView (views.get(position));
                }
  
                @Override
                public int getCount ()
                { 
                    return views.size();
                }
  
                @Override
                public boolean isViewFromObject (View view, Object object)
                {
                  return view == object;
                }
  
                public int addView (View v)
                {
                  return addView (v, views.size());
                }
  
                public int addView (View v, int position)
                {
                  views.add (position, v);
                  return position;
                }
  
                public int removeView (ViewPager pager, View v)
                {
                  return removeView (pager, views.indexOf (v));
                }
  
                public int removeView (ViewPager pager, int position)
                {
                  pager.setAdapter (null);
                  views.remove (position);
                  pager.setAdapter (this);
  
                  return position;
                }
  
                public View getView (int position)
                {
                  return views.get (position);
                }
  
              }
  </code></pre>