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.
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.
In my ViewPager I override the onPageChangeListener()
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 notifyDataSet()
in order to redraw the view.
I override the getItemPosition()
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 getItemPosition
. 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.
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.
My code is as below.
Adding a fragment (called from onResume()
)
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();
}
}
The ViewPager FragmentPagerAdapter class
public class MyFragment extends FragmentPagerAdapter {
private Context mContext;
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "One", "Two", "Three" };
private List<MyListType> mListOne;
private List<MyListType> mListTwo;
private List<MyListType> 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();
}
}