listview error when scrolling down

大城市里の小女人 提交于 2019-12-11 11:33:21

问题


I have button and progress bar(invisible) in listview. When button clicks it should disappear and progress bar should be visible and start running (downloading from web server) and when it is done with running button should appear again. Now when I click first item's button, the progress bar runs but if I scroll down until first item goes off screen I see progress bar running simultaneously with first item's progress bar on last item of listview. If I scroll up the first item's progressbar runs normally. It happens same if I click second item the the second last item of listview does the same. What is the problem and how could I solve it? Please help!!!


回答1:


You have to remember what positions are downloading, their progress (if you're not showing an indefinite bar), and update those values in your adapter's getView. This actually gets very complicated quickly though- if you want to update the view again when the download finishes, you have to do a lot of very careful coding do to how listviews work and the way they recycle views, or you have to update the whole listview whenever a file finishes downloading or updates progress, which may result in flicker.




回答2:


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




回答3:


try something like this...I am using different views for pdf downloading but you can use this concept

    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;
    }

for more information see this by Romain




回答4:


just replace your getView with this, i am removing the if(view==null) by this you'll get every time a new view else than from the tag

@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);
}


来源:https://stackoverflow.com/questions/16515077/listview-error-when-scrolling-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!