下载文件时,我不会更新已下载文件的百分比,也不会在下载完成后对其进行更新...
使用的代码
URL url = new URL(link);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
String saveFilePath = "/sdcard/" + tag + ".apk";
OutputStream output = new FileOutputStream(UserSetting.FILEROOT);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
holder.click.setText((int) ((total * 100) / lenghtOfFile)+"");
System.out.println((int) ((total * 100) / lenghtOfFile)+"");
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
holder.click.setText("نصب");
} catch (Exception e) {
}
答案 0 :(得分:0)
第一件事把百分比参数放到您的持有人中
class MyHolder extends RecyclerView.ViewHolder{
//....
int percentage = 0;
//...
}
//then assign the value to it in when downloading progress changed
holder.percentage (int) ((total * 100) / lenghtOfFile);
// after that set the percentage as click button text
holder.click.setText(String.valueOf(percentage));
// and do the same thing in the beginning of onBind method
// thus the percentage still updated and won't be lost on scrolling
注意:您的代码会给您带来问题,每次您滚动并查看显示的文件
最简单的修复方法是尝试缓存文件,这样您就不必每次都下载相同的数据