RecyclerView-FastScroller
RecyclerView-FastScroller copied to clipboard
Problem when using with ViewPager2 widget
There seems to be a problem when using the ViewPager2 widget to display a number of RecyclerViews each with a fast scroller. When swiping back and forwards between views the fast scroller stops working or crashes the app.
The problem seems to be the fast scroller's onDetachedFromWindow() function gets called (which de-attaches the fast scroller) when swiping away from a view meaning that it no longer works when the view is restored.
can you post the crash logs?
2020-10-27 18:37:58.094 27196-27196/uk.org.freeflight.bellfinder E/AndroidRuntime: FATAL EXCEPTION: main Process: uk.org.freeflight.bellfinder, PID: 27196 java.lang.IllegalStateException: Observer com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller$adapterDataObserver$1$1@7296234 was not registered. at android.database.Observable.unregisterObserver(Observable.java:69) at androidx.recyclerview.widget.RecyclerView$Adapter.unregisterAdapterDataObserver(RecyclerView.java:7502) at com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller.detachFastScrollerFromRecyclerView() at com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller.onDetachedFromWindow() at android.view.View.dispatchDetachedFromWindow(View.java:15564) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3187) at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3179) at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4715) at android.view.ViewGroup.removeViewAt(ViewGroup.java:4665) at androidx.recyclerview.widget.RecyclerView$5.removeViewAt(RecyclerView.java:911) at androidx.recyclerview.widget.ChildHelper.removeViewAt(ChildHelper.java:168) at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeViewAt(RecyclerView.java:8911) at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:9182) at androidx.recyclerview.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1433) at androidx.recyclerview.widget.LinearLayoutManager.recycleViewsFromStart(LinearLayoutManager.java:1483) at androidx.recyclerview.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1558) at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1582) at androidx.recyclerview.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1395) at androidx.recyclerview.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1124) at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:1937) at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5447) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874) at android.view.Choreographer.doCallbacks(Choreographer.java:686) at android.view.Choreographer.doFrame(Choreographer.java:618) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6186) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Faced the same bug, solved by putting this with try-catch in onResume() of the fragment
try {
binding.mediaFastscroller.attachFastScrollerToRecyclerView(binding.rvMoments)
} catch (e: Exception) {
e.printStackTrace()
}
It's not the perfect solution perhaps, but it works. :)