简体   繁体   English

在Android中将项目添加到顶部时保持列表的滚动位置

[英]Keep the scroll position of a list while adding item to the top in Android

Here and in some other places I found a proposed solution for the problem of keeping the scroll position while adding items to the top of a list. 在这里和其他一些地方,我找到了一个解决方案,用于在将项目添加到列表顶部时保持滚动位置的问题。

I tested it and the result is that the list scrolls and then the original position is restored but the user can see the list jumping. 我测试了它,结果是列表滚动然后恢复原始位置,但用户可以看到列表跳转。

Another problem with this approach is that if I add the items very quickly it does not work. 这种方法的另一个问题是,如果我非常快速地添加项目,它就不起作用。 The reason is that the new cursor can have more than one new item because some previous cursors did not manage to be used. 原因是新游标可能有多个新项目,因为某些以前的游标无法使用。

Is there a better solution? 有更好的解决方案吗?

Can try this 可以试试这个

listPos = getListView().getFirstVisiblePosition();
int newPos = listPos + (customAdapter.getCount() - oldListSize);
getListView().setSelectionFromTop(newPos, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM