0

I have seen other similar questions but cannot seem to get my head around a few things:

Here is what I am trying to achieve

enter image description here

So when the back arrow is pressed I would like the ViewPager to go back from 0 to -1 etc.

Would creating unlimited fragments affect performance of the app , or would just adding 3 fragments be better.

Finally if it would be better to use only 3 fragemnts, how would I go about in fetching new data depending on the date it is currently on.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142

2 Answers2

0

On click of arrow (< or >), you can create onClick listner and based on that you can do some operations as well as if you are using calender in that selecting date than also you can create onClick listner for that.

Basically the performance would depends on:

  • How big your XML layout is.
  • How much tasks are getting done in UI thread.
  • How frequent you are switching between views.

i would say, using three fragment will be better.

you should check this answer.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • Thank you for contribution, I shall try and implement something following the instructions you have provided, but how would I **refresh**my fragments to show the new information. Also would the data already be pre-loaded in one of the fragments before it is selected. – MedicoreProgrammer Sep 27 '16 at 08:24
  • to refresh fragement, you need to call the method again from where you are getting all data that can be done by creating instance of it if you are getting data from activity. check http://stackoverflow.com/questions/20702333/refresh-fragment-at-reload – Amit Vaghela Sep 27 '16 at 08:29
0

For anyone who wants to know how I managed to do it, here is my code:

Note, I have not created an endless viewpager, instead after carefully looking at various other, I have discovered they also did not create an endless ViewPager themselves, instead opting for a very long total pages.

Below is my ViewPagerAdapter which extends FragmentStatePagerAdapter. Looking at the documents for android, this was recommended when using loads of fragments.

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

 private Fragment1 frag1 = new Fragment1();
 static final int PAGE_NUM_ITEMS = 7321;


 public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
 }

 @Override
 public Fragment getItem(int position) {

    return frag1.newInstance(position);
 }

 @Override
 public int getCount() {

    return PAGE_NUM_ITEMS;
 }
}

In my fragment class the code is:

  public  Fragment1 newInstance(int page){

    Fragment1 frag1= new Fragment1();

    return frag1;

}

Then setting the viewpager to the middle of the pages, which I believe is 3660 as Java starts at 0.