Android listview 懒加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10696530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android listview lazy loading
提问by Jai
I want to do some stuff. I wanna do lazy loading in ListView
. My ListView
contain more than 10,000 data & only in TextView
. so i can't load all that data in first time when i launch list activity. it's not efficient so i can load first 20 or 30 items in list. further the rows of the ListView
are loaded when I scroll over them. so when i reach at last index of ListView
on last index, i will put progressbar n it will note that the new data are loaded, so at that time new data will be load with last + 1 index. How can i do this?
我想做点事。我想在ListView
. 我的ListView
包含 10,000 多个数据且仅在TextView
. 所以当我启动列表活动时,我无法第一次加载所有数据。它效率不高,所以我可以加载列表中的前 20 或 30 个项目。ListView
当我滚动它们时,会进一步加载它们的行。因此,当我到达最后一个索引的ListView
最后一个索引时,我将放置进度条 n 它会注意到新数据已加载,因此届时将使用 last + 1 索引加载新数据。我怎样才能做到这一点?
回答by Andro Selva
You can achieve this by using endless Adapter implementation. This exactly does what you want. You can also restrict the number of rows to be refreshed per scroll. Here is a link to it.,
您可以通过使用无尽的适配器实现来实现这一点。这正是你想要的。您还可以限制每次滚动要刷新的行数。这是它的链接。,
Android: Implementing progressbar and "loading..." for Endless List like Android Market
Android:为 Android Market 等 Endless List 实现进度条和“加载...”
https://github.com/commonsguy/cwac-endless
https://github.com/commonsguy/cwac-endless
To use it, you extend EndlessAdapter to provide details about how to handle the endlessness. Specifically, you need to be able to provide a row View, independent from any of the rows in your actual adapter, that will serve as a placeholder while you, in another method, load in the actual data to your main adapter. Then, with a little help from you, it seamlessly transitions in the new data.
要使用它,您需要扩展 EndlessAdapter 以提供有关如何处理无限的详细信息。具体来说,您需要能够提供一个独立于实际适配器中任何行的行视图,当您以另一种方法将实际数据加载到主适配器时,它将用作占位符。然后,在您的帮助下,它会在新数据中无缝转换。
回答by Arshid KV
Make your lists scroll faster:-
使您的列表滚动得更快:-
- Reduce the number of conditions used in the
getView
of your adapter. - Reduce the number of garbage collection warnings that you get in the logs
- Add scroll function (restrict the number of rows to be refreshed per scroll)
- 减少
getView
适配器中使用的条件数。 - 减少您在日志中收到的垃圾收集警告的数量
- 添加滚动功能(限制每次滚动需要刷新的行数)
回答by Neeraj
Add an onScrollListenerto the ListView. When the user scrolls, check if the ListView is nearing its end. If yes, then fetch more data. As an example :
将onScrollListener添加到 ListView。当用户滚动时,检查 ListView 是否接近尾声。如果是,则获取更多数据。举个例子 :
public abstract class LazyLoader implements AbsListView.OnScrollListener {
private static final int DEFAULT_THRESHOLD = 10 ;
private boolean loading = true ;
private int previousTotal = 0 ;
private int threshold = DEFAULT_THRESHOLD ;
public LazyLoader() {}
public LazyLoader(int threshold) {
this.threshold = threshold;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(loading) {
if(totalItemCount > previousTotal) {
// the loading has finished
loading = false ;
previousTotal = totalItemCount ;
}
}
// check if the List needs more data
if(!loading && ((firstVisibleItem + visibleItemCount ) >= (totalItemCount - threshold))) {
loading = true ;
// List needs more data. Go fetch !!
loadMore(view, firstVisibleItem,
visibleItemCount, totalItemCount);
}
}
// Called when the user is nearing the end of the ListView
// and the ListView is ready to add more items.
public abstract void loadMore(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount);
}
Activity :
活动 :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setOnScrollListener(new LazyLoader() {
@Override
public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// Fetch your data here !!!
}
});
}
}
You can find the complete implementation at this link
您可以在此链接中找到完整的实现
回答by sagar.shirsath
//put in calling function :
//放入调用函数:
@Override
public View onCreateView(android.view.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ScrollListener scrollListener = new ScrollListener();
listView.setOnScrollListener(scrollListener);
}
and inner class:
和内部类:
class ScrollListener implements OnScrollListener
{
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
int size = searchedPeople.size();
if(isScrolled && totalItemCount != 0 && size < totalPeoples)
{
if(firstVisibleItem + visibleItemCount >= totalItemCount)
{
yourfunction(size, size + limit); // call service in your functioin
isScrolled = false;
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
}
}