如何从Android中的getView()函数中的隐藏项取消AsyncTask执行?

ngu*_*sao 5 android listview android-arrayadapter android-asynctask

我有一个问题:在ArrayAdapter的getView()函数中,我使用AsyncTask执行将图像加载到ImageView.但是当这个项目不可见(滚动列表视图)时,AsyncTask函数仍然加载图像.如果item不可见,我想禁用AsyncTask进程.有解决这个问题的方法吗?

Dav*_*and 0

您可以检查图像何时加载,是否仍然是应显示的正确 ListView 项目。如这个答案中所述,首先将路径存储为ImageView的标签,然后签入onPostExecute

protected void onPostExecute(Bitmap result) {
    if (!imv.getTag().toString().equals(path)) {
           /* The path is not same. This means that this
              image view is handled by some other async task. 
              We don't do anything and return. */
           return;
    }

    if(result != null && imv != null){
        imv.setVisibility(View.VISIBLE);
        imv.setImageBitmap(result);
    }else{
        imv.setVisibility(View.GONE);
    }
}
Run Code Online (Sandbox Code Playgroud)

但位图仍将完全加载。