了解 ListActivity 中的处理程序
我正在查看 ListActivity
源代码,我看到正在定义一个私有 Handler
,并且发布了一个 Runnable
到 onContentChanged()
方法中的该处理程序。
我不太明白这一点,因为据我了解,处理程序是用于线程间通信的。这里,处理程序的定义和发布发生在同一线程上,并且 post() 调用中没有指定延迟。我也看不到该处理程序被用于其他任何用途。
我可能误解了这里处理程序的使用。为什么按照这里的方式完成,而不是直接运行 mList.focusableViewAvailable() (可运行内部的调用)?结果不是一样吗?
下面是我认为的 ListActivity
源代码的相关部分:
public class ListActivity extends Activity {
protected ListView mList;
private Handler mHandler = new Handler();
private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
/**
* Updates the screen state (current list and other views) when the
* content changes.
*
* @see Activity#onContentChanged()
*/
@Override
public void onContentChanged() {
super.onContentChanged();
View emptyView = findViewById(com.android.internal.R.id.empty);
mList = (ListView)findViewById(com.android.internal.R.id.list);
if (mList == null) {
throw new RuntimeException(
"Your content must have a ListView whose id attribute is " +
"'android.R.id.list'");
}
if (emptyView != null) {
mList.setEmptyView(emptyView);
}
mList.setOnItemClickListener(mOnClickListener);
if (mFinishedStart) {
setListAdapter(mAdapter);
}
mHandler.post(mRequestFocus);
mFinishedStart = true;
}
}
I'm looking at the ListActivity
source code, and I'm seeing that a private Handler
is being defined, and that a Runnable
is posted to this handler in the onContentChanged()
method.
I don't quite get the point of this, as the handlers, as I understand it, are there for inter-thread communication. Here, the definition of the handler and the posting is happening on the same thread, and no delay is specified in the post() call. I can't see the handler being used for anything else, either.
I've probably misunderstood something about the use of handlers here. Why is it done the way it is here, and not by just running mList.focusableViewAvailable()
(the call inside the runnable) directly? Wouldn't the result be the same?
Beneath is what I believe are the relevant portions of the ListActivity
source code:
public class ListActivity extends Activity {
protected ListView mList;
private Handler mHandler = new Handler();
private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
/**
* Updates the screen state (current list and other views) when the
* content changes.
*
* @see Activity#onContentChanged()
*/
@Override
public void onContentChanged() {
super.onContentChanged();
View emptyView = findViewById(com.android.internal.R.id.empty);
mList = (ListView)findViewById(com.android.internal.R.id.list);
if (mList == null) {
throw new RuntimeException(
"Your content must have a ListView whose id attribute is " +
"'android.R.id.list'");
}
if (emptyView != null) {
mList.setEmptyView(emptyView);
}
mList.setOnItemClickListener(mOnClickListener);
if (mFinishedStart) {
setListAdapter(mAdapter);
}
mHandler.post(mRequestFocus);
mFinishedStart = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该关心
Handler
。您关心的应该是对post()
的调用。Handler
甚至不是真正需要的,因为post()
在View
上可用——不过,此代码可能早于该代码。post()
获取一个Runnable
并将其放入主应用程序线程的消息队列中。因此,只有当该队列中当前的所有其他消息都得到处理 (FIFO) 时,它才会得到处理。据推测,在focusableViewAvailable()
成功工作之前,ListActivity
需要先处理队列上的其他消息。Your concern should not be the
Handler
. Your concern should be the call topost()
. AHandler
is not even really needed, aspost()
is available onView
-- this code may pre-date that, though.post()
takes aRunnable
and puts it on the message queue for the main application thread. As such, it will not get processed until all other messages that are presently on that queue get processed (FIFO). Presumably,ListActivity
needs some other message on the queue to be processed first beforefocusableViewAvailable()
will work successfully.