gpt4 book ai didi

android - 当列表项快速滚动时,setOnScrollListener 不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:06:00 25 4
gpt4 key购买 nike

我尝试实现教程中描述的粘性标题 ListView

http://javatechig.com/android/listview-header-parallax-with-sticky-view-in-android

问题是当我快速滚动列表时,标题没有按要求移动到屏幕顶部。

我尝试在以下方法中记录 topYheroTopY 的值。

        @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

从日志输出中可以看出,topY 值停留在 84,因此标题。 enter image description here如何解决这个问题?任何解决方案都会有帮助!!!

最佳答案

添加这些行

if (view.getFirstVisiblePosition() == 0) {
.....
.....
}
else
{
stickyView.setY(0);
}

这是因为当第一个项目离开 View 时你不会进入 if 语句意味着 topY 没有更新所以你必须在 else 语句中将你的 stickyView.setY() 设置为 0。

关于android - 当列表项快速滚动时,setOnScrollListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com