I am new to Android programming and this is my first question on this site. I spent a lot of time looking for this problem, but did not find an answer that fits my case.
I had two pages in the ViewPager, one for the current, one for the next page. When the second fragment appears, I set its index to 0, delete the first 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 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 update my View without needing it. This is a bit annoying to users. Is there a way to update the adapter without redrawing the view or another way to achieve the same goal? Thank you very much
source share