android中notifyDataSetChanged后的smoothScrollToPosition不起作用 [英] smoothScrollToPosition after notifyDataSetChanged not working in android

查看:28
本文介绍了android中notifyDataSetChanged后的smoothScrollToPosition不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 ListView 设置的自定义适配器并且工作正常.单击按钮时,基础数据中的某些内容发生了更改,因此需要使用 notifyDataSetChanged 来刷新 ListView.美好的.但我也希望 ListView 滚动到发生更改的行.为此,我在notifyDataSetChanged 之后立即调用smoothScrollToPosition.这行不通.

I've got a custom Adapter for a ListView setup and working ok. On a button click something is changed in the underlying data, so a notifyDataSetChanged is required to refresh the ListView. Fine. But I also want the ListView to scroll to the line where the change occurred. For this I call smoothScrollToPosition immediately after the notifyDataSetChanged. And that is not working.

如果我不调用 notifyDataSetChanged 则滚动有效,因此通知会阻止滚动.我可能正在与将来处理的事件发生冲突.任何人都可以告诉我出了什么问题吗?

If I do not call notifyDataSetChanged then the scroll works, so the notify blocks the scroll. I'm probably having a fight with events being processed in the future. Can anybody give me a pointer on what is going wrong?

推荐答案

调用notifyDataSetChanged()post()方法等待列表更新完毕>:

Use the post() method to wait for the list to finish updating after you call notifyDataSetChanged():

adapter.notifyDataSetChanged();
list.post( new Runnable() {
    @Override
    public void run() {
    //call smooth scroll
    }
  });

这篇关于android中notifyDataSetChanged后的smoothScrollToPosition不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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