//倒数第几个开始加载 private int mCountDown = 1; //上一次加载点 所在recyclerview 中的位置 private int mExLoadPos = -1; private OnLoadMore mOnLoadMore; private LinearLayoutManager mLinearLayoutManager; public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager, OnLoadMore onLoadMore) { this.mLinearLayoutManager = linearLayoutManager; mOnLoadMore = onLoadMore; } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int loadPos = recyclerView.getAdapter().getItemCount() - mCountDown; //最后一个可见视图在 recyclerview 中的位置 int lvvPos = mLinearLayoutManager.findLastVisibleItemPosition(); // System.out.println("last:" + mExLoadPos + " loadpos:" + loadPos + " lvvPos:" + lvvPos + " dy:" + dy); //当指定 加载点视图 小于等于最后可见视图,且 向下滑动; 加载机会 if (dy > 0 && loadPos <= lvvPos) { doLoadMore(loadPos); } } private synchronized void doLoadMore(int loadPos) { if (mExLoadPos != loadPos) { //如果上一次加载点 和本次加载点不同,则可以加载,含括 lastloadpos > loadpos && lastloadpos < loadpos // System.out.println("start load more"); mExLoadPos = loadPos; mOnLoadMore.onLoadMore(); } } public interface OnLoadMore { void onLoadMore(); } public int getmCountDown() { return mCountDown; } public void setmCountDown(int mCountDown) { this.mCountDown = mCountDown; }
主要用于监听竖直方向滑动事件,当达到设定值 CountDown 时 触发 load more 事件,多用于加载更多内容
时间: 2024-10-05 04:26:03