当前位置: 首页 > news >正文

Android笔记(四十):ViewPager2嵌套RecyclerView滑动冲突进一步解决

背景

在这里插入图片描述
ViewPager2内嵌套横向滑动的RecyclerView,会有滑动冲突的情况,引入官方提供的NestedScrollableHost类可以解决冲突问题,但是有一些瑕疵,滑动横向RecyclerView到顶部,按住它不放手继续往左拖再往右拖,这时候会发现外层ViewPager2滑动了,而不是横向RecyclerView滑动,于是参考NestedScrollableHost进行逻辑完善

完整代码

  • 主要是增加判断外层ViewPager2是否可滚动来设置是否允许父View拦截事件
open class NestRecyclerView @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null
): RecyclerView(context, attrs) {private var initialX = 0fprivate var initialY = 0fprivate val parentViewPager: ViewPager2?get() {var v: View? = parent as? Viewwhile (v != null && v !is ViewPager2) {v = v.parent as? View}return v as? ViewPager2}private fun canViewScroll(target: View?, orientation: Int, delta: Float): Boolean {val direction = -delta.sign.toInt()return when (orientation) {0 -> target?.canScrollHorizontally(direction) ?: false1 -> target?.canScrollVertically(direction) ?: falseelse -> throw IllegalArgumentException()}}override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {val orientation = parentViewPager?.orientation ?: return super.onInterceptTouchEvent(event)if (!canViewScroll(this, orientation, -1f) && !canViewScroll(this, orientation, 1f)) {return super.onInterceptTouchEvent(event)}when (event?.action) {MotionEvent.ACTION_DOWN -> {initialX = event.xinitialY = event.yparent.requestDisallowInterceptTouchEvent(true)}MotionEvent.ACTION_MOVE -> {val dx = event.x - initialXval dy = event.y - initialYval isVpHorizontal = orientation == ViewPager2.ORIENTATION_HORIZONTALif (isVpHorizontal == dy.absoluteValue > dx.absoluteValue) {parent.requestDisallowInterceptTouchEvent(false)} else {if (canViewScroll(this, orientation, if (isVpHorizontal) dx else dy)) {parent.requestDisallowInterceptTouchEvent(true)} else {if (canViewScroll(parentViewPager, orientation, if (isVpHorizontal) dx else dy)) {parent.requestDisallowInterceptTouchEvent(false)} else {parent.requestDisallowInterceptTouchEvent(true)}}}}}return super.onInterceptTouchEvent(event)}
}

向上滑动AppBarLayout不联动问题

如果布局CoordinatorLayout + AppBarLayout + ViewPager2内嵌套横向滑动的RecyclerView,这时拖拽横向滑动的RecyclerView向上移,AppBarLayout不会跟着向上移

原因分析

  • 拖拽横向滑动的RecyclerView向上移时,CoordinatorLayout.onNestedPreScroll内的lp.isNestedScrollAccepted(type)返回false,造成AppBarLayout没有执行scroll
    在这里插入图片描述

  • lp.isNestedScrollAccepted(type)被赋值的地方,会根据AppBarLayout$Behavior.onStartNestedScroll返回的accepted进行赋值
    在这里插入图片描述

  • AppBarLayout$Behavior.onStartNestedScroll内,会判断nestedScrollAxes的值不是2就返回false
    在这里插入图片描述

  • RecyclerView也支持嵌套滑动。startNestedScroll是由NestedScrollingChildHelper实现的,它会将嵌套滑动上传,也就是NestedScrollingChild都会将嵌套滑动先交给NestedScrollingParent处理。

