ListView

时间:2016-03-05 08:08:40

标签: android listview android-asynctask

我有ListView,其中点击按钮时应添加ProgressTabImageView的新项目ImageView.getVisibility() == View.GONE

ImageView开始转换为新的位图(在AsyncTask中),转换进度显示在ProgressTab中。转换结束后,ProgressTab将被删除,ImageView将设置为位图。 (现在ImageView.getVisibility() == View.VISIBLE

但是,经过3或4次重复后,它无法正常工作,并且在以下所有ImageView中设置了来自先前处理过的随机图像。 ListView重复其所有元素。怎么改呢? (对我的英语很抱歉)

适配器

public class ListAdapter extends BaseAdapter {

Context context;
List<Data> dataList;

public ListAdapter(Context context, List<Data> dataList) {
    this.context = context;
    this.dataList = dataList;
}

@Override
public int getCount() {
    return dataList.size();
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

static class ViewHolder{
    ImageView image;
    ProgressBar progressBar;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    View v = convertView;
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inf.inflate(R.layout.item_progress, null);

        holder = new ViewHolder();
        holder.image = (ImageView) v.findViewById(R.id.itemProgress_previewImage);
        holder.progressBar = (ProgressBar) v.findViewById(R.id.itemProgress_progressBar);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (holder.progressBar.getProgress() == 0 && holder.progressBar.getVisibility() == View.VISIBLE) {
        ConvertImageTask convertImageTask = new ConvertImageTask(holder.progressBar, holder.image, dataList.get(position).getTypeOfConverting());
        convertImageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataList.get(position).getBitmap());
    }

    return v;
}

的AsyncTask

public class ConvertImageTask extends AsyncTask<Bitmap, Integer, Bitmap> {

private static final String TAG = "WHALETAG";
Random random = new Random();
int timeout = random.nextInt(5)+2; // for artificial slowing of converting

private static final int TYPE_ROTATE = 0;
private static final int TYPE_INVERT = 1;
private static final int TYPE_MIRROR = 2;

private int type = -1;

ProgressBar bar;
ImageView image;
public ConvertImageTask(ProgressBar bar, ImageView imageView, int type) {
    this.bar = bar;
    this.image = imageView;
    this.type = type;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    bar.setMax(timeout);
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    int i = values[0];
    bar.setProgress(i);
}

@Override
protected Bitmap doInBackground(Bitmap... bitmaps) {

    Bitmap bitmap = bitmaps[0];

    switch(type){
        case TYPE_ROTATE:
            bitmap = rotateBitmap(bitmap);
            break;
        case TYPE_INVERT:
            bitmap = invertBitmap(bitmap);
            break;
        case TYPE_MIRROR:
            bitmap = mirrorBitmap(bitmap);
            break;
    }

    // slow converting
    for (int i=1; i<=timeout; i++) {
        Log.d(TAG, "doInBackground: left " + (timeout-i));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        publishProgress(i);
    }

    return bitmap;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    Log.d(TAG, "onPostExecute: convertation is over " + type);
    bar.setVisibility(View.GONE);
    image.setImageBitmap(bitmap);
    image.setVisibility(View.VISIBLE);
}

0 个答案:

没有答案