1. 程式人生 > >NestedScrollView+Viewpager+Recycleview的滑動衝突

NestedScrollView+Viewpager+Recycleview的滑動衝突

最新業務需求變化,一個頁面多個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>