title: Update Fragment in ViewPager From getItemPosition
body: <p>I have a screen which has the new TabLayout from the design support library. I also have a ViewPager which is used to display a Fragment. This Fragment displays different lists of views depending on which tab is selected.</p>
  
  <p>The app receives a response to a network call, my activity updates the FragmentPagerAdapter belonging to the ViewPager with the data returned by the response, in my case this is 3 lists. </p>
  
  <p>In my ViewPager I override the <code>onPageChangeListener()</code> to detect when a page has been selected, and then I make a call to my FragmentPagerAdapter with the new tab position. I also call <code>notifyDataSet()</code> in order to redraw the view. </p>
  
  <p>I override the <code>getItemPosition()</code> method to determine which list should be passed to the Fragment. However I don't think that this is the correct way to do it as their is a good deal of overdraw occurring from the <code>getItemPosition</code>. The method is actually updating each of the fragments with the same list of results. When scrolling to the next tab the current list is visible in the next tab until the scroll is release, then the fragment redraws with the correct list.</p>
  
  <p>My question is am I taking the correct approach to displaying the lists in my ViewPager Fragments? Or am I seriously overcomplicating things? I don't have much experience with ViewPagers or FragmentPagerAdapters so any input would be greatly appreciated.</p>
  
  <p>My code is as below.</p>
  
  <p>Adding a fragment (called from <code>onResume()</code>)</p>
  
  <pre><code>private void addFragment() {
  
      mViewPager = (ViewPager) findViewById(R.id.viewpager);
  
      mMyPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
              SampleActivity.this);
  
      mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(final int i, final float v, final int i2) {
          }
          @Override
          public void onPageSelected(final int i) {
              mMyPagerAdapter.updateTabPosition(i);
          }
          @Override
          public void onPageScrollStateChanged(final int i) {
          }
      });
  
      mViewPager.setAdapter(mMyPagerAdapter);
      mViewPager.setOffscreenPageLimit(1);
  
      mTabLayout = (TabLayout) findViewById(R.id.my_tab_layout);
      mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
      mTabLayout.setTabMode(TabLayout.MODE_FIXED);
      mTabLayout.setupWithViewPager(mViewPager);
  
      if (shouldMakeNetworkCall()) {
          getLists();
      }
  }
  </code></pre>
  
  <p>The ViewPager FragmentPagerAdapter class</p>
  
  <pre><code>public class MyFragment extends FragmentPagerAdapter {
  
  private Context mContext;
  final int PAGE_COUNT = 3;
  private String tabTitles[] = new String[] { "One", "Two", "Three" };
  private List&lt;MyListType&gt; mListOne;
  private List&lt;MyListType&gt; mListTwo;
  private List&lt;MyListType&gt; mListThree;
  private int mTabPosition;
  
  public MyPagerAdapter(FragmentManager fm, Context context) {
      super(fm);
      mContext = context;
  }
  
  @Override
  public Fragment getItem(int position) {
      switch (position) {
          case 0:
              return MyFragment.newInstance(mListOne);
          case 1:
              return MyFragment.newInstance(mListTwo);
          case 2:
              return MyFragment.newInstance(mListThree);
          default:
              return null;
      }
  }
  
  @Override
  public int getCount() {
      return PAGE_COUNT;
  }
  
  @Override
  public CharSequence getPageTitle(int position) {
      // Generate title based on item position
      return tabTitles[position];
  }
  
  @Override
  public int getItemPosition(Object object) {
      if (object instanceof MyFragment) {
  
          switch (mTabPosition) {
              case 0:
                  ((MyFragment) object).setupUI(mListOne);
                  break;
              case 1:
                  ((MyFragment) object).setupUI(mListTwo);
                  break;
              case 2:
                  ((MyFragment) object).setupUI(mListThree);
                  break;
              default:
                  break;
          }
      }
      return super.getItemPosition(object);
  }
  
  
  public void updateTabPosition(int tabPosition) {
      mTabPosition = tabPosition;
      notifyDataSetChanged();
  }
  }
  </code></pre>