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?

olle
  • 339
  • 2
  • 6
  • 19
  • If you use FragmentStatePagerAdapter and dynamically create the fragments in getItem, then it is simply a matter of increasing the value of getCount and call notifyDataSetChanged on your FragmentStatePagerAdapter variable. – cYrixmorten Nov 01 '13 at 16:57
  • I think he meant dynamic content INSIDE the fragments? It's not really clear. – Martin Marconcini Nov 01 '13 at 17:01
  • @MartínMarconcini yes, u're right – olle Nov 01 '13 at 17:03
  • It's worth mentioning that the "endless" scrolling has nothing to do with the ViewPager or the Fragments per-se. – Martin Marconcini Nov 01 '13 at 17:17
  • @MartínMarconcini so,what should I do? – olle Nov 01 '13 at 17:21
  • You have to implement a ListFragment and attach an scroll listener to your ListView and in the callback, check if you need to load more data or you're at the end. There are countless examples of implementing an endless listview in android ;) – Martin Marconcini Nov 01 '13 at 17:42

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
  • 23,346
  • 18
  • 98
  • 135