0

First of all I do not get any error. My problem is that I do not see the pages 2&3 (out of 4) in the ViewPager. Let me explain:

First all I add directly the pages inside the layout without fragments:

<android.support.v4.view.ViewPager
    android:id="@+id/drawerPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/page_start_one"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        //3 textviews, 1 imageview
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/page_start_two"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        //1 textview, 1 imageview
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/page_start_three"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        //1 textview, 1 imageview
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/page_start_four"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        //3 textviews, 1 button
    </RelativeLayout>
</android.support.v4.view.ViewPager>

I do so for simplicity. Now I load the pager adapter to the activity:

WizardPagerAdapter adapter = new WizardPagerAdapter();
ViewPager pager = (ViewPager) findViewById(R.id.drawerPager);
pager.setAdapter(adapter);

And of course define WizardPagerAdapter. You will see below that destroyItem() does nothing and instantiateItem() just returns the view with is found by the id. This is the code I based from this SO question :

class WizardPagerAdapter extends PagerAdapter {

    public RelativeLayout instantiateItem(View collection, int position) {

        System.out.println(position);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.id.page_start_one;
            break;
        case 1:
            resId = R.id.page_start_two;
            break;
        case 2:
            resId = R.id.page_start_three;
            break;
        case 3:
            resId = R.id.page_start_four;
            break;
        }

        return (RelativeLayout) findViewById(resId);
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
    }

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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }
}

As I said before, with this code I can only see the equivalent RelativeLayouts in pages 0 & 1 but the pages 2 & 3 are always empty.

  • Do you know why this is happening?
  • Is it because of memory limitation?
  • Or another assumption I have is pages 2 & 3 have never been inflated, since onCreate instantiateItem() run only for the first two. (snitched by System.out.println(position); which prints 0 & 1).
Community
  • 1
  • 1
Diolor
  • 13,181
  • 30
  • 111
  • 179

1 Answers1

0

It's probly because viewpager only caches 2 pages by default

you can change it with: pager.setOffscreenPageLimit(X);

where 'X' is the amount of pages.

Renan
  • 1