在顶部添加新项目时,在ListView中保持滚动位置 [英] Keep scroll position in ListView when adding new item on top

查看:74
本文介绍了在顶部添加新项目时,在ListView中保持滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ListView.builder进行聊天.

情况如下:用户键入新的聊天消息,发生这种情况时,我将新消息存储在状态中,因此itemCount增加了一个.

The scenario is the following: A user types in a new chat message, when that happens i am storing the new message in state and therefore the itemCount is increased by one.

如果我在列表的末尾,我会直接在顶部看到新项目,一切都很好.

If i am at the end of the list, I see the new item directly at the top, everything is OK.

但是,如果我在聊天过程中并且添加了一个项目,则视图会滚动一点.如何禁用此行为?我想这有点有意义,因为在ScrollControllers状态下滚动偏移量仍然是double,但是如果还有一个项目,看起来就好像在滚动...

However, if I am in the middle of the chat and an item is added, the view scrolls a little bit. How can I disable this behaviour? I guess it kinda makes sense because the scroll offset is still the same double in the ScrollControllers state, but if there is one more item, it looks like it scrolls...

有没有很好的方法可以做到这一点?我想手动方法是测量新项目的高度,然后再次手动设置正确的ScrollController偏移量.

Is there a good way to do this? I guess the manual approach would be to measure the height of the new item and manually set the correct ScrollController offset again.. but meeeh

推荐答案

实际上,您不应将新的聊天项目添加到列表中.并且您必须缓存它们,然后在列表到达顶部或底部之后将缓存注入列表中.我希望这种解决方案对您有用.

In fact, you should not add new chat items to the list. And you have to cache them and then inject the cache into the list after the list has reached the top or bottom. I hope this solution works for you.

这篇关于在顶部添加新项目时,在ListView中保持滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