2

I'm writing an application where I need to show some 100 page in ViewPager. There are facility to jump from one index to direct any random index page. Here Every page it self contain list of item.

Now if I just to user ViewPager.current(index) to jump from one to any random index page..then UI get stuck for a moment and then display.

So to avoid this I thought to implement only 7 adapter page view..where these 100(getCount()) page will reuses these 7 page.

But Im'm getting

E/AndroidRuntime(25961): java.lang.IllegalStateException: Fragment already added: ArrayListFragment{24dad9b6 #0 id=0x7f060009}
E/AndroidRuntime(25961):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1192)
E/AndroidRuntime(25961):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
E/AndroidRuntime(25961):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
E/AndroidRuntime(25961):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java. 

I'm using FragmentStatePagerAdapter for adapter implementation.

Following snap of my code I'm using

static final int NUM_ITEMS = 100;
    private int MAX_AVAILABLE_COUNT = 7;
    private ArrayList<ArrayListFragment> listFragment = new ArrayList();

    public class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);

            for (int i = 0; i < MAX_AVAILABLE_COUNT; i++) {
                listFragment.add(ArrayListFragment.newInstance(i));
            }
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {

            original = position;

            if (position >= MAX_AVAILABLE_COUNT - 1) {
                int newPosition = position % MAX_AVAILABLE_COUNT;

                position = newPosition;
            }

            Fragment fragment = listFragment.get(position);

            return fragment;
        }
    }

Any suggestion what I'm doing wrong here. Or any other suggestion to achieve it!!

CoDe
  • 11,056
  • 14
  • 90
  • 197

1 Answers1

0

Your error is because you are manipulating with position in getItem() method. You can't do like that because at some point you will return fragment tied to other page and you will get exception like above. And storing references to fragments is also bad practice, always return new instance of fragment object.

curioushikhov
  • 2,461
  • 2
  • 30
  • 44
  • Always return new instance ..make slow app as I mention. What could be other way to achieve this. Any suggestion ! – CoDe Jun 01 '15 at 10:09
  • ViewPager doesn't load all the pages at once, so new instance on demand will improve your startup speed. And if speed is important try not use fragments but create custom PagerAdapter directly. – curioushikhov Jun 01 '15 at 10:20
  • App I'm developing may have 1000 child..n so Im using FragmentStatePagerAdapter that anyway help on demand. Each of fragment I'm using has Expandable List View...n that make my app slow. So if just use min number of child instead 1000 fragment child It definately can help.. let me know if u have any suggestion. – CoDe Jun 01 '15 at 10:39