Android 在完成特定方法之前,如何强制视图更新其状态?

Android 在完成特定方法之前,如何强制视图更新其状态?,android,Android,我有RealViewSwitcher控件,我正在使用一些代码更新屏幕SwitchListener中的UI,如下所示 public void onScreenSwitched(int screen) { //1 update some text view for example //2 heavy process } 我的UI更新直到繁重的过程完成后才会出现,我如何能即时更新我的文本视图 如何即时更新文本视图 从本质上说,你不能 但是,最有可能的情况是,您的“繁重过程”应该在后台线程中完

我有RealViewSwitcher控件,我正在使用一些代码更新屏幕SwitchListener中的UI,如下所示

public void onScreenSwitched(int screen) {
//1 update some text view for example 

//2 heavy process 

}
我的UI更新直到繁重的过程完成后才会出现,我如何能即时更新我的文本视图

如何即时更新文本视图

从本质上说,你不能

但是,最有可能的情况是,您的“繁重过程”应该在后台线程中完成,例如
AsyncTask
。这将使您的UI更新得到更快的处理

如果您的“重过程”本身就是UI更新,那么您需要安排将“重过程”延迟到当前方法返回并且Android可以处理您的第一组UI更新之后。实现这一点的一种方法是将“繁重的流程”工作放入一个
可运行的
,您可以使用
Handler
上的
post()
方法或任何
视图

私有类YouClass>{
private class YouClass extends AsyncTask<Type1, Type2, Type3> {
 protected Long doInBackground(Type1... obj1) {
     //operations to do in background
 }

 protected void onProgressUpdate(Type2... obj2) {
     //operations during the process progress
 }

 protected void onPostExecute(Type3 obj3) {
     //the operations after the completion of your task
 }
}
受保护的长doInBackground(类型1…obj1){ //要在后台执行的操作 } 受保护的void onProgressUpdate(类型2…obj2){ //工艺过程中的操作 } PostExecute上受保护的void(类型3 obj3){ //完成任务后的操作 } }
有关AsyncTask goto的更多参考信息