Android:在列表视图中设置进度条

时间:2015-09-04 00:12:50

标签: android listview progress-bar

我正在尝试在列表视图中放置一个进度条,以便一旦从Facebook好友列表中获取图像就可以加载图像。我在XML布局中声明了进度条。我实现了异步方法。我将图像设置为不可见,在后执行方法中,我将其设置为可见,进度条消失。但是,进度条保持在视图旋转状态,并且不会消失。

我的问题与此问题非常相似,但未提供解决方案。

Android ProgressBar inside ImageView

我的自定义ArrayAdaptor中的代码如下:

更新:

static class ViewHolder {
    public TextView textview;
    public CircularImageView image;
}



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


    //REUSES THE VIEW

    if(convertView==null){

        // inflate the layout

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

        convertView = inflater.inflate(layoutResourceId, parent, false);


        // configure view holder
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.textview = (TextView) convertView.findViewById(R.id.textViewItem);
        viewHolder.image = (CircularImageView) convertView.findViewById(R.id.fb_friendpics);
        convertView.setTag(viewHolder);

    }

    // fill data
    ViewHolder holder = (ViewHolder) convertView.getTag();

     ObjectItem objectItem = data[position];

    holder.textview.setText(objectItem.itemName);
    holder.textview.setVisibility(View.INVISIBLE);

    //call the execute cmd
    ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressbar_fbpics);


    holder.image.setVisibility(View.INVISIBLE);


    new DownloadImageTask(holder, progressBar)
            .execute("https://graph.facebook.com/" + objectItem.itemId + "/picture?type=large");

    return convertView;

}


private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    ProgressBar progressBar;
    ViewHolder holder;

    public DownloadImageTask(ViewHolder holder, ProgressBar progressBar) {

        this.holder = holder;
        this.progressBar = progressBar;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

        if(result!=null ) {
            Log.d("IMAGE: " , "SET");
            progressBar.setVisibility(View.GONE);

            holder.image.setImageBitmap(result);
            holder.image.setVisibility(View.VISIBLE);
            holder.textview.setVisibility(View.VISIBLE);

        }

    }
}

一旦我进入屏幕,它就打印了很多次,因为我只需打印两次,因为我只需要联系。

09-03 19:53:21.587  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.632  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.668  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.718  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.773  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.829  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.891  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:21.947  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.035  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.086  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.153  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.228  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.389  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.675  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:22.844  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:23.028  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:23.230  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:23.432  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:23.769  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:23.954  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:24.139  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:24.324  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET 
09-03 19:53:24.677  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:24.863  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.089  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.393  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.609  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.674  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.754  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.805  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.872  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.923  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:25.973  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:26.024  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
09-03 19:53:26.091  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.142  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET 
 09-03 19:53:26.211  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.277  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.328  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.395  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.480  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.548  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.633  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.681  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.748  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.799  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.849  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET 
 09-03 19:53:26.900  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:26.969  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:27.020  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:27.085  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:27.153  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:27.220  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET
 09-03 19:53:27.271  30339-30339/com.voiceup.voiceup D/IMAGE:﹕ SET

1 个答案:

答案 0 :(得分:0)

根据您的代码,您引用的是progressbar,它被声明为DownloadTask外部类中的一个字段。您无法直接访问Adapter.getView()中的progressbar

除此之外,我会提出两点建议:

  1. 我强烈建议您为此选择第三方图片加载库。例如,请查看PicassoIonFresco
  2. 在适配器中使用视图回收。我可以看到你没有使用任何类型的回收。请查看this video和此tutorial