使用SwipeRefreshLayout,網上資料copy了一個OnScrollListener給ListView,java
結果當第一個item長度超過一屏,明明尚未到達列表頂部,Scroll事件就被攔截,列表沒法滾動,同時啓動了刷新。ide
修正代碼後,自定義的OnScrollListener以下:spa
/** 因爲Listview與下拉刷新的Scroll事件衝突, 使用這個ScrollListener能夠避免Listview滑動異常 */ public static class SwpipeListViewOnScrollListener implements AbsListView.OnScrollListener { private SwipeRefreshLayout mSwipeView; private AbsListView.OnScrollListener mOnScrollListener; public SwpipeListViewOnScrollListener(SwipeRefreshLayout swipeView) { mSwipeView = swipeView; } public SwpipeListViewOnScrollListener(SwipeRefreshLayout swipeView, OnScrollListener onScrollListener) { mSwipeView = swipeView; mOnScrollListener = onScrollListener; } @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { View firstView = absListView.getChildAt(firstVisibleItem); // 當firstVisibleItem是第0位。若是firstView==null說明列表爲空,須要刷新;或者top==0說明已經到達列表頂部, 也須要刷新 if (firstVisibleItem == 0 && (firstView == null || firstView.getTop() == 0)) { mSwipeView.setEnabled(true); } else { mSwipeView.setEnabled(false); } if (null != mOnScrollListener) { mOnScrollListener.onScroll(absListView, firstVisibleItem, visibleItemCount, totalItemCount); } } }