1. 程式人生 > >PullToRefreshScrollView巢狀SwipeMenuListView衝突問題解決

PullToRefreshScrollView巢狀SwipeMenuListView衝突問題解決

(一)listView內容不正常顯示(注:網上有很多方法)

重寫ListView的onMeasure方法

  /**
     * 以下要重寫,防止listview在scrollview容器中不能正常顯示的問題
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, expandSpec);

    }

(二)ScrollView的上下事件與ListView的左右事件衝突

重寫ListView

public class MySwipeMenuListView extends SwipeMenuListView {

    public MySwipeMenuListView(Context context) {
        super(context);
        mGestureDetector = new GestureDetector(context,onGestureListener);
    }

    public MySwipeMenuListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mGestureDetector = new GestureDetector(context,onGestureListener);
    }

    public MySwipeMenuListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context,onGestureListener);
    }


//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//
//                //setParentScrollAble(false);//當手指觸到listview的時候,讓父ScrollView交出ontouch許可權,也就是讓父scrollview 停住不能滾動
//                Log.w("MyLog", "onInterceptTouchEvent down");
//            case MotionEvent.ACTION_MOVE:
//
//                Log.w("MyLog", "onInterceptTouchEvent move");
//                break;
//
//            case MotionEvent.ACTION_UP:
//                Log.w("MyLog", "onInterceptTouchEvent up");
//
//            case MotionEvent.ACTION_CANCEL:
//
//                Log.w("MyLog", "onInterceptTouchEvent cancel");
//                //setParentScrollAble(true);//當手指鬆開時,讓父ScrollView重新拿到onTouch許可權
//
//                break;
//            default:
//                break;
//
//        }
//
//        return super.onInterceptTouchEvent(ev) ;
//    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean b =  mGestureDetector.onTouchEvent(ev);
        Log.w("MyLog","-- "+ b+" --");
        return super.onTouchEvent(ev);
    }

    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){
//        @Override
//        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//            Log.w("MyLog","onFling");
//            float x = e2.getX() - e1.getX();
//            float y = e2.getY() - e1.getY();
//            if (Math.abs(y) >= Math.abs(x)){
//                setParentScrollAble(false);
//                return true;
//            }
//            //當手指觸到listview的時候,讓父ScrollView交出ontouch許可權,也就是讓父scrollview 停住不能滾動
//            setParentScrollAble(true);
//            return false;
//        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.w("MyLog", "onScroll");
            if (distanceY != 0 && distanceX != 0) {
            }
            if (Math.abs(distanceY) >= Math.abs(distanceX)) {
                return true;
            }
            //當手指觸到listview的時候,讓父ScrollView交出ontouch許可權,也就是讓父scrollview 停住不能滾動
            setParentScrollAble(false);
            return false;
        }
    };

    /**
     * 是否把滾動事件交給父scrollview
     *
     * @param flag
     */
    private void setParentScrollAble(boolean flag) {
        //這裡的parentScrollView就是listview外面的那個scrollview
        Log.w("MyLog", "setParentScrollAble -- " + flag);
        getParent().requestDisallowInterceptTouchEvent(!flag);
    }
}

附錄:

 <com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrMode="both">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.inetgoes.fangdd.view.MySwipeMenuListView
                android:id="@android:id/list"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>

    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>