java.lang.IllegalStateException : Page can only be offset by a positive amount

标签 java android android-viewpager2

我在预启动报告中收到此错误,但它没有显示对我的 .java 文件的任何引用。谁能解释一下吗?

我正在使用 ViewPager2 (1.0.0) 进行页面转换。

Issue: java.lang.IllegalStateException: Page can only be offset by a positive amount, not by -54
 FATAL EXCEPTION: main
Process: [redacted], PID: 18424
java.lang.IllegalStateException: Page can only be offset by a positive amount, not by -54
    at androidx.viewpager2.widget.ScrollEventAdapter.updateScrollEventValues(ScrollEventAdapter.java:280)
    at androidx.viewpager2.widget.ScrollEventAdapter.onScrolled(ScrollEventAdapter.java:178)
    at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:5173)
    at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5338)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:603)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5459)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我找到了this page about ViewPager2引用 ViewPager2 的错误类似的错误。

可能相关:

我最近才开始收到此错误。我最近按以下方式更改了我的代码。我将重写 getItemCount() 方法,并且它返回的 NUM_PAGES 值会根据其他地方的 optional 而有所不同。以前,当切换此选项时,我只会 recreate() 保留所有 fragment 的 Activity。相反,现在我调用 Objects.requireNonNull(recyclerViewAdapter).notifyDataSetChanged(); 并且它工作得很好。我不确定这是否是添加/删除或显示/隐藏最右侧 fragment 的正确方法,因为其他选项会在 fragment 通常所在的位置留下一个空白页面。

编辑:我从 ViewPager2 中删除了我的 PageTransformer - 它仅设置 alpha - 并且错误当前已消失。但我认为这可能是巧合。我还没有在“野外”看到错误,只是间歇性地在发布前报告中看到错误。

编辑:我的 fragment 中没有带有布局动画的 View 组: Not animating ViewGroups

最佳答案

如果您的 ViewPager2 子 fragment 中的 ViewGroup 的 animateLayoutChanges 设置为 true,则可能会发生此错误。

如果是这种情况,您需要通过调用告诉 ViewGroup 仅对自己的布局更改进行动画处理,而不对 ViewPager2 的布局更改进行动画处理

viewGroup.layoutTransition.setAnimateParentHierarchy(false)

详情请参阅 that answer .

ViewPager2 documentation解释一下:

If your pages contain LayoutTransitions, then those LayoutTransitions must have animateParentHierarchy set to false.

Note that if you have a ViewGroup with animateLayoutChanges="true" in your layout xml file, a LayoutTransition is added automatically to that ViewGroup.

You will need to manually call getLayoutTransition().setAnimateParentHierarchy(false) on that ViewGroup after you inflated the xml layout.

关于java.lang.IllegalStateException : Page can only be offset by a positive amount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660691/

相关文章:

android - 如何将 Android 应用程序编译为 64 位

javascript - 如何制作顶部带有 Logo 标题的网格?

android - 如何在另一个 ViewPager2 中使用 ViewPager2

java - ViewPager2/Tabs 问题与 ViewModel 状态

java - 为什么 build.gradle 中的每个项目都是灰色的?

java - 如何查询一对一的Squillions

Java初始赋值

java - 使用新的日期时间 API 格式化日期

android - 如何检查 IntentService 是否已启动

android - 为什么 `PagerAdapter::notifyDataSetChanged` 没有更新 View ?