解决HorizontalScrollView和ListView滑动冲突

话不多说,直接附上代码:

/**
 * 完美解决listview与HorizontalScrollView滑动事件冲突
 * @author jy
 *
 */
public class BouncyHScrollView extends HorizontalScrollView {


private static final int MAX_X_OVERSCROLL_DISTANCE = 200;
private WeakReferencewReference;
private int mMaxXOverscrollDistance;
private GestureDetector mGestureDetector;

public BouncyHScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
wReference=new WeakReference(context);
initBounceDistance();
}


public BouncyHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
wReference=new WeakReference(context);
initBounceDistance();
}


public BouncyHScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
wReference=new WeakReference(context);
initBounceDistance();
}


private void initBounceDistance() {
try {
final DisplayMetrics metrics = wReference.get().getResources()
.getDisplayMetrics();
mMaxXOverscrollDistance = (int) (metrics.density * MAX_X_OVERSCROLL_DISTANCE);
mGestureDetector = new GestureDetector(new YScrollDetector());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

}


@SuppressLint("NewApi")
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// 这块是关键性代码
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
scrollRangeX, scrollRangeY, mMaxXOverscrollDistance,
maxOverScrollY, isTouchEvent);
}


@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
// return super.onInterceptTouchEvent(ev);
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); 
}


// Return false if we're scrolling in the x direction
    class YScrollDetector extends SimpleOnGestureListener {


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return super.onScroll(e1, e2, distanceX, distanceY);
}
   
    }
    




}

你可能感兴趣的:(listview)