簡體   English   中英

RecyclerView.Adapter不更新按鈕文本

[英]RecyclerView.Adapter dont update button text

下載文件時,我不會更新已下載文件的百分比,也不會在下載完成后對其進行更新...

使用的代碼

 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) {
                        }

首先把百分比參數放到你的持有人

  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

注意:您的代碼會給您帶來問題,每次您滾動並查看顯示時,文件都會重新下載

最簡單的解決方法是嘗試緩存文件,因此您不必每次都下載相同的數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM