原文地址:https://blog.csdn.net/static_void_james/article/details/74353295#commentsedit
学习Android群英传看到书中的smoothScrollToPosition时,很多博客说这个方法移动listview不准确,却没有说明具体原因,最终找到了原博主这篇博客,感谢原博主。
首先,学一个英语短语:
such that 使得满足…的条件
两者的API文档分别如下:
AbsListView.smoothScrollToPositionFromTop:
Smoothly scroll to the specified adapter position.The view will scroll such that the indicated position is displayed offset pixels below the top edge of the view.
流畅地滚动到指定位置。这个listview将滚动,直到指定position位置的view显示在离顶端offset个像素的位置。
ListView.smoothScrollToPosition
Smoothly scroll to the specified adapter position. The view will scroll such that the indicated position is displayed.
流畅地滚动到指定位置。这个listview将滚动,直到指定position位置的view显示出来。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
smoothScrollToPosition:只是滑到指定位置的view在屏幕上显示出来,只要显示出来,就不再滑动,不管显示在屏幕最上方还是最下方。有时候会失效,就是因为已经指定位置的view已经显示在屏幕上了,如果想显示在指定的位置,需要使用smoothScrollToPositionFromTop,使指定位置的view显示在离父布局顶端N个像素的位置。或者指定smoothScrollToPosition,参数列表中的position一个偏移量,使未显示的view显示出来,将需要摆放位置的view向特定方向挤压过去,以达到指定view的位置的目的。
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
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-778f64ae39.css" rel="stylesheet">
</div>