android listview

转载文章请注明出处:http://blog.csdn.net/dangxw_/article/details/18318167

一:动态加载listview:

android listview 是很常用的组件。如果列表项过于多或者需要从网络上读取内容的话大多app都将其做成动态加载的。下拉刷新或者上拉刷新。

如图:

原来listview本身提供添加删除footerview和headerview的方法,而且可以添加多个:

void addFooterView( View v)
Add a fixed view to appear at the bottom of the list.
void addFooterView( View v,  Object data, boolean isSelectable)
Add a fixed view to appear at the bottom of the list.
void addHeaderView( View v,  Object data, boolean isSelectable)
Add a fixed view to appear at the top of the list.
void addHeaderView( View v)
Add a fixed view to appear at the top of the list.
注:添加这些东西必须在setadapter之前完成。当然事后可以更改。

删除时调用:

boolean removeFooterView( View v)
Removes a previously-added footer view.
boolean removeHeaderView( View v)
Removes a previously-added header view.
而我们并不是想要让这些提示刷新的列表项一直都在,需要根据用户的手势和当前列表项所显示的位置来判断是否隐藏和切换。

幸好api很全名,listview提供了添加滑动监听器的方法:

void setOnScrollListener( AbsListView.OnScrollListener l)
Set the listener that will receive notifications every time the list scrolls.
其实更准确的说是在他的父类:abslistview中就已经给出此方法。
剩下的就是定义onscrolllistener了

原来其只提供两个方法:

Public Methods
abstract void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
Callback method to be invoked when the list or grid has been scrolled.
abstract void onScrollStateChanged( AbsListView view, int scrollState)
Callback method to be invoked while the list view or grid view is being scrolled.
onscroll就是我们要判断是否要显示footerview或headerview用到的,

第一个参数是监听的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滑动时卡的问题讨论和解决

这些动态的组件都是由一个adapter来填充内容的,如果你在没个列表项的返回中加上一条log之后会发现,其实它是完全动态的。也就是说为了节省内存空间, 系统在判读当前状态下显示了多少个列表项,需要显示哪些列表项,与此同时加载所需要的项并且释放无用的项目。每次的滑动(如果滑动到了列表项之间的边界的话)都会在加载些内容,如果列表项的内容过多,或者要实时读取网络中的内容的话就会出现卡的现象。
如果想要解决这个问题需要手动存储已经加载过的内容,再次用到时不用再次加载。
存储的方法可以是多种存入缓存或者存入本地,另外还可以减慢listview对用户划屏速度的响应,从而避免卡顿现象的发生。
三:listview之overscroll

public void setOverscrollFooter (Drawable footer)
Added in  API level 9

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)
Added in  API level 9

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
这两个方法都接受一个图片作为参数,这张图片就类似于:




这张图片会位于所有列表项之外。方法中给出了解释:

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.


但是很可悲,系统默认是不允许listview划过界的,即便设置了这张图片并且setOverScrollMode(ListView.OVER_SCROLL_ALWAYS);,也仅仅能显示几个像素而已。
但是仔细想想其实实现它的思路不止这一个,也许可以给listview加上触摸事件,然后将当前位置设置成全局的变量,当判断其到达顶端并且还是有move事件就向下移动listview本身,并且将原本想要添加的图片设置成背景。
我并没有尝试这个方法,因为即使的发现他的父类中提供给了这个方法:
void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
Called by  overScrollBy(int, int, int, int, int, int, int, int, boolean) to respond to the results of an over-scroll operation.
这下更夸张了,因为overscrollby是view中的方法,直接跳到这个类了,这个方法详解:
protected boolean overScrollBy (int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)
Added in  API level 9

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.
于是干脆直接override了这个方法得了,更改之后的确生效了。但是会发现这个效果很丑,因为向下拉动时与平常拉动并无太大差别,而且有时候就会停止反弹,查阅资料后发现原来这个方法本身就存在bug. 后来又不甘心的在这两个方法上各种做手段,但是尝试了很多种方法,仍是不管用。但是调试发现,原来问题出在view的overscrollby 上。
   后来又看到view 本身提供两个滚动方法:
public void scrollBy (int x, int y)
Added in  API level 1

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)
Added in  API level 1

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
一个是移动多少距离,一个是移动到某个位置。
于是果断决定重写一下这个方法,由于我们软件本身并没有要求实现这个功能,所以demo的代码需要很多美化,不过已经消除了这个bug了。大体的思路是给listview本身添加上ontouchlistener然后监听事件本身的变化,并重写它的onOverscrolled()。
注:不要再调用super.onOverscrolled()了,不然又会有bug
虽然杜绝了回弹的bug,但是本身还是很丑,因为回弹速度太快,这个问题可以很好解决,也就是将直接的scrollTo(0,0)分步骤执行。
除此之外应该加入阻尼效果。
四 listview阻尼效果
其实对于阻尼效果相对来讲比较容易实现,只需要override了onOverscrolled方法,并且在他的scrollY上做出自己想要比例的缩小,然后super返回值即可。

五listview点击item中的按钮无响应。

点击按钮无响应可能是两种原因,一是焦点的抢夺。http://blog.csdn.net/zhufuing/article/details/8677407
二是listview本身的bug,需要在布局中将其设置成match_parent. http://bbs.csdn.net/topics/380101404
写困了都。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值