【发布时间】:2015-09-01 12:06:23
【问题描述】:
我尝试实现教程中描述的粘性标题列表视图来自
http://javatechig.com/android/listview-header-parallax-with-sticky-view-in-android
问题是当我快速滚动列表时,标题不会按需要移动到屏幕顶部。
我尝试用以下方法记录 topY 和 heroTopY 的值。
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
/* Check if the first item is already reached to top.*/
if (view.getFirstVisiblePosition() == 0) {
View firstChild = listView.getChildAt(0);
topY = 0;
if (firstChild != null) {
topY = firstChild.getTop();
Log.d("topY", "" + topY);
}
heroTopY = stickyViewSpacer.getTop();
Log.e("heroTopY", "" + heroTopY);
Log.d("topY,heroTopY", topY + "," + heroTopY);
stickyView.setY(Math.max(0, heroTopY + topY));
// Set the image to scroll half of the amount that of ListView
heroImageView.setY(topY * 0.5f);
}
}
日志输出为:
09-01 17:35:23.692 19530-19530/com.javatechig.parallaxlistview D/topY﹕ 0
09-01 17:35:23.692 19530-19530/com.javatechig.parallaxlistview E/heroTopY﹕ 500
09-01 17:35:23.692 19530-19530/com.javatechig.parallaxlistview D/topY,heroTopY﹕ 0,500
09-01 17:35:23.856 19530-19530/com.javatechig.parallaxlistview D/topY﹕ -29
09-01 17:35:23.856 19530-19530/com.javatechig.parallaxlistview E/heroTopY﹕ 500
09-01 17:35:23.856 19530-19530/com.javatechig.parallaxlistview D/topY,heroTopY﹕ -29,500
09-01 17:35:23.873 19530-19530/com.javatechig.parallaxlistview D/topY﹕ -72
09-01 17:35:23.873 19530-19530/com.javatechig.parallaxlistview E/heroTopY﹕ 500
09-01 17:35:23.873 19530-19530/com.javatechig.parallaxlistview D/topY,heroTopY﹕ -72,500
09-01 17:35:23.892 19530-19530/com.javatechig.parallaxlistview D/topY﹕ -84
09-01 17:35:23.892 19530-19530/com.javatechig.parallaxlistview E/heroTopY﹕ 500
09-01 17:35:23.892 19530-19530/com.javatechig.parallaxlistview D/topY,heroTopY﹕ -84,500
【问题讨论】:
-
在
onScroll开头添加Log.e -
使用
if (view.getFirstVisiblePosition() == 0){},只有当您到达顶部时,您才会移动您的stickyView,这是您想要的吗? -
@Rami,先生,这里是视频链接youtu.be/eyWDkwVnYxo。当列表视图的第一项到达屏幕顶部时,我想实现标题 Heading1 粘在顶部,如果滚动缓慢,它可以正常工作。但是如果快速滚动 Heading1 视图不会移到顶部!!
标签: android listview onscrolllistener