Android 判斷ListView滑動方向
代碼很簡單,給mListView監聽onScrollListener事件,然后在onScroll進行判斷
//listView中第一項的索引
private int mListViewFirstItem = 0;
//listView中第一項的在屏幕中的位置
private int mScreenY = 0;
//是否向上滾動
private boolean mIsScrollToUp = false;
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
if(mListView.getChildCount()>0)
{
boolean isScrollToUp = false;
View childAt = mListView.getChildAt(firstVisibleItem);
int[] location = new int[2];
childAt.getLocationOnScreen(location);
Log.d("onScroll", "firstVisibleItem= "+firstVisibleItem+" , y="+location[1]);
if(firstVisibleItem!=mListViewFirstItem)
{
if(firstVisibleItem>mListViewFirstItem)
{
Log.e("--->", "向上滑動");
isScrollToUp = true;
}else{
Log.e("--->", "向下滑動");
isScrollToUp = false;
}
mListViewFirstItem = firstVisibleItem;
mScreenY = location[1];
}else{
if(mScreenY>location[1])
{
Log.i("--->", "->向上滑動");
isScrollToUp = true;
}
else if(mScreenY<location[1])
{
Log.i("--->", "->向下滑動");
isScrollToUp = false;
}
mScreenY = location[1];
}
if(mIsScrollToUp!=isScrollToUp)
{
onScrollDirectionChanged(mIsScrollToUp);
}
}
}
private void onScrollDirectionChanged(boolean isScrollToUp)
{
}</pre>
本文由用戶 yn6e 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!