NestedScrollView+Viewpager+Recycleview的滑动冲突

本文介绍了如何处理一个页面中包含多个Recycleview和Viewpager的滑动冲突问题。在华为7.0设备上出现了Recycleview item事件冲突,而NestedScrollView阻止了Viewpager的手动滑动。通过自定义NestedScrollView,解决了这个问题,实现了完美滑动体验。

最新业务需求变化,一个页面多个Recycleview+Viewpager,viewpager实现左右滑动,且可以手动滑动,页面逻辑简单,就是数据比较大,最初的时候实现有滑动冲突,后边使用NestedScrollView可以实现滑动,但是Viewpager不能实现手动滑动,Recycleview的item事件冲突(这个只在华为7.0手机上出现,华为8.0及三星手机上未发现问题)网上也是各种找,后边看是NestedScrollView里面的onInterceptTouchEvent给拦截了

下面贴一下封装完成的NestedScrollView的类,完美实现

public class JudgeNestedScrollView extends NestedScrollView {
    private boolean isNeedScroll = true;
    private float xDistance, yDistance, xLast, yLast;
    private int scaledTouchSlop;

    public JudgeNestedScrollView(@NonNull Context context) {
        super(context);
    }

    public JudgeNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public JudgeNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                Log.e("SiberiaDante", "xDistance :" + xDistance + "---yDistance:" + yDistance);
                return !(xDistance >= yDistance || yDistance < scaledTouchSlop) && isNeedScroll;

        }
        return super.onInterceptTouchEvent(ev);
    }


    /*
    改方法用来处理NestedScrollView是否拦截滑动事件
     */
    public void setNeedScroll(boolean isNeedScroll) {
        this.isNeedScroll = isNeedScroll;
    }
}

参考:http://www.cnblogs.com/shen-hua/p/8052459.html

贴一下布局:

<com.xxxx.app.widget.JudgeNestedScrollView
    android:id="@+id/scorll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:focusableInTouchMode="true"

    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:visibility="gone">

    <com.zhy.autolayout.AutoLinearLayout
        android:id="@+id/lin_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F1F1F1"
        android:orientation="vertical">

        <!--轮播图-->
        <FrameLayout
            android:id="@+id/view_lbt"
            android:layout_width="wrap_content"
            android:layout_height="590px" />
        <!--详情-->

        <TextView
            android:id="@+id/tv_header_word"
            android:layout_width="match_parent"
            android:layout_height="60px"
            android:layout_alignTop="@+id/rv"
            android:layout_marginRight="55px"
            android:background="@color/gainsboro"
            android:paddingLeft="40px"
            android:paddingTop="10px"
            android:text="A"
            android:textColor="@color/black"
            android:textSize="14sp"
            android:visibility="visible" />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:focusable="false"
            android:layout_height="wrap_content"
            ></android.support.v7.widget.RecyclerView>


    </com.zhy.autolayout.AutoLinearLayout>

</com.xxxx.app.widget.JudgeNestedScrollView>

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值