SlidingTabLayout:为什么预加载下一个选项卡?

SlidingTabLayout: why preload next tab?

我已经在我的应用程序上实现了 SlidingTabLayout,遵循 https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java 和所有教程 google。一切正常,但我不明白为什么我这样做

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

被自动调用:

  getItem(0);
  getItem(1);

而不是

 getItem(0);

这是默认选项卡。像是为了性能优化而预加载下一个选项卡?我怎样才能避免它?

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

是的。

How can I avoid it?

不要使用 ViewPagerViewPager 背后的想法是用户可以在页面之间滑动。这需要加载当前页面两侧的页面,这样页面之间的动画才能顺利进行。

不确定这是否是您要查找的内容,但听起来您想限制显示视图任一侧加载的视图。

这可能有帮助:

//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

}

注意 setOffscreenPageLimit() 方法。 尝试将其设置为 0 或 1。

来自文档(在 class 中,Android 开发者网站不提供这种特殊性)

/**
 * 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.
 */

您可以尝试找到您想要的值。