修复4.3.3中AndroidPAG在recyclerview使用导致隐藏被显示问题 #1710
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
问题描述
在4.3.3版本中
recyclerview
使用时会有一些情况导致被隐藏的PAG在onResume被强制设置可见,这在业务上是不可接受。问题视频如下:
https://github.com/Tencent/libpag/assets/22413240/e9f6ac9f-a626-4197-a385-75993e6a958d
另外附上最小化复现Demo:
Demo链接
问题原因
在大多数情况调用
View#setVisibility
会触发View#onVisibilityAggregated
但是在recyclerview
情况不一定符合PAG开发者的预期。而
PAGView#onVisibilityAggregated
做了一些可见性判断处理。RecyclerView.Adapter#onBindViewHolder
回调时item根视图没有附加到RecyclerView
之上。在
View#setVisibility
时会触发View#setFlags
回调,并在setFlags
函数间接回调到View#onVisibilityAggregated
。在继续往下追溯源码的就是((ViewGroup) mParent).isShown())相关问题,这里就是由于
RecyclerView
没有绑定item视图问题(isShown会不断向上找父view,但是此时父View没有)。修复方式
在调用
View#setVisibility
时判断是否需要手动调用Pag的checkVisible函数。注:这里之所以把View中所有if条件判定直接搬迁过来不做逻辑裁剪是为了可读性和维护成本(未来源码改动也只是复制粘贴),且编译器会自动消除else分支。