对ListView进行优化,加载大数据量或者包含图片信息的时候滚屏加载

本文详细介绍了如何通过重写OnScrollListener的onScroll和onScrollStateChanged方法来实现滚动加载,有效解决ListView在大量数据时加载时间过长导致的卡屏问题。通过设置适配器的计数和调用notifyDataSetChanged方法,可以实现按需加载数据,提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做ListView加载数据时如果数据量大的话会造成加载时间过长而卡屏,所以为了解决这个问题,查看了SDK,

OnScrollListener中有两个方法

只要重写这两个方法就可以实现滚动加载,例如:

public void onScroll(AbsListView v, int firstVisibleItem,
   int visibleItemCount, int totalItemCount) {
  lastItem = firstVisibleItem + visibleItemCount - 1;
  if (adapter.count == lastItem) {
   adapter.count += 10;
   adapter.notifyDataSetChanged();
  }

}

public void onScrollStateChanged(AbsListView view, int scrollState) {
  // TODO Auto-generated method stub

  Log.i("onScrollStateChanged", "onScrollStateChanged");
}

public abstract void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
Since: API Level 1

Callback method to be invoked when the list or grid has been scrolled. This will be called after the scroll has completed

Parameters
view The view whose scroll state is being reported
firstVisibleItem the index of the first visible cell (ignore if visibleItemCount == 0)
visibleItemCount the number of visible cells
totalItemCount the number of items in the list adaptor

public abstract void onScrollStateChanged (AbsListView view, int scrollState)
Since: API Level 1

Callback method to be invoked while the list view or grid view is being scrolled. If the view is being scrolled, this method will be called before the next frame of the scroll is rendered. In particular, it will be called before any calls togetView(int, View, ViewGroup).

Parameters
view The view whose scroll state is being reported
scrollState The current scroll state. One of SCROLL_STATE_IDLE, SCROLL_STATE_TOUCH_SCROLL orSCROLL_STATE_IDLE.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值