Search code examples
androidandroid-recyclerviewcustom-adapter

Update Selected Progressbar in RecyclerView


I have a custom adapter that contains some TextViews and ProgressBars. When the user clicks on each row I want to show the ProgressBar of the selected row and update values. This is my adapter:

 public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
        public TextView from, subject;
        public ProgressBar progressBar;
  public MyViewHolder(View view) {
            super(view);
            from = (TextView) view.findViewById(R.id.from);
            subject = (TextView) view.findViewById(R.id.txt_primary);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            view.setOnLongClickListener(this);
        }
}

and this is my click event in MainActivity

 @Override
    public void onMessageRowClicked(int position) {
        if (mAdapter.getSelectedItemCount() > 0) {
            enableActionMode(position);

        } else {
                message = messages.get(position);
                message.setRead(true);
                messages.set(position, message);
                mAdapter.notifyDataSetChanged();
               downloadFile(message.getFrom(), getBaseUrl() + message.getLink());
        }
    }

and this is my downloadFile

 private void downloadFile(final String filename, final String path){

                    int downloadId = PRDownloader.download(path, dPath, mylFileName)
                            .build()
                            .setOnProgressListener(new OnProgressListener() {
                                @Override
                                public void onProgress(Progress progress) {

                                }
                            })
                            .start(new OnDownloadListener() {
                                @Override
                                public void onDownloadComplete() {

                                    }
                                }

                                @Override
                                public void onError(Error error) {

                                }
                            });
                }
            }).execute(path);
        }
    }

I'm a beginner and I do not know what to do.


Solution

  • Use this to show the value at ProgressBar in your onMessageRowClicked() method

     progressBar.setProgress(value);//value will be of Integer type
    

    In this way,

     .setOnProgressListener(new OnProgressListener() {
                            @Override
                            public void onProgress(Progress progress) {
                                long progressPercent = progress.currentBytes * 100 / progress.totalBytes;
                                progressBar.setProgress((int) progressPercent);
                                progressBar.setIndeterminate(false);
                            }
                        })