SwipeRefreshLayout下拉刷新冲突解决

jopen 10年前

使用SwipeRefreshLayout,网上资料copy了一个OnScrollListener给ListView,

结果当第一个item长度超过一屏,明明还没有到达列表顶部,Scroll事件就被拦截,列表无法滚动,同时启动了刷新。

修正代码后,自定义的OnScrollListener如下:

/** 由于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);          }      }  }