我已经按照以下方式使用了 v4 支持库的 SwipeRefreshLayout:
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshItems();
}
});
void refreshItems() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
}, 3000);
}
在此策略中,如果我在第一个列表项可见时下拉屏幕,则会调用 onRefresh() 方法。
这叫做下拉刷新。但我想要相反的效果。也就是说,如果我在最后一个列表项可见时拉起屏幕,那么应该调用一个方法或者无论如何都应该通知它。是否可以?如果可能,请提供方法。
最佳答案
在您的 RecyclerView/ListView 的适配器类中,在膨胀最后一个项目时,您可以放置 if 语句并调用一个方法。
代码,如果你正在使用 RecyclerView:-
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
}
}
}
}
});
同时添加以下代码:-
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
关于android - android RecyclerView中上拉刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005417/