@[Android|ListView|OnScrollListener]java
在寫listview的時候很時候咱們都會遇到一些場景android
可是Android提供給咱們的OnScrollView不能給咱們提供這些功能
因而我寫了一個OnScrollListenerPro來完成這樣的事情ide
package org.hangox.test; import android.view.View; import android.widget.AbsListView; /** * Created With Android Studio * User 47 * Date 2014-11-03 * Time 17:42 * 一個增強板的OnScrollListener * 能夠提供向上滑動向下活動檢測 * 是不是最後一個檢測 */ public class OnScrollListenerPro implements AbsListView.OnScrollListener { private AbsListView mAbsListView; private int mLimit = 40; private int mLastScroll; @Override public void onScrollStateChanged(AbsListView view, int scrollState) { mAbsListView = view; } /** * 設定最少滾動多少纔算是滾動 * @param mLimit */ public void setLimit(int mLimit){ this.mLimit = mLimit; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mAbsListView = view; int currentScrollY = getScrollY(); int dValue = currentScrollY - mLastScroll; if(dValue < -mLimit){//向上滾 onScrollUp(mLastScroll,currentScrollY); mLastScroll = currentScrollY; }else if(dValue > mLimit ){//向下滾 onScrollDown(mLastScroll,currentScrollY); mLastScroll = currentScrollY; } if(((totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1))) onLastItemShow(); } /** * 向上滾動 * @param lastScroll * @param currentScroll */ public void onScrollUp(int lastScroll,int currentScroll){ } /** * 向下滾動 * @param lastScroll 上一次滾動值 * @param currentScroll 這一次 */ public void onScrollDown(int lastScroll,int currentScroll){ } public void onLastItemShow(){ } public int getScrollY() {//獲取滾動距離 View c = mAbsListView.getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = mAbsListView.getFirstVisiblePosition(); int top = c.getTop(); int headerHeight = 0; if (firstVisiblePosition >= 1) { headerHeight = mAbsListView.getHeight(); } return -top + firstVisiblePosition * c.getHeight() + headerHeight; } }
使用很簡單,重寫其中的函數就能夠了函數