2014-08-28 92 views
2

惠普這下面的行動,我可以檢查滾動列表查看是完成。現在我想檢查列表視圖滾動到完成。如何獲得這個事件?找出ListView是否滾動到上?

@Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
     if (prevVisibleItem != firstVisibleItem) { 
      if (prevVisibleItem < firstVisibleItem) 
       Log.e(getClass().toString(), "DOWN"); 
      else 
       Log.e(getClass().toString(), "UP"); 

      prevVisibleItem = firstVisibleItem; 
      /* 
      * CHECK SCROLLING BUTTOM FINISH 
      */ 

      if((firstVisibleItem + visibleItemCount) == totalItemCount) 
      { 
       Log.i("Info", "Scroll Bottom FINISH"); 
      } 

      /* 
      * CHECK SCROLLING UP FINISH 
      */ 
     } 
    } 
+0

這可能有幫助... [CLICKHERE](http://stackoverflow.com/questions/6358428/implementation-of-onscrolllistener-to-detect-the-end-of-scrolling-in-a-listview) – TeachMeJava 2014-08-28 08:00:40

+0

@TeachMeJava多數民衆贊成這是鏈接'在ListView滾動結束' – 2014-08-28 08:14:49

回答

3
if(firstVisibleItem == 0 && !isFirstTime){ 
//it is top of the list 
} 

isFirstTime類中定義一個變量,它初始化爲true。當用戶滾動列表 檢查

if (firstVisibleItem != 0) 
{ 
    isFirstTime =false; 
} 

,這意味着用戶滾動列表的第一個元素是如此,如果再次

firstVisibleItem == 0 

意味着列表滾動起來,共完成消失。

+3

請嘗試至少包括一個簡短的評論,你已經做了什麼,爲什麼:)這可能會幫助他理解你的原因 – 2014-08-28 08:57:53

+0

你能簡單地解釋你的答案嗎?這樣的小答案可能在將來被刪除 – 2014-08-28 09:02:26

+0

@Tasos K.可以嗎? – mmlooloo 2014-08-28 09:06:34

0

使用 OnSwipeTouchListener.java:

import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 

     super.onTouch(view, motionEvent); 
     return gestureDetector.onTouchEvent(motionEvent); 

    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 
    public void onSwipeRight() {} 
    public void onSwipeLeft() {} 
    public void onSwipeTop() {} 
    public void onSwipeBottom() {} 
} 

用法:

imageView.setOnTouchListener(new OnSwipeTouchListener() { 

    public void onSwipeTop() { 
     Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeRight() { 
     Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeLeft() { 
     Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeBottom() { 
     Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
    } 

}); 
+0

無法解析'onTouch()' – 2014-08-28 08:13:34

0

這可能是helpful.I搜查計算器你的問題,我覺得這個答案正是你的問題的回答。滾動完成檢測。 Click Here for Full Code Answer

private void isScrollCompleted() { 
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) { 
     /*** In this way I detect if there's been a scroll which has completed ***/ 
     /*** do the work! ***/ 
    } 
} 
+0

@TechMeJava。如何解決'currentVisibleItemCount'和'currentScrollState'?我的班級擴展'ListFragment' – 2014-08-28 08:10:44