<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/teach_swipe" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout>
public class RecyclerOnScrollListener extends RecyclerView.OnScrollListener { private LinearLayoutManager layoutManager; private LoadMoreData loadMoreData; public RecyclerOnScrollListener(LinearLayoutManager layoutManager, LoadMoreData loadMoreData) { this.layoutManager = layoutManager; this.loadMoreData = loadMoreData; } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int lastVisibleItem = layoutManager.findLastVisibleItemPosition(); int totalItemCount = layoutManager.getItemCount(); //dy>0表示向上滑动 //lastVisibleItem >= totalItemCount - 2表示剩下两个 if (lastVisibleItem >= totalItemCount - 2 && dy > 0) { loadMoreData.loadMore(); } } public interface LoadMoreData { void loadMore(); } }
核心代码片段:------------------
//下拉刷新 mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.teach_swipe); mSwipeRefresh.setSize(1); mSwipeRefresh.setColorSchemeColors(Color.RED, Color.GREEN); mSwipeRefresh.setProgressBackgroundColorSchemeResource(R.color.colorAccent); mSwipeRefresh.setOnRefreshListener(this); @Override public void onRefresh() { indexPage = 1; getData(); mHandler.sendEmptyMessageDelayed(UPDATE, DELAY_TIME); } //上拉加载 mRecyclerView.addOnScrollListener(new RecyclerOnScrollListener(layoutManager, new RecyclerOnScrollListener.LoadMoreData() { @Override public void loadMore() { indexPage++; getData(); } }));
时间: 2024-11-08 21:56:05