zoukankan      html  css  js  c++  java
  • [Android]ViewPager如何只初始化一个页面

    使用过ViewPager的应该都知道,ViewPager的setoffscreenpagelimit()方法,使用该方法可以设置在ViewPager滑动时,左右两侧各保存多少个页面,那我们直接设置setoffscreenpagelimit(0),不就好了吗。当然不是这样子的,当我们setoffscreenpagelimit(0)时,如果细心的话,就会发现,并不是只保存当前页面,而是两边的页面同时也有保存。这时候,我们怀着疑问去阅读源码,才会发现,这个方法竟然是这样写的:

    /**
         * 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.
         *
         * <p>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.</p>
         *
         * <p>You should keep this limit low, especially if your pages have complex layouts.
         * This setting defaults to 1.</p>
         *
         * @param limit How many pages will be kept offscreen in an idle state.
         */
        public void setOffscreenPageLimit(int limit) {
            if (limit < DEFAULT_OFFSCREEN_PAGES) {
                Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to "
                        + DEFAULT_OFFSCREEN_PAGES);
                limit = DEFAULT_OFFSCREEN_PAGES;
            }
            if (limit != mOffscreenPageLimit) {
                mOffscreenPageLimit = limit;
                populate();
            }
        }
    

    这个方法的默认值竟然是1。
    这样子,当我们只想初始化一个页面的时候,就要注意了。这个时候,我采用的是懒加载的方式,因为在页面进入前台的时候,是会调用setUserVisibleHint()这个方法的,通过这个方法我们可以获得当前当前页面是否对用户可见,并在对用户可见的时候进行初始化,这时还要注意一点,那就是setUserVisibleHint()这个方法的调用并不保证View等需要的东西是否已经初始化成功,所以我们还是要判断一下的。

  • 相关阅读:
    C# BackGroundWorker控件演示代码
    C#多线程池演示例程下载图片
    反流技术之IE插件技术研究第二部分
    C# 从剪贴板中读取HTML中的中文字符出现乱码问题的解决方案
    C#单线程演示程序带参数
    C# 中as和is的用法总结
    Delphi关于记录文件的操作
    用PowerDesigner创建Access数据库
    常用Delphi开发资料网址
    等待外部应用程序的执行结果
  • 原文地址:https://www.cnblogs.com/diql/p/5926227.html
Copyright © 2011-2022 走看看