ListVIew中包含水平滑动控件,左右滑动时容易触发上下滑动

 

自定义ListView

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

public class LiveCustomListView extends ListView {
public LiveCustomListView(Context context) {
super(context);
}

public LiveCustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

private float mLastX;
private float mLastY;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//避免左右滑动水平图片时容易触发上下滑动列表
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastX = ev.getX();
mLastY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(mLastX - ev.getX()) > Math.abs(mLastY - ev.getY())) {
return false;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return super.onInterceptTouchEvent(ev);
}
}

posted on 2016-09-18 12:41  guangdeshishe  阅读(1567)  评论(0编辑  收藏  举报

导航