如何更新recyclerview项目中的上传进度百分比?

时间:2019-05-08 10:10:08

标签: java android android-recyclerview

我想将应用程序中的文件列表上传到Amazon s3存储桶。我可以获取上载进度,但无法在“回收者”视图项中设置上载进度值。 (我想在与其他设备共享文件时显示与Shareit应用程序类似的文件共享进度。)

我已经实现了recyclerview,并且效果很好。我在每个要上传的文件上都有进度条和Textview。我只想在进度条和每个项目的文本视图中显示上传进度。

public void onStateChanged(int id, TransferState state) {
    if (state.COMPLETED.equals(observer.getState())) {

        Toast.makeText(thisActivity, "File Upload Complete", Toast.LENGTH_SHORT).show();

    }

}

@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
    long _bytesCurrent = bytesCurrent;
    long _bytesTotal = bytesTotal;

    float percentage = (int) ((float)_bytesCurrent /(float)_bytesTotal * 100);
    Log.d("percentage","" + percentage);

}

@Override
public void onError(int id, Exception ex) {
    Toast.makeText(thisActivity, "" + ex.getMessage(), Toast.LENGTH_SHORT).show();
    Log.d("ErrorMessage","" + ex.getMessage());

}

//下面的Recyclerview适配器代码

public class FileListAdapter extends 
RecyclerView.Adapter<FileListAdapter.ViewHolder> {
public static ArrayList<String> filePaths;
private RecyclerViewClickListener clicklistener;
private int rowLayout;
private Context mContext;
private ImageView fileThumbnail, fileEdit, fileRemove;
private TextView filePath, fileSize, uploadPrtg;
public ProgressBar uploadProgs;
private int Progress;
private String ProgPos;

public FileListAdapter(ArrayList<String> filePaths, int rowLayout, Context context) {
    this.filePaths = filePaths;
    this.mContext = context;
    this.rowLayout = rowLayout;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    if (ProgPos != null && ProgPos.equalsIgnoreCase(String.valueOf(position))){
        uploadProgs.setProgress((int) Progress);
        uploadPrtg.setText((int)(Progress)+"%");
    }
    Context context = holder.itemView.getContext();
    File file = new File(filePaths.get(position));
    String path = filePaths.get(position);

    String extension2 = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/"));
    filePath.setText(extension2);

    long length = file.length() / 1024;
    fileSize.setText("Size :- " + length + " KB");




    String extension = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));

    if (extension.equalsIgnoreCase(".mp4")){
        fileEdit.setVisibility(View.VISIBLE);
    }

    if (path != null) {
        Glide.with(context)
                .load(path)
                .into(fileThumbnail);
    } else {
        fileThumbnail.setImageResource(R.drawable.ic_action_photo);
    }
}

public void updateProgress(float percentage, int position){
    ProgPos = String.valueOf(position);
    Progress = (int) percentage;
    filePaths.set(position, String.valueOf(percentage));
    notifyDataSetChanged();
}

@Override
public int getItemCount() {
    return filePaths.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener {

    ViewHolder(View view) {
        super(view);
        fileThumbnail = view.findViewById(R.id.file_thumbnail);
        filePath = view.findViewById(R.id.file_path);
        fileSize = view.findViewById(R.id.file_size);
        fileEdit = view.findViewById(R.id.editMyvid);
        fileRemove = view.findViewById(R.id.removeMyFile);
        uploadProgs = view.findViewById(R.id.progressBar);
        uploadProgs.setMax(100);
        uploadPrtg = view.findViewById(R.id.txtPercentage);

        fileEdit.setVisibility(View.GONE);

        view.setTag(view);
        fileEdit.setOnClickListener(this);
        fileRemove.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (clicklistener != null) {
            clicklistener.onItemClick(view, getAdapterPosition());
        }

    }
}

public void setClickListener(RecyclerViewClickListener itemClickListener) {
    this.clicklistener = itemClickListener;
}

public interface RecyclerViewClickListener {

    void onItemClick(View v, int position);

}

public void removeAt(int position) {
    filePaths.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, filePaths.size());
}

如果有人想要recyclerview适配器代码,我也可以对其进行更新。请帮助我完成它。预先感谢。

1 个答案:

答案 0 :(得分:0)

@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
    long _bytesCurrent = bytesCurrent;
    long _bytesTotal = bytesTotal;

    float percentage = (int) ((float)_bytesCurrent /(float)_bytesTotal * 100);
    Log.d("percentage","" + percentage);
   adapter.updateProgress(percentage, position);// pass adapter list item position here 
}

更换适配器 代替ArrayList<String> filePaths使用ArrayList<FilesClass> filePaths并相应地更新适配器

FilesClass.java

public class FilesClass {
    String FileName;
    Float Percentage;

    public FilesClass(String fileName, Float percentage) {
        FileName = fileName;
        Percentage = percentage;
    }

    public FilesClass() {
    }

    public String getFileName() {
        return FileName;
    }

    public void setFileName(String fileName) {
        FileName = fileName;
    }

    public Float getPercentage() {
        return Percentage;
    }

    public void setPercentage(Float percentage) {
        Percentage = percentage;
    }
}

,然后添加文件路径和默认进度0。使用此自定义模型后,所有文件和进度都将管理到同一索引 然后,

public void updateProgress(float percentage, int position){
    ProgPos = String.valueOf(position);
    Progress = (int) percentage;
    filePaths.get(position).setPercentage(percentage);
    notifyItemChanged(position);
}