在實(shí)際開發(fā)中有可能會(huì)有ScrollView和ListView組合的頁面摊滔,需要自己解決兩者的滑動(dòng)沖突
問題描述
場景
解決方法
不同于設(shè)置ListView與內(nèi)容相等的高度來解決
而是實(shí)現(xiàn) 當(dāng)手指觸摸區(qū)域是ListView時(shí)則滑動(dòng)事件交給ListView
public class EChangeScrollView extends ScrollView {
private int left;
private int top;
private int right;
private int bottom;
private boolean isNoIntercept = false;
private int adjustHeight;
private int adjustWidth;
public EChangeScrollView(Context context) {
super(context);
}
public EChangeScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 設(shè)置修正值剔交,位置修正
* View的getTop并不能代表觸摸的getX扇救,按需修正
* @param width
* @param height
*/
public void setAdjustDistance(int width, int height){
adjustWidth = width;
adjustHeight = height;
}
/**
* 這個(gè)控件為了解決ScrollView和ListView的滑動(dòng)沖突步势,所以傳入ListView国撵,如果有其他需要可以自行修改
* @param lv
*/
public void setListView(ListView lv){
if (lv == null){
top = 0;
left = 0;
right = 0;
bottom = 0;
}else {
top = adjustHeight + lv.getTop();
bottom = adjustHeight + lv.getBottom();
left = adjustWidth + lv.getLeft();
right = adjustWidth + lv.getRight();
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//抬起結(jié)束觸摸瞪醋,事件重新交給ScrollView
if (ev.getAction() == MotionEvent.ACTION_UP){
isNoIntercept = false;
}
//為true則事件交給子View消費(fèi)
if (isNoIntercept){
return false;
}
float x = ev.getX();
float y = ev.getY();
if (ev.getAction() == MotionEvent.ACTION_DOWN){
if (x>left && x<right && y>top && y<bottom){
//按下時(shí)選中ListView區(qū)域瓤的,則事件交給子View處理
isNoIntercept = true;
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}
自己的項(xiàng)目在使用休弃,完畢