简体   繁体   English

未聚焦时更新ListView片段

[英]Updating ListView fragment when not focused

I'm working on a tablet application with a ListView fragment on the left, and some other views including a SearchView (essentially a TextView) on the right. 我正在使用左侧是ListView片段的平板电脑应用程序,以及右侧的一些其他视图,包括SearchView(本质上是一个TextView)。 When the SearchView is focused, I want to update the data of the listview, but I can't. 当SearchView聚焦时,我想更新列表视图的数据,但我不能。 If I notifyDataSetChanged on the UI thread, nothing happens. 如果我在UI线程上的notifyDataSetChanged ,没有任何反应。 If I call setAdapter() again on my listview, the listview goes blank. 如果我在listview上再次调用setAdapter() ,则listview将变为空白。

Only when I touch the listview again (it gets temporary focus again or something to cause getView() to be called on the adapter) does it get updated. 只有当我再次触摸列表视图时(它再次获得临时焦点或者导致在适配器上调用getView()的东西)才会更新。

Anyone got an idea on how to fix this? 有人知道如何解决这个问题吗?

Min-Sdk and Target is 4.0.3. Min-Sdk和Target是4.0.3。

Did you try to call your refresh routine using runOnUiThread() ? 您是否尝试使用runOnUiThread()调用刷新例程?

Reform your objects list, bind to a new instance of your adapter, then call notifyDataSetChanged() and setAdapter() again in refresh routine would work. 改造对象列表,绑定到适配器的新实例 ,然后在刷新例程中再次调用notifyDataSetChanged()setAdapter()将起作用。

您必须使用ViewHolder来处理ListView中的更新。

What now works for me is updating the data in the underlying adapter and then calling notifyDataSetChanged on that adapter, all from the UI thread. 现在适合我的是更新底层适配器中的数据,然后在UI线程上调用该适配器上的notifyDataSetChanged

I did not need to call setAdapter again on the listView. 我不需要再次在listView上调用setAdapter

Of course I had already tried all this before asking the question, so not sure what made it suddenly start working. 当然,在提出问题之前我已经尝试了所有这些,所以不确定是什么让它突然开始工作。

Call it inside the runOnUiThread method. 在runOnUiThread方法中调用它。 Also add the new object to the adapter inside it. 还要将新对象添加到其中的适配器。

Like that: 像那样:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        chatListAdapter.add(message);
        chatListAdapter.notifyDataSetChanged();
    }
});

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

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