页面包含具有布局转换(或动画布局更改=“true”)的视图组,这会干扰滚动动画

我在ViewPager2中使用片段,我注意到生产中的两个相关的TravilStateException(我自己无法重现它)发生在运行Android 8或9的小米,玉龙,华硕,vivo等设备中:

Fatal Exception: java.lang.IllegalStateException: Page can only be offset by a positive amount, not by -758
       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:1029)
       at android.view.Choreographer.doCallbacks(Choreographer.java:834)
       at android.view.Choreographer.doFrame(Choreographer.java:760)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
       at android.os.Handler.handleCallback(Handler.java:873)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:224)
       at android.app.ActivityThread.main(ActivityThread.java:7083)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)

Fatal Exception: java.lang.IllegalStateException: Page(s) contain a ViewGroup with a LayoutTransition (or animateLayoutChanges="true"), which interferes with the scrolling animation. Make sure to call getLayoutTransition().setAnimateParentHierarchy(false) on all ViewGroups with a LayoutTransition before an animation is started.
       at androidx.viewpager2.widget.ScrollEventAdapter.updateScrollEventValues(ScrollEventAdapter.java:272)
       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:1029)
       at android.view.Choreographer.doCallbacks(Choreographer.java:841)
       at android.view.Choreographer.doFrame(Choreographer.java:769)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
       at android.os.Handler.handleCallback(Handler.java:794)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:176)
       at android.app.ActivityThread.main(ActivityThread.java:6651)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

在环顾四周时,我经过了以下线程:https://issuetracker.google.com/issues/129530305,正如我正确理解的那样,似乎总结为在viewPager2的任何父布局上设置为false,这就是我所做的。不幸的是,这并没有解决我的问题。然后我发现了另一个线程:java.lang.IllegalStateException:页面只能被正量偏移,这似乎没有多大帮助。任何想法还有什么可能导致这个问题?animateLayoutChanges


答案 1

您需要在每个页面/片段上执行此操作

 View view = layoutInflater.inflate(R.layout.page, parent, false);
 ViewGroup viewGroup = view.findViewById(R.id.animated_viewgroup);
 viewGroup.getLayoutTransition().setAnimateParentHierarchy(false);

在这里查看官方文档

https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2

它指出

如果您的页面包含布局转换,则这些布局转换必须将动画父层次结构设置为 false。请注意,如果在布局 xml 文件中有一个包含 animateLayoutChanges=“true” 的视图组,则布局转换会自动添加到该视图组中。在膨胀 xml 布局后,您需要在该视图组上手动调用 getLayoutTransition().setAnimateParentHierarchy(false)。

与你的错误日志在第一行谈论的一样

Fatal Exception: java.lang.IllegalStateException: Page(s) contain a ViewGroup with a LayoutTransition (or animateLayoutChanges="true"), which interferes with the scrolling animation. Make sure to call getLayoutTransition().setAnimateParentHierarchy(false) on all ViewGroups with a LayoutTransition before an animation is started.

答案 2

我在使用时遇到了第二个错误。如上文所述,在我的布局中,我发现了,然后在片段中我调用viewPager2fragmentviewGroupanimateLayoutChanges="true"OnViewCreated

viewGroup.getLayoutTransition().setAnimateParentHierarchy(false);

这解决了我的问题!


推荐