Страницы содержат ViewGroup с LayoutTransition (или animateLayoutChanges = "true"), который мешает прокрутке анимации - PullRequest
3 голосов
/ 31 января 2020

Я использую фрагменты с ViewPager2 и замечаю два соответствующих исключения IllegalStateException (я не могу воспроизвести его сам), возникающих на таких устройствах, как Xiaomi, Yulong, asus, vivo, работающие Android 8 или 9:

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, которая, как я правильно понял, похоже, сводится к установке animateLayoutChanges в false на любом родительском макете viewPager2, и это то, что я сделал. К сожалению, это не решило мою проблему. Затем я нашел другой поток: java .lang.IllegalStateException: Страница может быть смещена только на положительную величину , которая, кажется, не очень помогает. Есть идеи, что еще может вызвать проблему?

1 Ответ

2 голосов
/ 01 февраля 2020

Вам необходимо выполнить это на каждой из ваших страниц / фрагментов.

 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

В нем говорится, что

Если ваши страницы содержат LayoutTransitions, то для этих LayoutTransitions должно быть установлено значение animateParentHierarchy, равное false. Обратите внимание, что если у вас есть ViewGroup с animateLayoutChanges = "true" в вашем файле макета xml, LayoutTransition автоматически добавляется в эту ViewGroup. Вам нужно будет вручную вызвать getLayoutTransition (). SetAnimateParentHierarchy (false) для этой ViewGroup после того, как вы накачаете макет xml.

То же, о чем говорится в вашем журнале ошибок, в первой строке

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.
...