0

I'm using viewpager in my test android-app.

I'd like to use 3 fragments with dynamic content. Can i make "endless" scrolling? Because i want to work with Date like this: 2fragment - (currentdate||date), 1fragment - (date-1), 3fragment - (date+1)

What's the best way to go about this?

Ilnar Karimov
  • 339
  • 2
  • 6
  • 19

1 Answers1

0

Create a custom FragmentStatePagerAdapter. Override the (mandatory) getItem method:

  @Override
  public void getItem(int position){ 
   switch (position) {
   case 0:
          // create your dates for the leftmost fragment  
          date1 == xxx; 
          date2 == yyy;
          // add it to a Bundle arg
          Bundle bundle = new Bundle();
          bundle.add blah blah
          // Create your Fragment
          Fragment fragment = new YourFragmentWithTheList();
          fragment.setArgs(bundle);
          return fragment;
      case 1:
          // create your dates for the center fragment 
          [ do the same as above ]
          return fragment;
      case 2: 
          // now your rightmost fragment.
          same as above… 
   }
return null
}
  1. Also you must override getCount…

    @Override
    public int getCount() {
        return 3; // you only have three hardcoded frags
    }
    

And then in YourFragmentWithTheList.java you have to get the date from the arguments and do the usual ListFragment stuff (create the list, initialize the adapter, load more when at the bottom, etc.). You have the dates to get the correct data for each one. So your Fragment must be dummy and expect the dates to come in the arguments.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144