5

我有一个 listView 并且在每个项目上我都添加了一个 progressBar,它需要在图像下载后消失。但我找不到办法做到这一点。我试图让它在 getView 类上消失,但它不会在下载图像后立即消失。

例如,在 AsyncTask 类的 DoInBackground 上向 scrollView 添加一些视图时,我可以下载图像,然后 onPostExecute 我可以设置图像,然后删除进度条。这工作得很好。我想为 listView 做这样的事情。有人可以帮我吗?

我不知道我是否清楚,但我可以总结一下,我的 list_item.xml 包含 imageview 和 progressbar。我想让这些进度条在下载和设置图像后消失。

感谢您的帮助。

这是我的适配器类:

class myListAdapter extends ArrayAdapter<Items> {

    private ArrayList<Items> items;
    private Context ctx;

    public myListAdapter(Context context, int textViewResourceId,
            ArrayList<Items> items) {
        super(context, textViewResourceId, items);
        this.ctx = context;
        this.items = items;
    }

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

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

        if (v == null) {
            LayoutInflater inf = (LayoutInflater) ctx
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            v = inf.inflate(R.layout.main_list_item, null);
        }

        Items index = listContents.get(position);

        if (index != null) {

            ImageView img = (ImageView) v.findViewById(R.id.listImage);
            TextView title = (TextView) v.findViewById(R.id.listTopText);
            TextView content = (TextView) v.findViewById(R.id.listDownText);

            if (title != null)
                title.setText(index.getTitle());

            if (content != null)
                content.setText(index.getContent());

            if (img != null) 
                img.setImageBitmap(index.getImageBitmap());
            ((ProgressBar) v.findViewById(R.id.listItemProgressBar)).setVisibility(View.GONE);

        }

        return v;
    }
}

因此,在下面的这一行中,我使进度条消失了,但是在下载并设置图像后它应该消失了……但是在添加的列表项上没有进度条,这是我的问题吗?

 ((ProgressBar) v.findViewById(R.id.listItemProgressBar)).setVisibility(View.GONE);
4

2 回答 2

4

你可以这样做,但这不是最好的做法。你应该自己检查线程同步。并使用 LruCache 或 WeakRefrences 缓存位图。演示仅显示逻辑。

final class MyAdapter extends BaseAdapter {

    private final HashMap<String, Bitmap> mImageMap;
    private final HashSet<String> mDownloadingSet;

    public MyAdapter() {
        mImageMap = new HashMap<String, Bitmap>();
        mDownloadingSet = new HashSet<String>();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return NUMBER_YOU_WANT;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setImage(String url, Bitmap bitmap) {
        mDownloadingSet.remove(url);
        if (bitmap != null) {
            mImageMap.put(url, bitmap);
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.grid_view,
                    parent, false);
            holder = new ViewHolder();

            /***
             * find the views.
             */

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String url = "";// Get the image url here.

        if (mImageMap.containsKey(url)) {
            holder.image.setImageBitmap(mImageMap.get(url));
            holder.progressBar.setVisibility(View.GONE);
        } else {
            holder.image.setImageResource(R.drawable.img_downloading);
            holder.progressBar.setVisibility(View.VISIBLE);
            if (!mDownloadingSet.contains(url)) {
                ImageDownloader task = new ImageDownloader();
                mDownloadingSet.add(url);
                task.execute(url);
            }

        }

        return convertView;
    }
}

static class ViewHolder {
    ImageView image;
    ProgressBar progressBar;
}

final class ImageDownloader extends AsyncTask<String, Void, Bitmap> {

    String url;

    @Override
    protected Bitmap doInBackground(String... params) {
            url = params[0];
        final Bitmap bitmap = fetchUrlAndDecode(params[0]);
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        mAdapter.setImage(url, result);
    }

    private Bitmap fetchUrlAndDecode(String url) {
        Bitmap bitmap = null;

        /**
         * Fetch your bitmap and decode it.
         */

        return bitmap;
    }

}
于 2012-05-11T23:30:22.803 回答
1

这是您的 XML:

 <RelativeLayout
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:paddingLeft="5dp" >

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/text_progress"
        android:indeterminate="false"
        android:layout_centerInParent="true"
        android:progress="50"
        android:max="100" />

     <TextView
        android:id="@+id/text_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="15dp"
        android:text="0%"/>

</RelativeLayout>

并覆盖 getView# 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.oportunidade_item_list, null);

    ProgressBar title = (ProgressBar) vi.findViewById(R.id.progress);
    title.setProgress(Integer.parseInt(((Oportunidade) get(position)).getProbabilidade()));
    return super.getView(position, vi, parent);
}

调用超级,它会为你设置其他视图的值,如果你这样做了ArrayList<HashMap<?, ?>>

就这些;

于 2012-08-30T17:37:14.683 回答