8

我正在使用ViewPager2版本 1.0.0-beta05,带有 RecyclerView.Adapter 和ZoomOutPageTransformer,我发现当我们调用 notifyDataSetChanged 时,ViewPager 视图会爆炸。

在 1.0.0-alpha01 版本中,他们说 notifyDataSetChanged 功能齐全(解决了 VP1 错误)

吹的视图

正常行为

        pagerAdapter?.clickListener = this
        with(pager) {
            clipToPadding = false
            clipChildren = false
            offscreenPageLimit = 3
        }
        pager.adapter = pagerAdapter
        pager.setPageTransformer(ZoomOutPageTransformer())


        GlobalScope.launch(Dispatchers.Main) {
            // launch a new coroutine in background and continue
            repeat(15) {
                delay(5000L) // non-blocking delay for 1 second (default time unit is ms)
                Log.e("hello", "notify")
                pagerAdapter?.notifyDataSetChanged()
            }
        }

我没有更改数据源,我只是做了这个小测试,问题仍然存在,每次调用 notifyDataSetChanged 后,视图都会随机调整大小。

4

2 回答 2

6

你找到解决办法了吗?

对我来说,我必须调用ViewPager2'srequestTransform()函数。但是我需要post在adatper之后调用函数。notifyDataSetChanged().

...
adapter.notifyDataSetChanged()
vb.viewpager.post {
    // I am using Fragment, and I get some crashes while I am switching tabs/fragments,
    // so here I reference the `nullable` _vb property
    _vb?.viewpager?.requestTransformation()
}

希望这可以帮助。

于 2020-05-15T08:12:41.527 回答
1

您应该阅读 api 文档:

 /**
 * Sets a {@link PageTransformer} that will be called for each attached page whenever the
 * scroll position is changed. This allows the application to apply custom property
 * transformations to each page, overriding the default sliding behavior.
 * <p>
 * Note: setting a {@link PageTransformer} disables data-set change animations to prevent
 * conflicts between the two animation systems. Setting a {@code null} transformer will restore
 * data-set change animations.
 * ...
 */
public void setPageTransformer(@Nullable PageTransformer transformer) {}

要修复它,请尝试重置页面转换器:

pager.setPageTransformer(null)
adapter.notifyDataSetChanged()
pager.setPageTransformer(myPageTransformer)
于 2020-11-30T14:39:20.107 回答