0

I am showing three fragment right now with the middle fragment showing the date of today. The fragment on the left shows the date of yesterday and the fragment on the right shows the date of tomorrow. For example:

  • Fragment left = 2013-12-04
  • Fragment middle = 2013-12-05
  • Fragment right = 2013-12-06

What I want to achieve is that when I swipe to another fragment, that fragment becomes the middle fragment. When I swipe to Fragment left, it becomes Fragment middle. So a new Fragment left needs to be created with a new date and Fragment right needs to be removed. Now it is:

  • Fragment left = 2013-12-03
  • Fragment middle = 2013-12-04
  • Fragment right = 2013-12-05

The code I have so far:

public class Schema extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schema);    
        Utils.setDates();
        List<Fragment> fragments = getFragments();
        PagerAdapter pageAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
        pager.setAdapter(pageAdapter);
        pager.setCurrentItem(1);
    }

    private List<Fragment> getFragments(){
        List<Fragment> fList = new ArrayList<Fragment>();
        ArrayList<String> dates = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        dates.add(df.format(cal.getTime()));
        cal.add(Calendar.DATE, 1);
        dates.add(df.format(cal.getTime()));
        cal.add(Calendar.DATE, -2);
        dates.add(df.format(cal.getTime()));
        Collections.sort(dates);
        for (String date : dates){
            fList.add(FragmentSchema.newInstance(date));
        }
        return fList;
    }
}
stefana
  • 2,296
  • 3
  • 23
  • 45

1 Answers1

-1

try pager.setOffscreenPageLimit(5);