4

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

stackoverflow
  • 2,320
  • 3
  • 17
  • 21
dvtrung94
  • 43
  • 4
  • Do you want it to work in a way that it should load all data initially and then manually redraw the view on some event? Then you can use – sb_269 May 03 '13 at 04:39

1 Answers1

0

Do you want it to work in a way that it should load all data initially and then manually redraw the view on some event? Then you can use mPager.setOffscreenPageLimit(n) where n is your number of pages in mPager. This will draw all the pages on loading.

sb_269
  • 553
  • 2
  • 7
  • 22