本文介绍了如何使用 RecyclerView 实现无限列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 ListView
更改为 RecyclerView
.我想使用 OnScrollListeneronScroll
RecyclerView
中的/code> 以确定用户是否滚动到列表的末尾.
I would like to change ListView
to RecyclerView
. I want to use the onScroll
of the OnScrollListener
in RecyclerView
to determine if a user scrolled to the end of the list.
我如何知道用户是否滚动到列表末尾以便我可以从 REST 服务获取新数据?
How do I know if a 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
loading = true;
}
}
}
}
});
别忘了添加
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
这篇关于如何使用 RecyclerView 实现无限列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!