我在异步任务中运行以下代码。但由于某种原因,onGlobalLayout 代码在我的主 Activity 线程中运行。我怎样才能阻止这个并确保代码在我的 asynctask 线程中运行?
textViewAll.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
//My code occurs here
}
});
最佳答案
How can I stop this and make sure the code is run in my asynctask thread?
通过扩展AsyncTask
创建一个内部类,并在doInBackground
方法中移动正在执行长操作的内容的所有代码,如果想要更新UI根据长操作结果的元素然后从doInBackground
返回结果并在onPostExecute
方法中获取它。
只需在 onGlobalLayout
方法中调用 AsyncTask.execute()
即可。
关于java - 在 asynctask 线程 Android 中运行 View 监听器回调代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33742022/