在列表视图中仅加载10个项目 [英] Loading only 10 items in listview
问题描述
该列表视图使用线程池从Internet加载了100张图片.如何仅将元素10加载到必要的程度?向下滚动时再加载10个项目?我正在为此使用Lru缓存
The listview is loaded 100 pictures from the internet using a thread pool. How load only elements 10 to the extent necessary? And when scrolldown loading another 10 items? I'm using Lru cache for this
推荐答案
您可以为此使用limit
和offset
:
示例:
假设我要获取10条记录,结束后滚动将获取另外10条记录:
suppose i want to fetch 10 records and after end the scroll will fetch another 10 records:
定义偏移量变量
private static int offset = 0;
在绑定适配器时传递了offset变量:
when you bind the adapter at that pass the offset variable:
Select * from table limit 10 offset 0
因此,在为listview编写以下代码之后,这将仅从0-9获取10条记录:
So this will fetch only 10 records from 0-9 after that write below code for listview:
listview.setOnScrollListener(new OnScrollListener() {
private int mLastFirstVisibleItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
mLastFirstVisibleItem = firstVisibleItem;
final int lastItem = firstVisibleItem + visibleItemCount;
if (lastItem == totalItemCount) {
if (preLast != lastItem) {
preLast = lastItem;
offset = offset + 10;
new fetchrecords().execute();
} else {
}
}
}
});
因此,当您当时转到列表视图项的最后一个位置时,它将调用方法fetchrecords()
和您的offset value is 10
.
so when go you to last position of list view item at that time it will call the method fetchrecords()
and your offset value is 10
.
因此,当该方法在那时调用时,偏移值是10.
So when this method calls at that time your offset value is 10.
因此下一次您的查询如下:
so for next time your query is like:
select * from table limit 10 offset 10
因此它将获取其他接下来的10条记录.
So it will fetch other next 10 records.
完成了!
这篇关于在列表视图中仅加载10个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!