Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConsecutiveViewPager2 裡放 StaggeredGridLayoutManager RecyclerView,切換 ConsecutiveViewPager2 會閃退 #309

Open
klps5603 opened this issue Jun 12, 2024 · 5 comments

Comments

@klps5603
Copy link

您好,我在 ConsecutiveViewPager2 Fragment 中放 StaggeredGridLayoutManager RecyclerView,切換 ConsecutiveViewPager2 時,會出現以下錯誤訊息:

java.lang.NullPointerException: Attempt to read from field 'java.util.ArrayList androidx.recyclerview.widget.StaggeredGridLayoutManager$Span.mViews' on a null object reference in method 'void androidx.recyclerview.widget.StaggeredGridLayoutManager.recycleFromEnd(androidx.recyclerview.widget.RecyclerView$Recycler, int)'
at androidx.recyclerview.widget.StaggeredGridLayoutManager.recycleFromEnd(StaggeredGridLayoutManager.java:1928)
at androidx.recyclerview.widget.StaggeredGridLayoutManager.recycle(StaggeredGridLayoutManager.java:1762)
at androidx.recyclerview.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1681)
at androidx.recyclerview.widget.StaggeredGridLayoutManager.scrollBy(StaggeredGridLayoutManager.java:2154)
at androidx.recyclerview.widget.StaggeredGridLayoutManager.scrollVerticallyBy(StaggeredGridLayoutManager.java:2013)
at androidx.recyclerview.widget.RecyclerView.scrollStep(RecyclerView.java:1972)
at androidx.recyclerview.widget.RecyclerView.scrollByInternal(RecyclerView.java:2071)
at androidx.recyclerview.widget.RecyclerView.scrollBy(RecyclerView.java:1880)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.scrollChild(ConsecutiveScrollerLayout.java:1301)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.scrollDown(ConsecutiveScrollerLayout.java:1219)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.dispatchScroll(ConsecutiveScrollerLayout.java:1101)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.scrollTo(ConsecutiveScrollerLayout.java:1250)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.scrollBy(ConsecutiveScrollerLayout.java:1244)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.scrollChild(ConsecutiveScrollerLayout.java:1301)
at com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout.scrollChildContentToTop(ConsecutiveScrollerLayout.java:1455)
at com.donkingliang.consecutivescroller.ConsecutiveViewPager2.scrollChildContent(ConsecutiveViewPager2.java:176)
at com.donkingliang.consecutivescroller.ConsecutiveViewPager2.access$000(ConsecutiveViewPager2.java:29)
at com.donkingliang.consecutivescroller.ConsecutiveViewPager2$AttachListener.onViewAttachedToWindow(ConsecutiveViewPager2.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:23238)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3698)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3705)
at android.view.ViewGroup.addViewInner(ViewGroup.java:5552)
at android.view.ViewGroup.addView(ViewGroup.java:5328)
at android.view.ViewGroup.addView(ViewGroup.java:5268)
at androidx.recyclerview.widget.RecyclerView$5.addView(RecyclerView.java:889)
at androidx.recyclerview.widget.ChildHelper.addView(ChildHelper.java:107)
at androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:8902)
at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8860)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1647)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
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:1969)
at androidx.recyclerview.widget.RecyclerView$SmoothScroller.onAnimation(RecyclerView.java:12316)
at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5564)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1689)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1698)
at android.view.Choreographer.doCallbacks(Choreographer.java:1153)
at android.view.Choreographer.doFrame(Choreographer.java:1069)
14:34:34.591 E at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1646) (Ask Gemini)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:230)
at android.os.Looper.loop(Looper.java:319)

當我先到A tab Fragment recyclerView 是有多筆內容的,並往下捲動,之後切換到 B tab Fragment recyclerView 沒有資料,向上捲動最外層的 ConsecutiveScrollerLayout,最後切換回 A tab Fragment recyclerView,會出現這個問題

覺得是回收與重繪 StaggeredGridLayoutManager ,佈局出現問題

請問大大有沒有類似的經驗,謝謝

@klps5603
Copy link
Author

klps5603 commented Jun 12, 2024

   <com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">

         <com.donkingliang.consecutivescroller.ConsecutiveViewPager2 
                 android:layout_width="match_parent"
                 android:layout_height="match_parent" />

  </com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout>

更正,ConsecutiveViewPager2 額外包個 ConsecutiveScrollerLayout ,可能有些多餘但可以解決問題
但往下捲動 A Tab Fragment,切換到 B Tab Fragment ,向上捲動最外層 ConsecutiveScrollerLayout (不吸頂狀態), 回到 A Tab Fragment ,捲動 RecyclerView 時,他會自行捲動到 RecyclerView 頂部,不會記錄上次滑動的位置

@donkingliang
Copy link
Owner

如果你想保持RecyclerView 原本的滑动位置,可以重新ConsecutiveViewPager2 的getScrolledViews方法,返回null

@klps5603
Copy link
Author

klps5603 commented Jun 12, 2024

class ConsecutiveViewPager2(context: Context, attrs: AttributeSet? = null) :
        ConsecutiveViewPager2(context, attrs) {
               override fun getScrolledViews(): MutableList<View> {
                    return mutableListOf()
                }

  }

getScrolledViews 應是回一個空 list ,但沒有效果

@klps5603
Copy link
Author

懂了,我改重寫成 return null ,再試試看,謝謝

@klps5603
Copy link
Author

還是有機會出現 RecyclerView 自動滑到頂的問題,當不吸頂的狀態下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants