我需要用文本信息填充一个ListView,这些信息需要一定时间来收集。我的方法是使用AsyncTask来执行后台任务,但是当结果到达时将文本设置到TextView会减缓列表的速度:每次调用getView()时都会出现卡顿。
以下是我的AsyncTask类:
以下是我的AsyncTask类:
private class BreadcrumbTask extends AsyncTask<FFile, Void, String>{
private WeakReference<TextView> mTextView;
public BreadcrumbTask(TextView textView){
mTextView = new WeakReference<TextView>(textView);
}
@Override
protected String doInBackground(FFile... params) {
// text processing
}
@Override
protected void onPostExecute(String result) {
if (mTextView != null){
TextView tv = mTextView.get();
if (tv != null)
//this line blocks the UI. if I comment it the lag is gone
tv.setText(result);
}
// 在getView()中创建新任务并执行它。 // 问题显然来自于onPostExecute()中的tv.setText(result)。当我注释掉这个时,列表流畅运行。那么如何更新TextView而不会减慢UI呢?