android listview
一:动态加载listview:
android listview 是很常用的组件。如果列表项过于多或者需要从网络上读取内容的话大多app都将其做成动态加载的。下拉刷新或者上拉刷新。
如图:
:
原来listview本身提供添加删除footerview和headerview的方法,而且可以添加多个:
|
Add a fixed view to appear at the bottom of the list.
|
|
Add a fixed view to appear at the bottom of the list.
|
|
Add a fixed view to appear at the top of the list.
|
|
Add a fixed view to appear at the top of the list.
|
删除时调用:
|
Removes a previously-added footer view.
|
|
Removes a previously-added header view.
|
幸好api很全名,listview提供了添加滑动监听器的方法:
|
Set the listener that will receive notifications every time the list scrolls.
|
剩下的就是定义onscrolllistener了
原来其只提供两个方法:
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Callback method to be invoked when the list or grid has been scrolled.
|
||||||||||
|
Callback method to be invoked while the list view or grid view is being scrolled.
|
第一个参数是监听的view本身,第二个是当前页上所显示的第一个列表项的索引,第三个是本页面上可以显示的item的数量,最后一个参数是指所有列表项的最大值。
由此就可以判断出当前的位置:如果firstVisibleItem + visibleItemCount >= totalItemCount答案是true既意味着用户已经滑动到了最低端,需要设置footerview了,否者就无视用户滑动。
当然在我们判断出应该显示footerview时不能仅仅是显示出来,还要加载接下来的列表项资源,如果是异步下载的话还可以快点,如果是同步的,为了避免加载过长还是建议一次少加载些列表项。
另外
如果是要自己封装好listview的话,方便今后一直使用的话,可以给listview传递一个接口,或者传递给本身一个handler,方便可以通知主程序开始加载。
另外,其实对于onscrollstatechanged()方法也有很大的用途,比如当用户不滑动列表时想要显示一个提示,而滑动列表项时则令其清空。这个方法的第二个参数则是该状态的int值。
参数
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 . |
二:listview滑动时卡的问题讨论和解决
public void setOverscrollFooter (Drawable footer)
Sets the drawable that will be drawn below all other list content. This area can become visible when the user overscrolls the list, or when the list‘s content does not fully fill the container area.
Parameters
footer | The drawable to use |
---|
public void setOverscrollHeader (Drawable header)
Sets the drawable that will be drawn above all other list content. This area can become visible when the user overscrolls the list.
Parameters
header | The drawable to use |
---|
|
Called by
overScrollBy(int,
int, int, int, int, int, int, int, boolean) to respond to the results of an over-scroll operation. |
protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)
Scroll the view with standard behavior for scrolling beyond the normal content boundaries. Views that call this method should override onOverScrolled(int,
int, boolean, boolean)
to respond to the results of an over-scroll operation. Views can use this method to handle any touch or fling-based scrolling.
Parameters
deltaX | Change in X in pixels |
---|---|
deltaY | Change in Y in pixels |
scrollX | Current X scroll value in pixels before applying deltaX |
scrollY | Current Y scroll value in pixels before applying deltaY |
scrollRangeX | Maximum content scroll range along the X axis |
scrollRangeY | Maximum content scroll range along the Y axis |
maxOverScrollX | Number of pixels to overscroll by in either direction along the X axis. |
maxOverScrollY | Number of pixels to overscroll by in either direction along the Y axis. |
isTouchEvent | true if this scroll operation is the result of a touch event. |
Returns
- true if scrolling was clamped to an over-scroll boundary along either axis, false otherwise.
public void scrollBy (int x, int y)
Move the scrolled position of your view. This will cause a call to onScrollChanged(int,
int, int, int)
and the view will be invalidated.
Parameters
x | the amount of pixels to scroll by horizontally |
---|---|
y | the amount of pixels to scroll by vertically |
public void scrollTo (int x, int y)
Set the scrolled position of your view. This will cause a call to onScrollChanged(int,
int, int, int)
and the view will be invalidated.
Parameters
x | the x position to scroll to |
---|---|
y | the y position to scroll to |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。