android - Updating ProgressBar from AsyncTask lagging UI -


i have fragment recyclerview , recyclerview's viewholder holding progressbar show progress of downloading process.

public static class viewholder extends recyclerview.viewholder {     ....     private progressbar progressbar = null;     ....     public viewholder(view itemview) {         super(itemview);          ....         this.progressbar = (progressbar) itemview.findviewbyid(r.id.progessbar);         ....     } } 

i created callback fragment:

public interface callbackitemchanged {     void onitemchanged(final int position); } 

and if called do:

@override public void onitemchanged(final int position) {     this.adapter.notifyitemchanged(position); } 

and in asynctask:

protected void onprogressupdate(integer... progress) {     super.onprogressupdate(progress);      this.entry.setprogress(progress[0]);     this.entry.setprogressstring(progress[0] + "%");      this.callbackitemchanged.onitemchanged(this.entry.getposition()); } 

the progress published ui lagging hell dont know why? onprogressupdate running on ui thread isnt it? think should or wrong?

how can ui smoothly working while updating progressbar?

edit

@override protected string doinbackground(void... params) {     this.inputstream = null;     this.outputstream = null;     this.connection = null;      file file = new file(this.entry.getpath(this.context));     file parent = file.getparentfile();      try {         parent.mkdirs();         file.createnewfile();          this.connection = (httpsurlconnection) new url(this.entry.geturl()).openconnection();         this.connection.connect();          int filelength = this.connection.getcontentlength();          this.inputstream = this.connection.getinputstream();         this.outputstream = new fileoutputstream(file);          byte data[] = new byte[4096];         long progress = 0;         int count;          while ((count = this.inputstream.read(data)) != -1) {             if (this.iscancelled() || this.entry.iscancelled()) {                 this.handleclose();                 this.handledelete();                  return null;             }              if (filelength > 0) {                 this.publishprogress((int) ((progress +=count) * 100 / filelength));             }              this.outputstream.write(data, 0, count);         }     } catch (exception e) {         this.handledelete();          return e.tostring();     } {         this.handleclose();     }      return null; } 

you're publishing progress inside loop, main thread called lot of times.

you delay progress publishing simple thread.sleep():

    while ((count = this.inputstream.read(data)) != -1) {         if (this.iscancelled() || this.entry.iscancelled()) {             this.handleclose();             this.handledelete();              return null;         }          // write data before publishing progress         this.outputstream.write(data, 0, count);          try{             // adjust value. shouldn't small.             thread.sleep(100);         }catch (interruptedexception e){             // nothing can here         }finally {             if (filelength > 0) {                 this.publishprogress((int) ((progress +=count) * 100 / filelength));             }         }     } 

or publish increments of x%:

while ((count = this.inputstream.read(data)) != -1) {     if (this.iscancelled() || this.entry.iscancelled()) {         this.handleclose();         this.handledelete();          return null;     }      // write data before publishing progress     this.outputstream.write(data, 0, count);      if (filelength > 0) {         currentprogress = ((progress += count) * 100 / filelength);         // publish on increments of 1%         if (currentprogress >= previousprogress + 1) {             this.publishprogress(currentprogress);             previousprogress = currentprogress;         }      } } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -