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

鄂伦春网站建设安卓手机应用市场

鄂伦春网站建设,安卓手机应用市场,wordpress 极简设计,建设工程有限公司企业网站背景 ViewPager2内嵌套横向滑动的RecyclerView,会有滑动冲突的情况,引入官方提供的NestedScrollableHost类可以解决冲突问题,但是有一些瑕疵,滑动横向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.yayakq.cn/news/845394/

相关文章:

  • 旅游景区网站模板wordpress 3.9.2 下载
  • 建设学院实验网站的作用wordpress显示图片
  • 做网站还需要兼容ie6吗wordpress改数据库
  • 陕西宁德建设工程有限公司网站phpcms做网站
  • 做外单的网站网页界面设计特点
  • 做婚恋网站多少钱网页制作代码模板
  • 学校培训网站开发色块网站
  • 网站维护总结烟台seo关键词排名
  • 建设通网站原理网站运营这么做
  • 如何网站建设平台好听好记的网站域名
  • 网站索引下降如何解决wordpress整合ecms同步登录
  • 郑州市城乡建设规划网站给房地产公司做网站的公司
  • 如何设计网站后台vi品牌包装
  • 衡阳企业网站眼镜商城网站建设方案
  • 网站建设logo显示怎么设置h5免费制作平台易企秀
  • 创建wordpress网站外贸自建站平台排名
  • 做网站简约学校网站网上商城电商项目的管理步骤
  • 哪些网站上推广比较好邢台做企业网站
  • 快速学做网站台州网站设计哪家好
  • 广州网站优化哪里有网站建设应该考虑哪些问题
  • 好的做网站公司页面设计元素
  • 可以找厂家的网站本科毕业 做网站编辑
  • 学校网站建设工作内容网站做备案关停会显示什么
  • 四川电子商务网站南通网站建设规划书
  • 个人怎么做优惠券网站pc网站 手机网站 微网站
  • 做门户网站用什么模板好制作二维码网站免费
  • 高端营销网站外贸公司名称
  • 网站设计与制作说明软件定做定制开发
  • 网站开发 入门 pdf网站推广策划的思路包括哪些内容
  • 能接做网站的活的网站wordpress 倒计时插件