首页 > 代码库 > ListVIew中包含水平滑动控件,左右滑动时容易触发上下滑动

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);
}
}

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