ListView反弹效果实现

zq1447 2012-06-20 02:09:19
转自:http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=43470&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D67

基于ScrollView反弹效果的一些原理(可以看我另外一篇帖子),看了下ListView滚动的一些代码.发现这两者实现方式有些不太一样.
ListView根本没有继承使用View的ScrollBy和ScrollTo,而且它的移动到最上和最下的判断也打不一样,代码如下,截取自AbstractListView的trackMotionScroll(int,int)方法:
final int end = getHeight() - listPadding.bottom;
final int spaceBelow = lastBottom - end;
final int height = getHeight() - mPaddingBottom - mPaddingTop;
if (deltaY < 0) {
deltaY = Math.max(-(height - 1), deltaY);
} else {
deltaY = Math.min(height - 1, deltaY);
}
if (incrementalDeltaY < 0) {
incrementalDeltaY = Math.max(-(height - 1), incrementalDeltaY);
} else {
incrementalDeltaY = Math.min(height - 1, incrementalDeltaY);
}
final int firstPosition = mFirstPosition;
if (firstPosition == 0 && firstTop >= listPadding.top && deltaY >= 0) {
// Don't need to move views down if the top of the first position
// is already visible
return true;
}
if (firstPosition + childCount == mItemCount && lastBottom <= end && deltaY <= 0) {
// Don't need to move views up if the bottom of the last position
// is already visible
return true;
}
这个mFirstPosition是显示的第一个ListView的Item,firstPosition + childCount == mItemCount这句话的意思是可以显示最后的childCount条(这是个规律,自己可以算算),在加上lastBottom(也就是最后一个显示item)比end(也就是ListView的最底部距离最上部的距离)小,一般情况下都是比它大,firstTop >= listPadding.top 和这个相反,也就是第一个item比paddingTop要大,如果ListView不设这个属性的话那就是0了.这里要说下不管是end、firstTop或者是lastBottom意思都是距离它的最上面的组件或者是parent的距离,不是屏幕.详细的大家可以去看trackMotionScroll.
我的实现是在基本不破坏原有功能的基础上加入了一些可以继续移动的代码,也就是使用View的scrollBy.
此为抛砖引玉,反弹效果刷新实现有点生硬,大家可以优化它,让它更流畅。
这种效果也应该有人写过,这是我自己的实现。提供附件下载,转载请注明出处。
...全文
92 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

790

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 其他移动相关
社区管理员
  • 移动开发其他问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