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

fix: pager view recycling crash #970

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,36 @@ object PagerViewViewManagerImpl {
fun removeAllViews(parent: NestedScrollableHost) {
val pager = getViewPager(parent)
pager.isUserInputEnabled = false

// Get the RecyclerView inside ViewPager2
val recyclerView = pager.getChildAt(0) as? androidx.recyclerview.widget.RecyclerView

// Clear any pending animations/operations
recyclerView?.suppressLayout(true)

val adapter = pager.adapter as ViewPagerAdapter?
adapter?.removeAll()

// Re-enable layout after cleanup
recyclerView?.suppressLayout(false)
}

fun removeViewAt(parent: NestedScrollableHost, index: Int) {
val pager = getViewPager(parent)
val adapter = pager.adapter as ViewPagerAdapter?
pager.isUserInputEnabled = false

val child = adapter?.getChildAt(index)
// Get the RecyclerView inside ViewPager2
val recyclerView = pager.getChildAt(0) as? androidx.recyclerview.widget.RecyclerView

if (child != null && child.parent != null) {
(child.parent as? ViewGroup)?.removeView(child)
}
// Clear any pending animations/operations
recyclerView?.suppressLayout(true)

val adapter = pager.adapter as ViewPagerAdapter?
adapter?.removeChildAt(index)

// Re-enable layout after cleanup
recyclerView?.suppressLayout(false)

refreshViewChildrenLayout(pager)
}

Expand Down Expand Up @@ -156,10 +170,30 @@ object PagerViewViewManagerImpl {

private fun refreshViewChildrenLayout(view: View) {
view.post {
view.measure(
View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.height, View.MeasureSpec.EXACTLY))
view.layout(view.left, view.top, view.right, view.bottom)
try {
if (view is ViewPager2) {
// Get the RecyclerView inside ViewPager2
val recyclerView = view.getChildAt(0) as? androidx.recyclerview.widget.RecyclerView

// Temporarily suppress layout operations
recyclerView?.suppressLayout(true)

view.measure(
View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.height, View.MeasureSpec.EXACTLY))
view.layout(view.left, view.top, view.right, view.bottom)

// Re-enable layout operations
recyclerView?.suppressLayout(false)
} else {
view.measure(
View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.height, View.MeasureSpec.EXACTLY))
view.layout(view.left, view.top, view.right, view.bottom)
}
} catch (e: Exception) {
// Ignore layout errors during cleanup
}
}
}
}
}