ScrollView嵌套RecyclerView滑动冲突解决

转载自:https://blog.csdn.net/coralline_xss/article/details/72887136

最近发现自己负责的项目中,有使用 ScrollView 嵌套 RecyclerView 的地方,但是没有做任何针对滑动冲突的处理,于是就想看下为什么没有做这个处理,便进行了如下测试,发现了几个由其的问题。

测试场景:页面内容包括类似 HeaderView 的部分 + RecyclerView列表部分,布局是垂直方向,此处列表之上的布局内容并不是以 header add到RecyclerView上的。
测试结果:

  1. 在部分手机上,如果列表内容过少,只会造成很小程度的滑动,这种滑动冲突是没法察觉到的,很容易忽略滑动冲突;
  2. 在部分手机上,列表内容过多时,滑动 RecyclerView 部分,会很卡顿,滑动 仅ScrollView 部分,很顺畅,笔者试过Vivio x5plus 5.0系统会出现这样的情况;
  3. 在部分手机上,不管列表内容多少,当滑动时,只有 RecyclerView部分滑动,Header顶部布局内容固定,没有一起滑动,Redmi Note4 6.0系统则会出现这种结果。

一般地,对于第三种结果,一看就知道不是我们想要的结果,这种便是滑动冲突,但是不仔细时,均会因前两种结果而忽略了滑动冲突。现在,将针对以上的滑动冲突提供几种不同的解决方法。

方式一:禁止RecyclerView滑动

最直接的方式是将布局管理器中判断可滑动的方法,直接返回false,代码如下:

 
  1. LinearLayoutManager layoutManager = new LinearLayoutManager(context) {

  2. @Override

  3. public boolean canScrollVertically() {

  4. // 直接禁止垂直滑动

  5. return false;

  6. }

  7. }

  8.  
  9. 源码实现:

  10. /**

  11. * @return true if {@link #getOrientation()} is {@link #VERTICAL}

  12. */

  13. @Override

  14. public boolean canScrollVertically() {

  15. return mOrientation == VERTICAL;

  16. }

另一种方式便是重写布局管理器,以 LinearLayoutManager 为例,重写滑动方法,并且通过外部手动设置,代码如下:

 
  1. public class ScrollLinearLayoutManager extends LinearLayoutManager {

  2.  
  3. private boolean isScrollEnable = true;

  4.  
  5. public ScrollLinearLayoutManager(Context context) {

  6. super(context);

  7. }

  8.  
  9. public ScrollLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {

  10. super(context, orientation, reverseLayout);

  11. }

  12.  
  13. public ScrollLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

  14. super(context, attrs, defStyleAttr, defStyleRes);

  15. }

  16.  
  17. @Override

  18. public boolean canScrollVertically() {

  19. return isScrollEnable && super.canScrollVertically();

  20. }

  21.  
  22. /**

  23. * 设置 RecyclerView 是否可以垂直滑动

  24. * @param isEnable

  25. */

  26. public void setScrollEnable(boolean isEnable) {

  27. this.isScrollEnable = isEnable;

  28. }

  29. }

方式二:通过View事件分发机制,进行事件拦截

重写父控件,让父控件 ScrollView 直接拦截滑动事件,不向下分发给 RecyclerView,具体是定义一个ScrollView子类,重写其 onInterceptTouchEvent()方法,代码如下:

 
  1. public class ScrollInterceptScrollView extends ScrollView {

  2. private int downX, downY;

  3. private int mTouchSlop;

  4.  
  5. public ScrollInterceptScrollView(Context context) {

  6. this(context, null);

  7. }

  8.  
  9. public ScrollInterceptScrollView(Context context, AttributeSet attrs) {

  10. this(context, attrs, 0);

  11. }

  12.  
  13. public ScrollInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr) {

  14. super(context, attrs, defStyleAttr);

  15. mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

  16. }

  17.  
  18. @TargetApi(Build.VERSION_CODES.LOLLIPOP)

  19. public ScrollInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

  20. super(context, attrs, defStyleAttr, defStyleRes);

  21. mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

  22. }

  23.  
  24. @Override

  25. public boolean onInterceptTouchEvent(MotionEvent ev) {

  26. int action = ev.getAction();

  27. switch (action) {

  28. case MotionEvent.ACTION_DOWN:

  29. downX = (int) ev.getRawX();

  30. downY = (int) ev.getRawY();

  31. break;

  32. case MotionEvent.ACTION_MOVE:

  33. int moveY = (int) ev.getRawY();

  34. // 判断是否滑动,若滑动就拦截事件

  35. if (Math.abs(moveY - downY) > mTouchSlop) {

  36. return true;

  37. }

  38. break;

  39. default:

  40. break;

  41. }

  42.  
  43. return super.onInterceptTouchEvent(ev);

  44. }

  45. }

还有一种方式是重写LinearLayoutManager,对其重写 onMeasure 方法测量。
解决方案详见:http://www.cnblogs.com/woaixingxing/p/6098726.html

但是,现在又出现了另一个问题,笔者使用上述两种方式,不管是直接禁止RecyclerView不可滑动,重写LinearLayoutManager,还是直接拦截滑动事件不分发给RecyclerView,Vivio x5plus 5.0手机确实不会卡顿,但是 Redmi Note4 6.0上,RecyclerView会出现显示不全的情况。

针对这种情形,使用网上的方法一种是使用 RelativeLayout 包裹 RecyclerView 并设置属性:android:descendantFocusability="blocksDescendants"
代码如下:

 
  1. <RelativeLayout

  2. android:layout_width="match_parent"

  3. android:layout_height="wrap_content"

  4. android:descendantFocusability="blocksDescendants">

  5.  
  6. <android.support.v7.widget.RecyclerView

  7. android:id="@+id/menuRv"

  8. android:layout_width="match_parent"

  9. android:layout_height="wrap_content"

  10. android:layout_marginLeft="@dimen/margin_16"

  11. android:layout_marginRight="@dimen/margin_16"/>

  12. </RelativeLayout>

这种方式网上的说法是主要针对 6.0手机的解决方式,在 Redmi Note4 6.0手机上确实可以显示完全,Vivio 5.0手机不会出现这种情况,仅需处理卡顿问题。

注:

android:descendantFocusability="blocksDescendants",该属>性是当一个view 获取焦点时,定义 ViewGroup 和其子控件直接的关系,常用来>解决父控件的焦点或者点击事件被子空间获取。
属性的值有三种:

  • beforeDescendants: ViewGroup会优先其子控件获取焦点
  • afterDescendants: ViewGroup只有当其子控件不需要获取焦点时才获取焦点
  • blocksDescendants: ViewGroup会覆盖子类控件而直接获得焦点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值