思路:在滑動基礎上添加scrollview到底檢測事件
1.首先檢測是否滑動
// 滑動載入scrollView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN :break;case MotionEvent.ACTION_MOVE ://檢查滑動事件 Log.d(TAG,"滑到底部");break;default :break;}}});
2.對scrollview添加到底檢測監聽事件
但是scrollview 不能像listview那樣添加onscrolllistener監聽,所以需要自己檢測
if (view.getMeasuredHeight() <= v.getScrollY() + v.getHeight()) { //到底部 Log.d(TAG,"滑到底部");}
3.組合代碼
// 滑動載入scrollView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN :break;case MotionEvent.ACTION_MOVE :View view = ((ScrollView) v).getChildAt(0);if (view.getMeasuredHeight() <= v.getScrollY() + v.getHeight()) {//載入資料代碼}break;default :break;}}});
4.測試的時候 發現這樣會導致滑動多次觸發,所以最佳化代碼如下
private int index = 0;// 滑動載入scrollView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_DOWN :break;case MotionEvent.ACTION_MOVE :index++;break;default :break;}if (event.getAction() == MotionEvent.ACTION_UP && index > 0) {index = 0;View view = ((ScrollView) v).getChildAt(0);if (view.getMeasuredHeight() <= v.getScrollY() + v.getHeight()) {//載入資料代碼}}return false;}});