CocoSnake

 

Android ListView 滚动加载

编辑器加载中... 主要是要掌握滚动加载的时机,在当前List剩下多少的时候就开始更新数据。

要为List设置OnScrollListener, 利用onScroll 控制刷新时机

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

利用firstVisibleItem和visibleItemCount可以判断当前滚动的位置。

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 to getView(int, View, ViewGroup). Parameters view The view whose scroll state is being reported scrollState The current scroll state. One of SCROLL_STATE_TOUCH_SCROLL or SCROLL_STATE_IDLE.

当滑到最后一条记录时 前天提到的firstVisibleItem和visibleItemCount的总和 firstVisibleItem+visibleItemCount等于 adapter.getCount(), 此时可以利用Handler在线程中更新数据,最重要的是在更新数据后调用Adapter的notifyDataSetChanged()方法通知ListView去更新。

P.S. 如果后台数据更新较慢,建议使用addFooterView和removeFooterView,

 

利用多线程更新List. Tips & Tricks (from Chon http://www.cnblogs.com/chon/archive/2011/06/28/2092317.html) ListView是为了大容量数据展示而设计的。如果数据量(Item的数量)不是很大,且用ListView实现起来比较麻烦,不妨换种思路,不使用ListView,而用ScrollView来实现。 如果Item信息布局比较复杂或者Item的数量很多,出于性能的考虑,建议自定义一个View组件实现需要的功能,而不是组合其它控件达到所要的效果。 ListView滚动变黑:在xml中给ListView增加一个属性android:cacheColorHint="#00000000" 。当ListVIew中有很多Item,有时候需要快速的滚动。比如从第一个Item滚动到第600个Item这个时候,中间的很多Item对用户来说意义不是很大,但android却要调用 adapter.getView()方法将这些Item逐一画出,并且因为滚动很快用户不希望有任何的延迟。这在一些低端手机比如g1,是很难作到的。所以google工程师想出的一个办法是在滚动的时候,让屏幕变黑用一张黑色bitmap盖住ListView,而不去绘制中间过程中的很多Item,从而提升性能。 Item有自己的背景盖住了Selector光标:在xml中给ListView增加一个属性:android:drawSelectorOnTop="true"这样光标就会跑到Item上面的图层,解决我们的问题。

posted on 2012-03-16 22:01  CocoSnake  阅读(1051)  评论(0编辑  收藏  举报

导航