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

前端 未结 2 1700
走了就别回头了
走了就别回头了 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

    0 讨论(0)
  • 2021-02-09 16:25

    Did you try setting click listener to download item in adapter itself like following:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if(row == null) {
            // Inflate
            Log.d(TAG, "Starting XML inflation");
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.download_list_item, parent, false);
            Log.d(TAG, "Finished XML inflation");
        }
    
        final DownloadItem item = mItems.get(position);
    
        ProgressBar downloadProgressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
        Button downloadButton = (Button) row.findViewById(R.id.downloadButton);
    
        downloadButton.setTag(item);
        downloadProgressBar.setMax(item.length);
        downloadProgressBar.setProgress(item.progress);
    
        downloadButton.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                new DownloadTask(DownloadArrayAdapter.this, item).execute();
            }
        });
    
        return row;
    }
    
    0 讨论(0)
提交回复
热议问题