SlidingTabLayout:为什么要预加载下一个标签?

时间:2015-05-29 20:40:59

标签: android android-studio material-design android-sliding pagerslidingtabstrip

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

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

会自动调用:

  getItem(0);
  getItem(1);

而不是

 getItem(0);

这是默认标签。就像下一个性能优化选项卡的预加载一样?我怎么能避免它?

2 个答案:

答案 0 :(得分:3)

  

就像预加载下一个性能优化标签一样?

  

我该如何避免呢?

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

答案 1 :(得分:2)

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

这可能有所帮助:

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

来自文档(在课堂上,Android Developer网站不提供这种特殊性)

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

您可以尝试找到所需的值。