对于那些面临类似问题的人,我找到了一个解决办法:
我使用了AsyncTask(膨胀部分在后台完成,而仅仅添加视图在前台完成)
public class MyAsync extends AsyncTask<Void, View, Void> {
LayoutInflater inflater;
Activity activity;
LinearLayout parent;
List<Integer> layoutIds;
public MyAsync(Activity activity,LinearLayout linLayout,List<Integer> layoutIds) {
this.activity = activity;
this.parent = linLayout;
this.layoutIds = layoutIds;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
inflater = activity.getLayoutInflater();
}
@Override
protected Void doInBackground(Void... voids) {
for (int i = 0; i < layoutIds.size(); i++) {
View view = inflater.inflate(layoutIds.get(i), null);
publishProgress(view);
}
return null;
}
@Override
protected void onProgressUpdate(View... view) {
super.onProgressUpdate(view);
parent.addView(view[0]);
}
}