Adding a ProgressBar to a ListView/OnClick called a single time

前端 未结 2 1699
走了就别回头了
走了就别回头了 2021-02-09 15:26

The idea is to have a list of items where after clicking an item, a ProgressBar will slowly fill as the task is completed. For example, picture a list of files, with a Download

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-09 16:06

    I have figured this out.

    Instead of calling notifyDataSetChanged(), I stored a reference to each ProgressBar in the DownloadItem object. Then, when scrolling through the ListView, when old objects were passed as convertView, I removed the ProgressBar from the old DownloadInfo and put it on the new one.

    Thus, getView for my array adapter then became:

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final DownloadInfo info = getItem(position);
        // We need to set the convertView's progressBar to null.
    
        ViewHolder holder = null;
    
        if(null == row) {
          LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          row = inflater.inflate(R.layout.file_download_row, parent, false);
    
          holder = new ViewHolder();
          holder.textView = (TextView) row.findViewById(R.id.downloadFileName);
          holder.progressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
          holder.button = (Button)row.findViewById(R.id.downloadButton);
          holder.info = info;
    
          row.setTag(holder);
        } else {
          holder = (ViewHolder) row.getTag();
    
          holder.info.setProgressBar(null);
          holder.info = info;
          holder.info.setProgressBar(holder.progressBar);
        }
    
        holder.textView.setText(info.getFilename());
        holder.progressBar.setProgress(info.getProgress());
        holder.progressBar.setMax(info.getFileSize());
        info.setProgressBar(holder.progressBar);
    
        holder.button.setEnabled(info.getDownloadState() == DownloadState.NOT_STARTED);
        final Button button = holder.button;
        holder.button.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            info.setDownloadState(DownloadState.QUEUED);
            button.setEnabled(false);
            button.invalidate();
            FileDownloadTask task = new FileDownloadTask(info);
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
          }
        });
        return row;
      }
    

    The download AsyncTask would then set the progress on the progressBar, if it was not null, and this worked as expected.

    I uploaded the corrected code to GitHub, you can see it here: https://github.com/mdkess/ProgressBarListView

提交回复
热议问题