ScrollView嵌套RecyclerView滑动冲突解决
  ttOzQgS7km1w 2023年12月12日 15 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 方法测量。
解决方案详见:

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

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

<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手机不会出现这种情况,仅需处理卡顿问题。

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

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

方式三:使用 NestedScrollView 嵌套 RecyclerView

这种方式较之 ScrollView 嵌套 RecyclerView 更简单,不用复写很多。布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="---HeaderView---"
            android:focusable="true"
            android:focusableInTouchMode="true"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

NestedScrollView 嵌套 RecyclerView也会出现 滑动卡顿问题,这是只需要禁止 RecyclerView 的滑动即可,通过在 xml 中给 RecyclerView 添加 android:nestedScrollingEnabled="false" 或者 直接设置 recyclerView.setNestedScrollingEnabled(false); 即可解决。

最后,还要说一下,不管是 ScrollView 还是 NestedScrollView 嵌套 RecyclerView 都会存在这样的问题:

  1. 初次进入页面,RecyclerView会直接显示在页面顶部,而不是 Header 部分,这是因为这种嵌套页面焦点被 RecyclerView 获取。解决方式:给 Header 部分View 添加属性:
android:focusable="true"
android:focusableInTouchMode="true"
  1. 两种方式嵌套,RecyclerView 会失去复用性,在数据量很大时,会存在内存消耗问题。解决方式之一:若Header部分是简单布局,可将其作为 RecyclerView 的头部部分,这种方式很常见。若Header部分比较复杂,如何保证 RecyclerView 的复用性,待尝试。

作者:Coralline_xss

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月12日 0

暂无评论

推荐阅读
  420SY9k1P3KI   2023年12月12日   25   0   0 html控件qthtmlqt控件
ttOzQgS7km1w