I am a newbie in Android programming and this is my first question on this site. I have spent lots of time searching for this issue but haven't find an answer that suit my case.
I had two pages in ViewPager, one for the current, one for the next page. When the second fragment shows, I set its index to 0, remove the first one and create a new fragment as a replacement for the second. Here is my code.
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == 1) {
currentQuestFr = nextQuestFr;
nextQuestFr = newQuest();
mPager.setCurrentItem(0, false);
mPagerAdapter.notifyDataSetChanged();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
And the Adapter
private class QuestPagerAdapter extends FragmentStatePagerAdapter {
public QuestPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) return currentQuestFr;
else return nextQuestFr;
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
}
The code works well, but notifyDataSetChanged()
seems to refresh my View with no need of that. It annoys users a little. Is there any way to refresh the Adapter without redraw the view, or other way to reach the same purpose?
Thanks very much