【问题标题】:ViewPager2.setOffscreenPageLimit() problemViewPager2.setOffscreenPageLimit() 问题
【发布时间】:2020-09-05 16:29:13
【问题描述】:

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

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

我确实... 我在 Android Developers website> 阅读了文档。但我找不到上述问题的原因,我不知道文档中的 '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();
    }
}

【问题讨论】:

  • 这对我来说似乎是一个错误。我在 IssueTracker here 中为此创建了一份报告。
  • @AdilHussain 你解决了这个问题吗?
  • 不,我只是重新依赖默认的屏幕外页面限制,而不是调用此设置器。这对我来说没什么大不了的。
  • @AdilHussain 我也是,我想确认这是我的错误。

标签: android android-viewpager2


【解决方案1】:

我也有同样的问题。我在使用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);
}

【讨论】:

  • 精湛的解决方案和工作正常。
【解决方案2】:

当您创建 TabLayoutMediator 时设置 smoothScrool=false 并且查看寻呼机不会创建所有片段,但此更改会带来另一个错误。当您在选项卡中进行导航时,有时 viewpager 无法正常工作,您会在屏幕上看到两个片段,一半和一半

【讨论】:

    猜你喜欢
    • 2020-11-30
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多