ViewPager2.setOffscreenPageLimit()问题

2024-03-13

API 30 Android 10.0+(Google API)、AVD (x86)

问题是... 只是为了测试 ViewPager2。我将 ViewPager2 与 TabLayout 和附加片段一起使用。然后我将“离屏页面限制值”设置为 1。我预计将保留 3 页。 (当前、左、右页)但保留了大约6页。当我使用以前的ViewPager时,它工作得很好。

我做了... 我阅读了文档 Android 开发者网站>。但我找不到上述问题的原因,我不知道文档中的“OFFSCREEN_PAGE_LIMIT_DEFAULT”意味着要维护多少页。它被定义为-1。

代码是...

public class MainActivity extends AppCompatActivity { 
    private TabLayout tabLayout; 
    private ViewPager2 viewPager; 
    private ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);

        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(new ViewPagerAdapter(this, 9));

        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText("Tab " + (position + 1));
            }
        }).attach();
    }
}

我有同样的问题。我在使用时用以下代码解决了setOffscreenPageLimit(1):

/**
 * Sets whether the LayoutManager should be queried for views outside of
 * its viewport while the UI thread is idle between frames.
 *
 * <p>If enabled, the LayoutManager will be queried for items to inflate/bind in between
 * view system traversals on devices running API 21 or greater. Default value is true.</p>
 *
 * <p>On platforms API level 21 and higher, the UI thread is idle between passing a frame
 * to RenderThread and the starting up its next frame at the next VSync pulse. By
 * prefetching out of window views in this time period, delays from inflation and view
 * binding are much less likely to cause jank and stuttering during scrolls and flings.</p>
 *
 * <p>While prefetch is enabled, it will have the side effect of expanding the effective
 * size of the View cache to hold prefetched views.</p>
 *
 * @param enabled <code>True</code> if items should be prefetched in between traversals.
 *
 * @see #isItemPrefetchEnabled()
 */
RecyclerView.LayoutManager layoutManager =  ((RecyclerView)(viewPager.getChildAt(0))).getLayoutManager();
if(layoutManager != null) {
    layoutManager.setItemPrefetchEnabled(false);
}


/**
 * Set the number of offscreen views to retain before adding them to the potentially shared
 * {@link #getRecycledViewPool() recycled view pool}.
 *
 * <p>The offscreen view cache stays aware of changes in the attached adapter, allowing
 * a LayoutManager to reuse those views unmodified without needing to return to the adapter
 * to rebind them.</p>
 *
 * @param size Number of views to cache offscreen before returning them to the general
 *             recycled view pool
 */ 
