2

I've implemented SlidingTabLayout on my application, following https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java and tutorial all google. All works, but I don't understand why when I do

  viewPager.setAdapter(viewPageAdapter);
  slidingTabLayout.setViewPager(viewPager);

are automatically called:

  getItem(0);
  getItem(1);

Instead of

 getItem(0);

that's the tab of default. Is like a pre-load of the next tab for performance optimization? How can I avoid it?

helloimyourmind
  • 994
  • 4
  • 14
  • 30

2 Answers2

3

Is like a pre-load of the next tab for performance optimization?

Yes.

How can I avoid it?

Don't use ViewPager. The idea behind ViewPager is that the user can swipe between pages. That requires the pages on either side of the current page to be loaded, so the animation between pages can happen smoothly.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So if I want to use SlidingTab with no preload what can I use instead of ViewPager? – helloimyourmind May 29 '15 at 20:44
  • @helloimyourmind: Well, that `SlidingTabLayout` looks like it is an indicator for a `ViewPager`, so you would need to find a replacement for that too. Or, fix whatever problem is causing you to worry about two pages being loaded by the `ViewPager`. – CommonsWare May 29 '15 at 20:46
  • I don't know how to fix it, because on the load of the second and third page I do HTML pages parsing that is fast, but the server is too much slow, so it require like 7secs for the page opening. – helloimyourmind May 29 '15 at 20:48
  • @helloimyourmind: Well, then, you have other problems. It's not like preventing the second page from being loaded is going to magically cause your server to go faster. And, if if takes 7 seconds, the user is perfectly capable of switching to a second tab within that period of time anyway. You have to be doing that network I/O on a background thread anyway, so you need your pages to be able to show some sort of progress indicator until such time as you have your data. You will need that no matter how you try showing those pages. – CommonsWare May 29 '15 at 20:51
  • Ok I solved my problem, thanks for your suggestions. Can u please have a look to my other question? http://stackoverflow.com/questions/30518710/slidingtablayout-replace-with-fragment – helloimyourmind May 29 '15 at 21:07
2

Not sure if this is what your looking for but sounds like you would like to limit the views loaded either side of the displayed view.

This may help:

//BEGIN_INCLUDE Fragment onViewCreated
public void onViewCreated(View view, Bundle savedInstanceState) {

    // BEGIN_INCLUDE (setup_viewpager)
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);


    //setOffscreenPageLimit() allows the viewpager to maintain state as it's swiped.  the chronometer will keep running as the page is swiped
    //This may need to be adjusted as the app gets more tabs ADDED 3-31-15
    mViewPager.setOffscreenPageLimit(8);//8 screens loaded and maintain state

}

Notice the setOffscreenPageLimit() method. Try setting this to 0 or 1.

From the Documentation (in the class, the Android Developer website does not offer this kind of specificity)

/**
 * Set the number of pages that should be retained to either side of the
 * current page in the view hierarchy in an idle state. Pages beyond this
 * limit will be recreated from the adapter when needed.
 *
 * This is offered as an optimization. If you know in advance the number
 * of pages you will need to support or have lazy-loading mechanisms in place
 * on your pages, tweaking this setting can have benefits in perceived smoothness
 * of paging animations and interaction. If you have a small number of pages (3-4)
 * that you can keep active all at once, less time will be spent in layout for
 * newly created view subtrees as the user pages back and forth.
 *
 * You should keep this limit low, especially if your pages have complex layouts.
 * This setting defaults to 1.
 *
 * @param limit How many pages will be kept offscreen in an idle state.
 */

You can experiment to find a value that you want.

El Mac
  • 3,256
  • 7
  • 38
  • 58
DroidCrafter
  • 136
  • 1
  • 5