gpt4 book ai didi

listview - Android ListView 滚动到底部时动态加载更多项目(来自 MysqlDatabase)

转载 作者:行者123 更新时间:2023-12-02 16:56:17 25 4
gpt4 key购买 nike

如何在滚动时检测列表末尾并触发将更多数据从 WEB 请求加载到 ListView

如果您正在从 WEB 服务器加载数据并且列表很大,那么实际的解决方案是加载一定量,如果用户滚动到列表末尾,则加载更多数据并继续,直到加载完毕完整列表

最佳答案

您可以将分页监听器添加到 ListView:1. 实现AbsListView.OnScrollListener,例如您可以创建

抽象类InfiniteListViewPaging

 public abstract class InfiniteListViewPaging implements AbsListView.OnScrollListener {

private static final String TAG = InfiniteListViewPaging.class.getName();
public ListView mListView;
public View mFooterView;
private boolean mCanLoadMore;
private boolean mIsLoading;

/**
* Should be created before setting the adapter on the {@code listView} as this sets the
* {@link android.widget.ListView#addFooterView(android.view.View)} on it.
* @param context
* @param listView
*/
public InfiniteListViewPaging(Context context, ListView listView) {
mFooterView = View.inflate(context, R.layout.footer_list_view, null);
mListView = listView;
listView.addFooterView(mFooterView, null, false);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
boolean lessThanOneScreenLeftToScrollDown = (totalItemCount - 2 * visibleItemCount) <= firstVisibleItem;
if (mCanLoadMore && !mIsLoading && lessThanOneScreenLeftToScrollDown) {
Log.d(TAG, "Load Next Page!");
mIsLoading = true;
loadMore();
}
}

public void setCanLoadMore(boolean canLoadMore) {
mCanLoadMore = canLoadMore;
mFooterView.setVisibility(canLoadMore ? View.VISIBLE : View.GONE);
}

public abstract void loadMore();

public void hasFinishedLoading() {
mIsLoading = false;
}
  • 然后在你的Activity(或Fragment)中,初始化ListView时设置

    mListPaging = new InfiniteListViewPaging(this, getListView()) {
    @Override
    public void loadMore() {
    loadMore();
    }
    };
    mListView.setOnScrollListener(mListPaging);
  • 关于listview - Android ListView 滚动到底部时动态加载更多项目(来自 MysqlDatabase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026236/

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