解决ViewPager2嵌套HorizontalScrollView的滑动冲突问题
解决ViewPager2嵌套HorizontalScrollView的滑动冲突问题
问题描述
横向ViewPager2内部嵌套纵向NestedScrollView,NestedScrollView中包含承载TableLayout的横向HorizontalScrollView。当HorizontalScrollView滑动至左右末端时,ViewPager2无法正常响应横向滑动操作,需多次同向滑动才可能触发页面切换,甚至完全无响应。
问题原因
HorizontalScrollView滑动到边界时,仍会拦截横向触摸事件,未将事件传递给上层的ViewPager2,导致ViewPager2无法触发页面切换逻辑。
解决方案
自定义HorizontalScrollView修改事件拦截逻辑
创建自定义HorizontalScrollView,重写触摸事件处理方法,当滑动到边界时主动释放事件,让上层ViewPager2能接收并处理横向滑动。
代码示例:
class PassThroughHorizontalScrollView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : HorizontalScrollView(context, attrs, defStyleAttr) { override fun onTouchEvent(ev: MotionEvent): Boolean { val result = super.onTouchEvent(ev) // 判断是否处于左右边界 val isAtLeftEdge = scrollX == 0 val isAtRightEdge = scrollX + width == getChildAt(0).width if (ev.action == MotionEvent.ACTION_MOVE && (isAtLeftEdge || isAtRightEdge)) { // 允许上层ViewPager2拦截事件 parent.requestDisallowInterceptTouchEvent(false) return false } return result } override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { val intercepted = super.onInterceptTouchEvent(ev) if (intercepted) { val isAtLeftEdge = scrollX == 0 val isAtRightEdge = scrollX + width == getChildAt(0).width // 边界时不拦截事件,交给上层处理 if (isAtLeftEdge || isAtRightEdge) return false } return intercepted } }
替换布局中的HorizontalScrollView
将原布局里的HorizontalScrollView替换为自定义的PassThroughHorizontalScrollView(注意替换为自己的包名):
<com.your.package.name.PassThroughHorizontalScrollView android:id="@+id/horizontalScrollView" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1"> <TableLayout android:id="@+id/topLogSourcesTableLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingHorizontal="15dp" android:paddingBottom="15dp" android:stretchColumns="0"> </TableLayout> </com.your.package.name.PassThroughHorizontalScrollView>
可选优化
若NestedScrollView的纵向滑动也存在潜在冲突,可给ViewPager2添加属性:
android:descendantFocusability="blocksDescendants"
内容的提问来源于stack exchange,提问作者Anon
相关产品推荐
相关产品推荐

