2

I want my adapter to add another page each time it reaches the last page. Here is the code I tried using:

static int ITEMS = 2;

public static class MyAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener{
    public MyAdapter(FragmentManager fragmentManager) 
    {
        super(fragmentManager);
    }

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

    @Override
    public Fragment getItem(int position) 
    {
        if (position >= ITEMS-1)
        {
            ITEMS++;
            notifyDataSetChanged();
        }
        Fragment f = ReviewFragment.init(position); 
        return f;
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

I suggest this:

public static class MyAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener{
    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f = ReviewFragment.init(position); 
        return f;
    }
}
Koso
  • 3,200
  • 2
  • 20
  • 22
  • I don't want to create 2 billions fragments though. – Sean Murray Jul 11 '13 at 18:14
  • They are not going to be create at start. On page 0 fragment 0 and 1 is created. After slide to page 1 fragment 2 is created. And so on. – Koso Jul 11 '13 at 20:04
  • I would like the user to not be able to swipe anymore when the content will be empty. The problem is I don't know when the next page will be empty as I am loading content from a website one page at a time. Any idea how to set the limit once it's already been created? – Sean Murray Jul 11 '13 at 21:17
  • Ah I can use an if statement in getItem to find out if the next page is null and then make ITEMS the size of the position. – Sean Murray Jul 11 '13 at 21:33
  • Yes you can do that like you said. I'm glad I could help you man:) – Koso Jul 11 '13 at 21:43