我在列表视图中有按钮和进度条(不可见)。当按钮单击它应该消失并且进度条应该可见并开始运行(从网络服务器下载),当它完成时运行按钮应该再次出现。现在,当我单击第一项的按钮时,进度条会运行,但是如果我向下滚动直到第一项离开屏幕,我会看到进度条与列表视图的最后一项上的第一项的进度条同时运行。如果我向上滚动第一项的进度条运行正常。如果我单击第二个项目,列表视图的倒数第二个项目也会发生同样的情况。有什么问题,我该如何解决?请帮忙!!!
4 回答
Choose the concept of viewHolder class which hold the state of listview untill the process is complete. Urs main problem is that when u scroll the listview ,view gets refreshs son only it come to initial state.
Class ViewHolder
{
TextView mtext;
Button mButton;
ProgressBar mBar;
}
In getview method initialise the viewholder class
links:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html http://developer.android.com/training/contacts-provider/display-contact-badge.html
尝试这样的事情......我正在使用不同的视图来下载 pdf,但你可以使用这个概念
Holder holder;
class Holder {
TextView txt_pdfname;
Button btn_download;
ImageView img_pdficon;
}
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
// position=arg0;
View v=arg1;
if (v == null) {
holder = new Holder();
v = layoutInflater.inflate(R.layout.cell_document, arg2,false);
holder.txt_pdfname = (TextView) v.findViewById(R.id.txt_pdfname);
holder.img_pdficon=(ImageView)v.findViewById(R.id.img_pdficon);
holder.btn_download = (Button) v.findViewById(R.id.btn_download);
v.setTag(holder);
} else{
holder = (Holder) v.getTag();
}
File pdfFile=new File(Environment.getExternalStorageDirectory().toString()+"/fpapdf/"+(pdfUrl[arg0].substring(pdfUrl[arg0].lastIndexOf('/')+1)));
if(pdfFile.exists()){
holder.btn_download.setVisibility(View.INVISIBLE);
}else
holder.btn_download.setVisibility(View.VISIBLE);
holder.txt_pdfname.setText(this.pdfArray[arg0]);
holder.img_pdficon.setImageResource(pdfImage[arg0]);
holder.btn_download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// YOUR CODE HERE
}
});
return v;
}
有关更多信息,请参阅Romain
只需用此替换您的 getView,我将通过此删除 if(view==null) 您每次都会获得除标签以外的新视图
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ArrayList<String> array=JournalArray.get(position);
final ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
view = new ViewHolder();
convertView = inflator.inflate(R.layout.familylist_item, null);
view.progress=(ProgressBar)convertView.findViewById(R.id.downprogress);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.text);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
view.imgAR=(Button)convertView.findViewById(R.id.imageAR);
view.imgAR.setTag(view);
view.imgDown=(Button)convertView.findViewById(R.id.imageDown);
view.imgDown.setTag(view);
view.imgPDF=(Button)convertView.findViewById(R.id.imagePDF);
view.imgPDF.setTag(view);
view.progress=(ProgressBar)convertView.findViewById(R.id.downprogress);
view.btnDel=(Button)convertView.findViewById(R.id.btnDel);
view.btnDel.setTag(view);
convertView.setTag(view);
}
您必须记住正在下载的位置、进度(如果您没有显示不定条),并在适配器的 getView 中更新这些值。这实际上很快变得非常复杂 - 如果您想在下载完成后再次更新视图,您必须对列表视图的工作方式和它们回收视图的方式进行大量非常仔细的编码,或者您必须更新整个视图listview 每当文件完成下载或更新进度时,可能会导致闪烁。