gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:52:28 25 4
gpt4 key购买 nike

我想将文件列表从我的应用程序上传到亚马逊 s3 存储桶。我可以获取上传进度,但无法在回收器 View 项中设置上传进度值。 (我想在将文件共享到另一台设备时像 Shareit 应用程序一样显示文件共享进度。)

我已经实现了recyclerview并且工作正常。我在每个要上传的文件上都有一个进度条和 Textview。我只想在进度条和每个项目的 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 适配器代码,我也可以更新它。请帮助我完成它。提前致谢。

最佳答案

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

关于java - 如何更新 recyclerview 项目中的上传进度百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com