Android:在listview中设置progressbar
我正在尝试在listview中放置一个进度条,以便在从facebook好友列表中获取图片后可以加载它们。我在XML布局中声明进度条。我实现了异步方法。我将图像设置为不可见,并在post-execute方法中将其设置为可见,将进度条设置为消失。但是,进度条仍在视图上旋转,并且不会消失 我的问题与这个问题非常相似,但没有提供解决方案 在自定义阵列适配器中,我的代码如下所示: 更新:Android:在listview中设置progressbar,android,listview,progress-bar,Android,Listview,Progress Bar,我正在尝试在listview中放置一个进度条,以便在从facebook好友列表中获取图片后可以加载它们。我在XML布局中声明进度条。我实现了异步方法。我将图像设置为不可见,并在post-execute方法中将其设置为可见,将进度条设置为消失。但是,进度条仍在视图上旋转,并且不会消失 我的问题与这个问题非常相似,但没有提供解决方案 在自定义阵列适配器中,我的代码如下所示: 更新: static class ViewHolder { public TextView textview;
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);
}
}
}
根据您的代码,您正在引用一个
progressbar
,它在DownloadTask的外部类中声明为一个字段。您无法直接访问Adapter.getView()中的progressbar
除此之外,我还要提出两项建议:
谢谢你的帮助。您认为我需要使用NotifyDataSetChanged()吗?实现视图回收是提高性能的一个好主意。没有提供执行此操作的代码。我是否应该为此使用视图持有者?当您更改基础数据时,应该调用NotifyDataSetChanged。例如,如果添加或删除适配器中的项。但是,当您使用ArrayAdapter.add、ArrayAdapter.remove等时,默认的ArrayAdapter实现将自动为您调用NotifyDataSetChanged,因此无需显式调用它。我只在列表视图上显示Facebook好友列表。因此,数据中的任何内容都不会被删除或添加。因此,我认为我不需要使用NotifyDataSetChanged()。对于视图回收,您有相关文档吗?只要看看我在回答中发送的2个链接,就可以开始了。@WindsuferOak我更新了上面的代码。我能够使用视图保持器,这提高了列表视图的性能。我将调查毕加索。我想知道你是否能在另一个问题上帮助我
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