在Android中,您如何检测用户何时已滚动到listView的顶部并在屏幕上向下拖动,即使他已到达ListView
的顶部,然后将此触摸通过ListView
进入父ViewGroup
?
答案 0 :(得分:2)
以下代码段将帮助您检测顶部过度滚动以及低于ListView
的过度滚动,
listView.setOnTouchListener(new OnTouchListener() {
private static final float OVERSCROLL_THRESHOLD_IN_PIXELS = 100;
private float downY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int firstVisibleItem = listView.getFirstVisiblePosition();
int totalItemCount = listView.getCount();
int visibleItemCount = listView.getChildCount();
boolean onTop = firstVisibleItem == 0 && listView.getChildAt(0) != null && listView.getChildAt(0).getTop() == 0;
boolean onBottom = firstVisibleItem + visibleItemCount == totalItemCount && listView.getChildAt(visibleItemCount-1).getBottom() == listView.getHeight();
if(onTop || onBottom) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float deltaY = event.getY() - downY;
if(onTop && deltaY > OVERSCROLL_THRESHOLD_IN_PIXELS)
{
// Top overscroll
}
if(onBottom && -deltaY > OVERSCROLL_THRESHOLD_IN_PIXELS) {
// Bottom overscroll
}
break;
}
}
return false;
}
});
答案 1 :(得分:0)
重写方法onOverScrolled,以响应过度滚动操作的结果。在这种情况下,当用户到达顶部并滚动列表时,您需要事件。