title: Update ViewPager dynamically?
body: <p>I can't update the content in ViewPager.</p>
  
  <p>What is the correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class?</p>
  
  <p>I was using only getItem() to instantiate and return my fragments:</p>
  
  <pre><code>@Override
  public Fragment getItem(int position) {
      return new MyFragment(context, paramters);
  }
  </code></pre>
  
  <p>This worked well. Except I can't change the content.</p>
  
  <p>So I found this: <a href="https://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view">ViewPager PagerAdapter not updating the View</a></p>
  
  <blockquote>
    <p>"My approach is to use the setTag() method for any instantiated view in the instantiateItem() method"</p>
  </blockquote>
  
  <p>Now I want to implement instantiateItem() to do that. But I don't know what I have to return (the type is Object) and what is the relation with getItem(int position)?</p>
  
  <p>I read the <a href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html" rel="noreferrer">reference</a>:</p>
  
  <blockquote>
    <ul>
    <li><p>public abstract Fragment getItem (int position)</p>
    
    <p>Return the Fragment associated with a specified position.</p></li>
    <li><p>public Object instantiateItem (ViewGroup container, int position)</p>
    
    <p>Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).
    Parameters</p>
    
    <p>container   The containing View in which the page will be shown.
    position    The page position to be instantiated.</p>
    
    <p>Returns</p>
    
    <p>Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.</p></li>
    </ul>
  </blockquote>
  
  <p>but I still don't get it.</p>
  
  <p>Here's my code. I'm using support package v4.</p>
  
  <p>ViewPagerTest </p>
  
  <pre><code>public class ViewPagerTest extends FragmentActivity {
      private ViewPager pager;
      private MyFragmentAdapter adapter; 
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.pager1);
  
          pager = (ViewPager)findViewById(R.id.slider);
  
          String[] data = {"page1", "page2", "page3", "page4", "page5", "page6"};
  
          adapter = new MyFragmentAdapter(getSupportFragmentManager(), 6, this, data);
          pager.setAdapter(adapter);
  
          ((Button)findViewById(R.id.button)).setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  reload();
              }
          });
      }
  
      private void reload() {
          String[] data = {"changed1", "changed2", "changed3", "changed4", "changed5", "changed6"};
          //adapter = new MyFragmentAdapter(getSupportFragmentManager(), 6, this, data);
          adapter.setData(data);
          adapter.notifyDataSetChanged();
          pager.invalidate();
  
          //pager.setCurrentItem(0);
      }
  }
  </code></pre>
  
  <p>MyFragmentAdapter</p>
  
  <pre><code>class MyFragmentAdapter extends FragmentPagerAdapter {
      private int slideCount;
      private Context context;
      private String[] data;
  
      public MyFragmentAdapter(FragmentManager fm, int slideCount, Context context, String[] data) {
          super(fm);
          this.slideCount = slideCount;
          this.context = context;
          this.data = data;
      }
  
      @Override
      public Fragment getItem(int position) {
          return new MyFragment(data[position], context);
      }
  
      @Override
      public int getCount() {
          return slideCount;
      }
  
      public void setData(String[] data) {
          this.data = data;
      }
  
      @Override
      public int getItemPosition(Object object) {
          return POSITION_NONE;
      }
  }
  </code></pre>
  
  <p>MyFragment</p>
  
  <pre><code>public final class MyFragment extends Fragment {
      private String text;
  
      public MyFragment(String text, Context context) {
          this.text = text;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.slide, null);
          ((TextView)view.findViewById(R.id.text)).setText(text);
  
          return view;
      }
  }
  </code></pre>
  
  <p>Here is also somebody with a similar problem, no answers
  <a href="http://www.mail-archive.com/android-developers@googlegroups.com/msg200477.html" rel="noreferrer">http://www.mail-archive.com/android-developers@googlegroups.com/msg200477.html</a></p>