我正在尝试加载一个包含 3 个片段的活动。对于每个片段,我使用 Asynctask,并在每个片段的 onPostExecute() 方法上加载 Ui 元素。问题是,当我在 Asynctask 加载 ui 元素时从片段滑动到片段时,应用程序真的很慢。我试图通过调用 onUiThread() 来解决这个问题,它稍微降低了速度。Asynctasks 用于片段的 onCreateView 方法。
我能做些什么来摆脱这种滞后?
这是一个 Asynctask :
public class MovieCommentsAsynctask extends AsyncTask<String, Void, String> {
private String TAG = "MovieCommentsAsynctask";
private Context context;
private boolean test;
private HttpClient client;
private List<MovieCommentsObject> comments;
private LayoutInflater inflater;
private Activity activity;
//
private RelativeLayout rl;
private ProgressBar pb;
private PullToRefreshScrollView sv;
private LinearLayout ll;
private Button btn;
public MovieCommentsAsynctask(Context context, RelativeLayout rl,
boolean test, Activity activity) {
this.context = context;
this.activity = activity;
this.rl = rl;
this.test = test;
client = new DefaultHttpClient();
bind();
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private void bind() {
ll = (LinearLayout) rl.findViewById(R.id.ll_fragment_movie_comments);
sv = (PullToRefreshScrollView) rl
.findViewById(R.id.sv_fragment_movie_comments);
pb = (ProgressBar) rl.findViewById(R.id.pb_fragment_movie_comments);
btn = (Button) rl.findViewById(R.id.btn_fragment_movie_comments_add);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i(TAG, "onPreExecute");
if (test) {
pb.setVisibility(View.VISIBLE);
} else {
}
}
@Override
protected String doInBackground(String... params) {
Log.i(TAG, "doInBackground");
String url = params[0];
BasicResponseHandler handler = new BasicResponseHandler();
HttpGet get = new HttpGet(url);
try {
return client.execute(get, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i(TAG, "onPostExecute");
pb.setVisibility(View.GONE);
if (result != null && result.startsWith("[")) {
Log.i(TAG, "restult = " + result);
Gson gson = new Gson();
Type type = new TypeToken<List<MovieCommentsObject>>() {
}.getType();
comments = gson.fromJson(result, type);
init();
}
}
private void init() {
for (int i = 0; i < comments.size(); i++) {
MovieCommentsObject comment = comments.get(i);
addComment(comment);
}
ll.setVisibility(View.VISIBLE);
}
private void addComment(final MovieCommentsObject comment) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final View v = inflater.inflate(R.layout.adapter_movie_comments, null);
SmartImageView siv = (SmartImageView) v
.findViewById(R.id.siv_adapter_movie_comments);
WebView wv_user = (WebView) v
.findViewById(R.id.wv_adapter_movie_comments_user);
WebView wv_date = (WebView) v
.findViewById(R.id.wv_adapter_movie_comments_date);
WebView wv_text = (WebView) v
.findViewById(R.id.wv_adapter_movie_comments_text);
TextView tv_type = (TextView) v.findViewById(R.id.tv_adapter_movie_comments_type);
wv_text.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
wv_user.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
wv_date.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
long date = comment.getInserted();
if (date > 0) {
FunctionsGeneral.justify(wv_date, FunctionsGeneral.getTime(date));
}
FunctionsGeneral.justify(wv_user, comment.getUser().getUsername());
FunctionsGeneral.justify(wv_text, comment.getText_html());
String image = comment.getUser().getAvatar();
if (image != null && image.trim().length() > 0) {
siv.setImageUrl(image);
}
if(comment.getType() != null){
tv_type.setText(comment.getType());
}
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 10;
ll.addView(v, params);
}
});
}
}