ScrollView嵌套RecyclerView滑动冲突解决

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

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

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

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

方式一:禁止RecyclerView滑动

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

LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
    @Override
    public boolean canScrollVertically() {
        // 直接禁止垂直滑动
        return false;
    }
}

源码实现:
/**
 * @return true if {@link #getOrientation()} is {@link #VERTICAL}
 */
@Override
public boolean canScrollVertically() {
    return mOrientation == VERTICAL;
}

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

public class ScrollLinearLayoutManager extends LinearLayoutManager {

private boolean isScrollEnable = true;

public ScrollLinearLayoutManager(Context context) {
    super(context);
}

public ScrollLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

public ScrollLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public boolean canScrollVertically() {
    return isScrollEnable && super.canScrollVertically();
}

/**
 * 设置 RecyclerView 是否可以垂直滑动
 * @param isEnable
 */
public void setScrollEnable(boolean isEnable) {
    this.isScrollEnable = isEnable;
}
}
方式二:通过View事件分发机制,进行事件拦截

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

public class ScrollInterceptScrollView extends ScrollView {
private int downX, downY;
private int mTouchSlop;

public ScrollInterceptScrollView(Context context) {
    this(context, null);
}

public ScrollInterceptScrollView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ScrollInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ScrollInterceptScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            downX = (int) ev.getRawX();
            downY = (int) ev.getRawY();
            break;
        case MotionEvent.ACTION_MOVE:
            int moveY = (int) ev.getRawY();
            // 判断是否滑动,若滑动就拦截事件
            if (Math.abs(moveY - downY) > mTouchSlop) {
                return true;
            }
            break;
        default:
            break;
    }

    return super.onInterceptTouchEvent(ev);
}
}
还有一种方式是重写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"
代码如下:

<RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:descendantFocusability="blocksDescendants">

   <android.support.v7.widget.RecyclerView
      android:id="@+id/menuRv"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="@dimen/margin_16"
      android:layout_marginRight="@dimen/margin_16"/>
 </RelativeLayout>

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

注:

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

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


猜你喜欢

转载自blog.csdn.net/qq_29443203/article/details/79208286