RecyclerView 上拉加载下拉刷新
Posted SiberiaDante
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView 上拉加载下拉刷新相关的知识,希望对你有一定的参考价值。
RecyclerView 上拉加载下拉刷新
<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(); } }));
以上是关于RecyclerView 上拉加载下拉刷新的主要内容,如果未能解决你的问题,请参考以下文章
Android实战----RecyclerView下拉刷新和上拉加载的简单实现
Android实战----RecyclerView下拉刷新和上拉加载的简单实现
Android实现RecyclerView的下拉刷新和上拉加载更多