class RecyclerView...public boolean onInterceptTouchEvent(MotionEvent e) {if (mLayoutSuppressed) {// When layout is suppressed,  RV does not intercept the motion event.// A child view e.g. a button may still get the click.return false;}// Clear the active onInterceptTouchListener.  None should be set at this time, and if one// is, it's because some other code didn't follow the standard contract.mInterceptingOnItemTouchListener = null;if (findInterceptingOnItemTouchListener(e)) {cancelScroll();return true;}if (mLayout == null) {return false;}final boolean canScrollHorizontally = mLayout.canScrollHorizontally();final boolean canScrollVertically = mLayout.canScrollVertically();if (mVelocityTracker == null) {mVelocityTracker = VelocityTracker.obtain();}mVelocityTracker.addMovement(e);final int action = e.getActionMasked();final int actionIndex = e.getActionIndex();switch (action) {case MotionEvent.ACTION_DOWN:if (mIgnoreMotionEventTillDown) {mIgnoreMotionEventTillDown = false;}mScrollPointerId = e.getPointerId(0);mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);if (mScrollState == SCROLL_STATE_SETTLING) {getParent().requestDisallowInterceptTouchEvent(true);setScrollState(SCROLL_STATE_DRAGGING);stopNestedScroll(TYPE_NON_TOUCH);}// Clear the nested offsetsmNestedOffsets[0] = mNestedOffsets[1] = 0;int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;if (canScrollHorizontally) {nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL;}if (canScrollVertically) {nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;}startNestedScroll(nestedScrollAxis, TYPE_TOUCH);break;...return mScrollState == SCROLL_STATE_DRAGGING;}

这里RecyclerView是横向的,所以nestedScrollAxis会被赋值为1,RecyclerView内调用startNestedScroll会向上层view传递,直到交给CoordinatorLayout处理,而CoordinatorLayout在调用onStartNestedScroll的时候,AppBarLayout$Behavior.onStartNestedScroll又返回false了,造成CoordinatorLayout回调onNestedPreScroll(由RecyclerView在ACTION_MOVE时调用dispatchNestedPreScroll触发)时无法调用AppBarLayout的滚动。

解决方法

在CoordinatorLayout调用onStartNestedScroll的时候不处理横向的情况,就不会导致lp.isNestedScrollAccepted(type)被赋值

class NestedCoordinatorLayout @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null
): CoordinatorLayout(context, attrs) {override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean {return if (axes and ViewCompat.SCROLL_AXIS_HORIZONTAL != 0) {false} else super.onStartNestedScroll(child, target, axes, type)}
}
http://www.lryc.cn/news/512143.html

相关文章:

  • POS系统即销售点系统 文档与数据库设计
  • 安全合规遇 AI 强援:深度驱动行业发展新引擎 | 倍孜网络CEO聂子尧出席ICT深度观察报告会!
  • 算法进阶:贪心算法
  • C++ 设计模式:工厂方法(Factory Method)
  • 手机联系人 查询 添加操作
  • 【LeetCode】2506、统计相似字符串对的数目
  • 金仓数据库对象访问权限的管理
  • Qt 中实现系统主题感知
  • Modbus TCP 报文说明
  • 音视频入门基础:MPEG2-TS专题(24)——FFmpeg源码中,显示TS流每个packet的pts、dts的实现
  • 大模型:OneFitsAll、Time - LLM、LLaTA
  • 连锁餐饮行业数据可视化分析方案
  • Ubuntu 下使用命令行将 U 盘格式化为 ext4、FAT32 和 exFAT 的详细教程
  • 多说话人ASR的衡量指标和有效计算工具包
  • 英伟达(NVIDIA)
  • 【环境配置】Jupyter Notebook切换虚拟环境
  • 嵌入式单片机窗口看门狗控制与实现
  • NiChart 多模态神经影像(structural MRI,functional MRI,and diffusion MRI)处理和分析工具包安装
  • Es搭建——单节点——Linux
  • Python自动化测试之线上流量回放:录制、打标、压测与平台选择
  • k-Means聚类算法 HNUST【数据分析技术】(2025)
  • STM32学习之 按键/光敏电阻 控制 LED/蜂鸣器
  • VUE前端实现防抖节流 Lodash
  • Ubuntu20.04 交叉编译Qt5.15.15 for rk3588
  • Unity编译Android apk包进度奇慢或gradle报错的解决方案
  • 【Qt】多元素控件:QListWidget、QTableWidget、QTreeWidget
  • Docker基础知识 Docker命令、镜像、容器、数据卷、自定义镜像、使用Docker部署Java应用、部署前端代码、DockerCompose一键部署
  • Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
  • 低代码开发中 DDD 领域驱动的页面权限控制
  • 如果你的网站是h5网站,如何将h5网站变成小程序-除开完整重做方法如何快速h5转小程序-h5网站转小程序的办法-优雅草央千澈