判断scrollView滚动结束

重写scrollView的onTouchEvent事件,在MotionEvent.ACTION_CANCEL和MotionEvent.ACTION_UP中通过反射获取scrollview中的OverScroller,然后获取OverScroller的状态判断scrollview是否结束滚动

@Override
public boolean onTouchEvent(MotionEvent ev)
{
int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
handler.sendEmptyMessage(0);
break;
}
return super.onTouchEvent(ev);
}


Handler handler = new Handler()
{


@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0:
checkIsScrolling();
break;
}
super.handleMessage(msg);
}
};


private void checkIsScrolling()
{
try
{
Field field = getClass().getSuperclass().getDeclaredField(
"mScroller");
field.setAccessible(true);
Object object = field.get(this);
OverScroller scroller = (OverScroller) object;
boolean isScrolling = scroller.computeScrollOffset();
if (isScrolling)
{
handler.sendEmptyMessageDelayed(0, 10);
} else
{
//滚动结束
}
} catch (Exception e)
{
e.printStackTrace();
}
}


下载地址http://download.csdn.net/detail/dianqiugg/9821544

你可能感兴趣的:(android)