gpt4 book ai didi

android - android RecyclerView中上拉刷新

转载 作者:行者123 更新时间:2023-11-29 17:19:58 25 4
gpt4 key购买 nike

我已经按照以下方式使用了 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() 方法。

enter image description here

这叫做下拉刷新。但我想要相反的效果。也就是说,如果我在最后一个列表项可见时拉起屏幕,那么应该调用一个方法或者无论如何都应该通知它。是否可以?如果可能,请提供方法。

最佳答案

在您的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com