android - Update WebView inside ViewPager after AsyncTask -


i have webview inside viewpager. when webview starts load data, needs perform task on urls, ids urls , update view.

my problem is, how update webview inside viewpager latest content? viewpager inside fragment.

here code:

@override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {      view view = inflater.inflate(r.layout.fragment_detail_native,             container, false);       //.... non-relevant code removed      mviewpager = (viewpager) view.findviewbyid(r.id.viewpager);     mcustompageradapter = new custompageradapter(items, activity);     mviewpager.setadapter(mcustompageradapter);     mviewpager.setcurrentitem(position);     mviewpager.setonpagechangelistener(new onpagechangelistener() {          @override         public void onpageselected(int position) {             if (mtwopane)                 monpageselectedlistener.onpageselected(position);         }          @override         public void onpagescrolled(int position, float arg1, int arg2) {         }          @override         public void onpagescrollstatechanged(int arg0) {         }     });      return view; } 

custompageradapter class:

    private class custompageradapter extends pageradapter {          list<item> items = new arraylist<item>();         layoutinflater inflater;         activity activity;         mywebview mwebview = null;          public custompageradapter(list<item> items2, activity activity) {             this.items = items2;             this.activity = activity;              inflater = (layoutinflater) activity                     .getsystemservice(context.layout_inflater_service);         }          @override         public int getcount() {             return items.size();         }          @override         public boolean isviewfromobject(view view, object object) {             return view == object;         }          @suppresslint("setjavascriptenabled")         @suppresswarnings("deprecation")         @override         public object instantiateitem(viewgroup container, int position) {              item item = items.get(position);              view view = inflater.inflate(r.layout.item_detail_screen,                     container, false);             mwebview = (mywebview) view.findviewbyid(r.id.webview);              //... webview settings code removed             //... building html content removed              string content = item.content;              //... other html content removed              html.append(content_prefix);             html.append(content_test);             html.append(content);             html.append(content_postfix);              html.append(htmlpostfix);              string htmlstring = html.tostring();             htmlstring = htmlstring.replace("%", "&#37;");              // @ point htmlstring has html data built.             // data has urls, needs parsed             // videoids , done asynctask              // method calls asynctask             parsevideolinks(htmlstring);              mwebview.loaddatawithbaseurl("", htmlstring, "text/html", "utf-8",                     "");              // ((viewpager) container).addview((view) view, 0);             ((viewpager) container).addview((view) view);              return view;          }          private void parsevideolinks(string htmlstring) {             urltask task = new urltask(htmlstring);             task.execute();         }          @override         public void destroyitem(view collection, int position, object view) {             ((viewpager) collection).removeview((view) view);         }          public class urltask extends asynctask<void, void, string> {              string html = "";              public urltask(string html) {                 this.html = html;             }              @override             protected string doinbackground(void... params) {                  document doc = jsoup.parse(html);                 doc.select("img").attr("style", "width:100%")                         .attr("height", "auto");                 doc.select("iframe").attr("style", "width:100%")                         .attr("height", "auto");                  elements paragragh_elements = doc.select("p");                 (element element : paragragh_elements) {                      string text = element.text();                      if (text != null && !text.equalsignorecase("")) {                          if (text.contains("https://www.youtube.com/")                                 || text.contains("http://www.youtube.com/")                                 || text.contains("https://www.youtu.be/")                                 || text.contains("https://youtu.be/")) {                              string expandedurl = expandurl(text);                             if (expandedurl != null) {                                  string[] spliturl = expandedurl.split("[?&]");                                  (string split : spliturl) {                                      if (split.contains("v=")) {                                         string videoid = split                                                 .replace("v=", "");                                          log.d(const.debug, "" + videoid);                                          element new_element = constructelement(                                                 videoid, text);                                         element.replacewith(new_element);                                      }                                  }                             } else {                                 // embed                              }                          } else {                             // not youtube url                         }                      }                  }                  return doc.html();             }              private element constructelement(string videoid, string url) {                  element outerdivelement = new element(tag.valueof("div"), "");                 outerdivelement.attr("style", "position:relative;");                  element innerdivelement1 = new element(tag.valueof("div"), "");                 element aelement1 = new element(tag.valueof("a"), "");                 aelement1.attr("href", url);                 element thumbimgelement = new element(tag.valueof("img"), "");                 thumbimgelement.attr("src",                         "http://img.youtube.com/vi/" + videoid + "/0.jpg")                         .attr("style", "width:100%;");                  aelement1.appendchild(thumbimgelement);                 innerdivelement1.appendchild(aelement1);                  element innerdivelement2 = new element(tag.valueof("div"), "");                 innerdivelement2.attr("style",                         "position:absolute; left:30px; top:30px;");                 element aelement2 = new element(tag.valueof("a"), "");                 aelement2.attr("href", url);                 element overlayimgelement = new element(tag.valueof("img"), "");                 overlayimgelement.attr("src",                         "file:///android_asset/images/play.png").attr("style",                         "width:130%;");                  aelement2.appendchild(overlayimgelement);                 innerdivelement2.appendchild(aelement2);                  // log.d(const.debug, "http://img.youtube.com/vi/" + videoid                 // + "/0.jpg");                  outerdivelement.appendchild(innerdivelement1);                 outerdivelement.appendchild(innerdivelement2);                  return outerdivelement;             }              @override             protected void onpostexecute(string result) {                 super.onpostexecute(result);                 // @ point, result has html content                // need load webview with.                // how set webview data here..              }              public string expandurl(string shortenedurl) {                 url url;                 string expandedurl = "";                 try {                     url = new url(shortenedurl);                     httpurlconnection httpurlconnection = (httpurlconnection) url                             .openconnection(proxy.no_proxy);                     httpurlconnection.setinstancefollowredirects(false);                     expandedurl = httpurlconnection.getheaderfield("location");                     httpurlconnection.disconnect();                 } catch (exception e) {                     e.printstacktrace();                 }                 return expandedurl;             }          }      } 

how can set webview load data once asynctask done, in onpostexecute()?

i create interface in async task (and maybe split out own class), example 1 below;

public interface htmlvideolinksparsedlistener {     public void onhtmlvideolinksparsed(string newhtml) } 

and activity implement interface (and corresponding method) i.e.

public class youractivity implements htmlvideolinksparsedlistener {   //your activity code here    @override   public void onhtmlvideolinksparsed(string newhtml)   {   } } 

then pass activity adapter , in turn async task , call interface method once html ready pass webview i.e.

public custompageradapter(list<item> items2, activity activity, htmlvideolinksparsedlistener listener) {         this.items = items2;         this.activity = activity;         this.listener = listener;          inflater = (layoutinflater) activity                 .getsystemservice(context.layout_inflater_service);     }      private void parsevideolinks(string htmlstring) {             urltask task = new urltask(htmlstring, listener);             task.execute();      } 

then async task ...

        //constructor         public urltask(string html, htmlvideolinksparsedlistener listener) {             this.html = html;             this.listener = listener;         }              @override             protected void onpostexecute(string result) {                 super.onpostexecute(result);                 listener.onhtmlvideolinksparsed(result);              } 

then in adapter setup method add instance variable html string used in instantiateitem() method , called notifydatasetchanged() on adapter this.

//in actvity...  @override       public void onhtmlvideolinksparsed(string newhtml)       {            mcustompageradapter.sethtmlstring(newhtml);            mcustompageradapter.notifydatasetchanged();       }  //then in adapter  //setter  public sethtmlstring(string newhtml) {      this.htmlstring = newhtml; }  public object instantiateitem(viewgroup container, int position) {       //check html string , use in web view else normal processing via async task        if (htmlstring!=null)       {          //add new html webview here       } else {          //do normal html processing via async task here        } } 

hope helps! :-)


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -