问题描述
我正在创建一个ListView,用于从服务器中获取新闻.而且我正在明智地获取数据页面,即一次我得到一页包含10个项目.在向下滚动时,我再次点击该Web服务并显示下一页数据,但是问题是,假设我在服务器上总共有22个项目,并且第一次尝试时我得到22到13,然后是12到3,直到最后一次2个数据Web服务一次又一次命中.我不知道为什么只有2个数据时Web服务会一次又一次命中.而且我不怎么加载上一页.
I am creating a ListView in which I am fetching news from server. And I am getting data page wise i.e. at one time I got one page with 10 items. On down scroll I am hitting that web service again and get next page data to be shown but problem is that suppose I have total 22 items on server and in first attempt I got 22 to 13 then 12 to 3 after that when it comes to last 2 data web service hits again and again. I don't know why web service hit again and again when there is only 2 data. And I don't how to load previous page.
相同的代码
listNews.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.e(TAG, "onScroll: "+firstVisibleItem +" "+visibleItemCount+" "+totalItemCount );
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
callNewsApi((Integer.parseInt(currentPage)+1)+"");
}
}
});
推荐答案
您需要在每次命中响应后验证最后一页.在服务器站点上执行此操作的更好方法.但是您也可以在客户端对其进行管理.下面是一个示例.
You need to validate the last page after each hit response. Better way to do it on server site . But you can also manage it at client end . below is an example .
private boolean isLastPage;
private int PER_PAGE_COUNT=10;
//@params size- size of data which you get form server each time hit i shoule <= PER_PAGE_COUNT
public void validateLastpage(int size) {
if(size==0){
isLastPage=true;
}else {
if (size % PER_PAGE_COUNT == 0) {
isLastPage = false;
} else {
isLastPage = true;
}
}
}
现在在每个匹配项上检查 isLastPage .服务器站点上没有更多数据后,这将停止您调用的额外网络.每次回复后使用新数据大小调用 validateLastpage(int size).在每个分页上检查最后一页.
Now check for isLastPage on each hit. This will stop your extra network called after no more data at server site.Call validateLastpage(int size) after each response with new data size .On each pagination check for last page .
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.e(TAG, "onScroll: "+firstVisibleItem +" "+visibleItemCount+" "+totalItemCount );
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
if(!isLastPage)
callNewsApi((Integer.parseInt(currentPage) + 1) + "");
}
}
这篇关于在Android中的ListView Scroll上加载下一页和上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!