ListView滑动冲突
2020-02-09 本文已影响0人
码农_薛
一 ListView文件
public class MyListerView extends ListView {
private float mLastY;// 当前触摸点高度
boolean isBotton = false; // listview 是否滑动到底部
boolean isTop = false; // lisview 是否滑动到顶部
public MyListerView(Context context) {
this(context, null);
}
public MyListerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyListerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (totalItemCount == 0) {
isTop = true;
isBotton = true;
return;
}
// 判断 listview 滑动到底部
if (visibleItemCount + firstVisibleItem == totalItemCount) {
View lastVisibleItemView = getChildAt(totalItemCount - firstVisibleItem - 1);
if (lastVisibleItemView != null && lastVisibleItemView.getBottom() <= view.getHeight()) {
// 滑动到了底部
isBotton = true;
} else {
isBotton = false;
}
} else {
isBotton = false;
}
// 判断listview 滑动到顶部
if (firstVisibleItem == 0) {
View firstVisibleItemView = getChildAt(0);
if (firstVisibleItemView != null && firstVisibleItemView.getTop() == 0) {
// 滑动到了底部
isTop = true;
} else {
isTop = false;
}
} else {
isTop = false;
}
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
super.onInterceptTouchEvent(ev);
//不允许上层viewGroup拦截事件.
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if ((ev.getY() - mLastY) < 0) {
getParent().requestDisallowInterceptTouchEvent(!isBotton);
} else if ((ev.getY() - mLastY) > 0) {
getParent().requestDisallowInterceptTouchEvent(!isTop);
}
break;
case MotionEvent.ACTION_UP:
//不允许上层viewGroup拦截事件
getParent().requestDisallowInterceptTouchEvent(true);
break;
}
mLastY = ev.getY();
return super.dispatchTouchEvent(ev);
}
}