简体   繁体   中英

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

I am getting this error in my Pre-Launch Reports, but it doesn't show any references to my .java files. Can anyone please shed some light on this?

I'm using a ViewPager2 (1.0.0) with page transformations.

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)

I found this page about ViewPager2 with reference to a similar error being a bug of ViewPager2.

Possibly relevant:

I only started getting this error very recently. I recently changed my code in the following manner. I'm overriding the getItemCount() method, and the NUM_PAGES value that it returns varies depending on a selectable option elsewhere. Previously I would just recreate() the Activity holding all the Fragments when this option is toggled. Instead, now I call Objects.requireNonNull(recyclerViewAdapter).notifyDataSetChanged(); and it works nicely. I'm not sure if this is the correct way to add/remove or show/hide the rightmost fragment, as other options leaves an empty page where the fragment normally would be.

EDIT: I removed my PageTransformer from the ViewPager2 - which only sets the alpha - and the error is currently gone. But I think it may be co-incidence. I haven't seen the error 'in the wild', only in the Pre-Launch Reports intermittently.

EDIT: I have no ViewGroups in my Fragments with layout animations:不动画视图组

This error can occur if you have a ViewGroup with animateLayoutChanges set to true in your ViewPager2's child fragments.

If this is the case, you need to tell that ViewGroup to only animate its own layout changes and not those of the ViewPager2, by calling

viewGroup.layoutTransition.setAnimateParentHierarchy(false)

as detailed in that answer .

The ViewPager2 documentation explains it :

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.

I had this problem as I was overriding getItemCount in the adapter so I can effectively lazy-load a large number of pages. The issue for me came when I tried to call viewPager.setCurrentItem(newPosition); for an item that was a long way off and the ViewPager tried to animate that change. After I changed the call to viewPager.setCurrentItem(newPosition, false); (which disabled smooth scrolling), I stopped getting this error.

It's an bug in ViewPager2, which is caused when calculating the offset of the first item in RecyclerView. Try removing start/left side separator, and paddingLeft/paddingStart.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM