I have a custom Listview, where each item contains a progressbar. But when the list contains many items, and I use the scrollbar to navigate through listview, some ProgressB
Check out the Listview with ProgressBar it might help you
try to use a holder for your cell view
put this class at the end of your adapter
class ViewHolder {
TextView txtName,txtStatus;
ImageView imageView;
public ViewHolder(View convertview) {
txtName = (TextView) convertview.findViewById(R.id.txtName );
imageView = (ImageView) convertview.findViewById(R.id.ColImgPath);
//and so on...
}
}
replace:
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_upload, null);
}
with:
if (convertview == null) {
convertview = activity.getLayoutInflater().inflate(R.layout.list_upload, null);
holder = new ViewHolder(convertview);
convertview.setTag(holder);
} else {
holder = (ViewHolder) convertview.getTag();
}
after that do your job with holder...
holder.imageView
instead of imageView and so on for all views
You must set progress state in getView()
method because android reuse ListView
items.
to get you a clue how to resolve this:
`
public View getView(final int position, View convertView, ViewGroup parent) {
.
.
.
Int rowId = holder.id;
UploadTask task = Uploader.getTaskById(rowId);
if (task == null) {
holder.progressBar.setVisibility(View.Gone);
} else {
int progress = task.getUploadProgress();
holder.progressBar.setVisibility(View.Visible);
holder.progressBar.setProgress(progress);
}
}
p.s. You should watch this Google IO video about listviewListView
So for your ProgressBar.
Whenever getView() is called, you set its visibility to GONE.
holder.uploadProgressBar.setVisibility(View.GONE);
So when starts to upload something (and sets uploadProgressBar to VISIBLE), and you scroll down (makes the list item invisible), then scroll up, getView() will be called again, and it will make your ProgressBar invisible.
So you need wrap the state in an object, or use a list to record each item state. For example, in your ImageAdapter
boolean[] uploadings = new boolean[getCount()];
Arrays.fill(uploadings, false);
in your getView()
if (uploadings[position]) {
// You need this, since you are not sure whether you are
// using newly inflated view or ConvertView
holder.uploadProgressBar.setVisibility(View.VISIBLE);
} else {
holder.uploadProgressBar.setVisibility(View.GONE);
}
And in your startUpload()
method, whenever you set your prograss bar to GONE or VISIBILE, set uploadings[position]
to false
or true
correspondingly.
And I think your ImageView probably has the same problem.
In your ViewHolder
class :
static class ViewHolder {
//...
boolean isUploading = false;
//...
}
In your getView()
:
public View getView(final int position, View convertView, ViewGroup parent) {
//...
if(holder.isUploading) {
holder.uploadProgressBar.setVisibility(View.VISIBLE);
} else {
holder.uploadProgressBar.setVisibility(View.GONE);
}
//...
}
In your startUpload()
:
public void startUpload(final int position) {
//...
holder.uploadProgressBar.setVisibility(View.VISIBLE);
holder.isUploading = true;
//...
}
Hope it will work.