如何实现无休止的列表RecyclerView? [英] How to implement endless list with RecyclerView?
问题描述
我喜欢的ListView更改为<一个href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html">RecyclerView.我怀念的的onScroll <一href="http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html#onScroll">OnScrollListener在RecyclerView确定该用户滚动到列表的末尾。我如何知道用户滚动到列表的末尾,这样我可以从一个REST服务获取新的数据。
I like to change from ListView to RecyclerView. I miss the onScroll of the OnScrollListener at RecyclerView to determine that the user scrolled to the end of the list. How do I know that the user scrolls to the end of the list so that I can fetch new data from a Rest Service.
推荐答案
由于@Kushal,这是我是如何实现它
Thanks to @Kushal and this is how I implemented it
private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if(dy > 0) //check for scroll down
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading)
{
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
loading = false;
Log.v("...", "Last Item Wow !");
//Do pagination.. i.e. fetch new data
}
}
}
}
});
不要忘了添加
Don't forget to add
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
这篇关于如何实现无休止的列表RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!