我想在调用sendScroll()方法时滚动页面。滚动工作正常,我想在屏幕上触摸时停止滚动。在下面的代码中,on touchevent无效。
private void sendScroll(){
final Handler timerHandler = new Handler();
final Runnable timerRunnable = new Runnable() {
@Override
public void run() {
scrollView.smoothScrollBy(0,5); // 5 is how many pixels you want it to scroll vertically by
timerHandler.postDelayed(this, 10); // 10 is how many milliseconds you want this thread to run
}
public boolean onTouchEvent(MotionEvent event) {
// MotionEvent object holds X-Y values
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//String text = "You click at x = " + event.getX() + " and y = " + event.getY();
//Toast.makeText(this, text, Toast.LENGTH_LONG).show();
timerHandler.removeCallbacks(timerRunnable); //stop
}
return super.onTouchEvent(event);
}
};
timerHandler.postDelayed(timerRunnable, 0);//start
}
它显示了timerRunnable和onTouchEvent(event)的错误。