【问题标题】:SlidingTabLayout: why preload next tab?SlidingTabLayout:为什么要预加载下一个选项卡?
【发布时间】:2015-05-29 20:40:59
【问题描述】:

我已经在我的应用程序上实现了 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);

这是默认选项卡。是否像预加载下一个选项卡以进行性能优化?如何避免?

【问题讨论】:

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


    【解决方案1】:

    是否像预加载下一个选项卡以进行性能优化?

    是的。

    如何避免?

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

    【讨论】:

    • 如果我想使用没有预加载的 SlidingTab,我可以用什么来代替 ViewPager?
    • @helloimyourmind:嗯,SlidingTabLayout 看起来像是ViewPager 的指标,所以你也需要找到一个替代品。或者,解决任何导致您担心ViewPager 加载两个页面的问题。
    • 我不知道如何解决它,因为在加载第二页和第三页时,我会进行 HTML 页面解析,速度很快,但服务器太慢了,所以需要 7 秒用于打开页面。
    • @helloimyourmind:那么,你还有其他问题。这不像阻止第二页被加载会神奇地使您的服务器运行得更快。而且,如果需要 7 秒,那么无论如何用户完全有能力在这段时间内切换到第二个选项卡。无论如何,您必须在后台线程上执行该网络 I/O,因此您需要您的页面能够显示某种进度指示器,直到您拥有数据为止。无论您如何尝试显示这些页面,您都将需要它。
    • 好的,我解决了我的问题,感谢您的建议。你能看看我的另一个问题吗? stackoverflow.com/questions/30518710/…
    【解决方案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 开发者网站不提供这种特殊性)

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多