Android: 在列表视图中设置进度条
Android: Setting the progressbar inside a listview
我正在尝试在列表视图中放置一个进度条,以便在从 facebook 好友列表中获取图像后可以加载图像。我在 XML 布局中声明了进度条。我实现异步方法。我将图像设置为不可见,并在 post 执行方法中将其设置为可见,并将进度条设置为消失。但是,进度条仍然在视图上旋转并且不会消失。
我的问题和这个问题非常相似,但是没有提供解决方案。
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
根据您的代码,您引用的 progressbar
被声明为 DownloadTask 外部 class 中的一个字段。您无法直接访问 Adapter.getView() 中的 progressbar
。
除此之外,我还有两个建议:
- 我强烈建议您为此选择第 3 方图像加载库。例如,看看 Picasso, Ion, and Fresco.
- 在您的适配器中使用视图回收。我可以看到你没有使用任何回收方式。看看this video, and this tutorial.
我正在尝试在列表视图中放置一个进度条,以便在从 facebook 好友列表中获取图像后可以加载图像。我在 XML 布局中声明了进度条。我实现异步方法。我将图像设置为不可见,并在 post 执行方法中将其设置为可见,并将进度条设置为消失。但是,进度条仍然在视图上旋转并且不会消失。
我的问题和这个问题非常相似,但是没有提供解决方案。
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
根据您的代码,您引用的 progressbar
被声明为 DownloadTask 外部 class 中的一个字段。您无法直接访问 Adapter.getView() 中的 progressbar
。
除此之外,我还有两个建议:
- 我强烈建议您为此选择第 3 方图像加载库。例如,看看 Picasso, Ion, and Fresco.
- 在您的适配器中使用视图回收。我可以看到你没有使用任何回收方式。看看this video, and this tutorial.