HorizontalScrollView와 ListView 슬라이딩 이벤트 충돌 해결

HorizontalScrollView와 ListView 슬라이딩 이벤트가 충돌하여 ListView 드롭다운이 민감하지 않습니다.다음은 비교적 완벽한 실현 방식을 제시합니다. 만약에 세로로 된 ScrollView라면 바꾸면 됩니다.
package wosai.com.membermarketing.util.layout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

/**
 *    HorizontalScrollView,        
 *
 * Created by yuyh on 2015/10/30.
 */
public class CustomHorizontalScrollView extends HorizontalScrollView {

    float lastX, lastY;

    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    public CustomHorizontalScrollView(Context context) {
        super(context);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    public CustomHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    /**
     *         <    ,      ,    。   ScrollView, '<'  '>'
     */
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (Math.abs(distanceY) < Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }
}

좋은 웹페이지 즐겨찾기