RecyclerView recyclerView=  ((RecyclerView)(viewPager.getChildAt(0)));
if(recyclerView != null) {
    recyclerView.setItemViewCacheSize(0);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ViewPager2.setOffscreenPageLimit()问题 的相关文章

随机推荐

  • jQueryUI Datepicker 上一个和下一个图标不显示

    我已将 jquery datepicker 合并到我的应用程序中 但是无论我选择什么主题 上一个 下一个图标都不会显示 我也使用了不同版本的 jquery ui js 我尝试了一些我之前看到过的项目 例如 使用 hideIfNoPrevNe
  • 需要有关将代码转换为 Matlab_extension 1 的建议

    这是之前提出的问题的扩展 link https stackoverflow com questions 20851902 need suggestion on code conversion to matlab noredirect 1 c
  • 在 Android 屏幕上显示通知

    我正在使用此代码发送本地通知 mNotificationManager NotificationManager getSystemService Context NOTIFICATION SERVICE PendingIntent cont
  • 如何在每页显示子报表的标题?

    我需要在C1Report设计器中设置什么属性才能在每个新页面上显示我的子报表的标题 Repeat true 设置该属性后 子报表不会在每个新页面上显示标题 它确实工作得很好Main Report tho 您需要在子报表的主键字段上添加分组
  • SSL_CTX_load_verify_locations 失败并显示 SSL_ERROR_NONE

    我到处搜索 但没有找到这个问题的解释 我正在运行以下命令 int ret 0 ERR clear error ret SSL CTX load verify locations ctx f 50 server SSLCACertificat
  • 如何在 Groovy 2.4 中计算 sha256 哈希值

    我正在尝试计算 Groovy 版本 2 4 16 中的 sha256 哈希值 这是 jmeter 测试的一部分 这是它支持的 Groovy 版本 我认为我无法更改它 我知道在 Groovy 2 5 中你可以使用这样的代码 https mrh
  • 如何使用javascript填写表单字段并提交?

    如果我有一个 html 文档 其大致结构是 div class headerstuff stuff div div class body div
  • 使用三元运算符而不是 IF THEN 有什么意义? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何实现全屏模式?

    在 Windows 操作系统下运行的应用程序如何实现像 Web 浏览器一样的全屏模式 它比最大化窗口和隐藏窗口装饰更复杂吗 表单样式 无 最大化 如果您指定语言 您可能会获得一些示例代码 EDIT 回应什么格雷格发布 https stack
  • 使用 ggplot2 中的 stat_poly_eq 指定每个方面的公式

    我借用了这个示例数据集here https stackoverflow com questions 44912496 geom smooth with facet grid and different fitting functions L
  • 异步方法恢复后值未更新

    看这段代码 public class SharedData public int Value get set void button1 Click object sender EventArgs e AAA async Task BBB S
  • Mercurial 卡在“等待锁定”状态

    克隆 Mercurial 存储库时 Windows 出现蓝屏 重新启动后 我现在几乎所有 hg 命令都会收到此消息 c src gt hg commit waiting for lock on repository c src McVrsS
  • 在比较四个字符的字符串时,有什么简单的方法可以检查两个或多个字符是否相等?

    我必须比较两个字符串 例如INTU and IXTE并检查两个或多个字符是否相同 对于前两个字符串 我想返回true 因为 I 和 T 相同 字符串中字母的顺序最终无关紧要 因为每个字符不能出现在字符串中的不同位置 似乎应该有一个简单的方法
  • 使用 dplyr 根据最大​​行值添加新列?

    我有一个大型数据库 其中包含一系列带有数字的列 我想用dplyr添加新列 mutate 其值是具有最大值的列的名称 所以 对于下面的例子 set seed 123 data frame bob rnorm 10 sam rnorm 10 d
  • 此计算机上未安装 Azure CLI 2.x

    我正在尝试使用 az storage blob upload batch 上传 blob 我遇到了以下两个例外 错误 此计算机上未安装 Azure CLI 2 x 错误 脚本失败 出现错误 错误 无法找到可执行文件 pwsh 请验证文件路径
  • 这是一个正确的线程安全随机包装器吗?

    我对线程和并发相当缺乏经验 为了解决这个问题 我目前正致力于在 F 中实现随机搜索算法 我遵循现有 C 示例的想法 围绕 System Random 类编写了一个包装器 但由于我不确定如何开始对其错误行为进行单元测试 所以我想听听更有经验的
  • 将 nvarchar 转换/转换为日期时间

    我有表 id timestamp 1 31 10 2020 16 32 11 2 09 09 2020 09 15 49 3 22 04 2020 02 48 15 表中有大量这些数据 列时间戳是数据类型 nvarchar 我需要按日期对数
  • 批处理cmd中弹出消息

    echo msgbox Hey Here is a message gt tmp tmp vbs cscript nologo tmp tmp vbs del tmp tmp vbs or echo msgbox Hey Here is a
  • 是否可以记住可组合函数的状态?

    我试图记住类型变量的状态 Composable gt Unit 我没有收到编译器错误 但应用程序在启动时崩溃 为什么会发生这种情况以及解决此问题的最佳方法是什么 最小可重现示例 Composable fun App isHappy Bool
  • ViewPager2.setOffscreenPageLimit()问题

    API 30 Android 10 0 Google API AVD x86 问题是 只是为了测试 ViewPager2 我将 ViewPager2 与 TabLayout 和附加片段一起使用 然后我将 离屏页面限制值 设置为 1 我预计将