Android Development--------------WebView (ii) WebView slide bottom Top monitor, load progress bar and other settings

Source: Internet
Author: User
<span id="Label3"></p><p><p>WebView Some of the common settings, sliding monitoring, so that the jump page is also displayed in the webview, loading progress, get title and so on</p></p><p><p><span style="font-size:18px"><strong>one, Slide Monitoring</strong></span></p></p><p><p>Sliding monitoring is required on the basis of WebView on the strengthening, because in WebView no direct monitoring of the sliding method, see WebView Source will find that there is a</p></p><p><p><strong><span style="font-size:18px">protected void onscrollchanged (int l, int t, int oldl, int oldt);</span></strong></p></p><p><p>This method. is protected so we can't use it directly, so we write a reinforced webview, using an interface callback.</p></p><p><p>Scrollwebview</p></p><p><p></p></p><pre code_snippet_id="659516" snippet_file_name="blog_20150505_1_5787745" class="java" name="code">Package Com.song.webviewtest;import Android.content.context;import Android.util.attributeset;import Android.util.log;import android.webkit.webview;/** * Title:ScrollWebView.java * Description: * @author Liusong * @versio N V1.0 */public class Scrollwebview extends WebView {public onscrollchangelistener listener;public scrollwebview (Context context, attributeset attrs, int defstyle) {super (context, attrs, defstyle);} Public Scrollwebview (context context, attributeset attrs) {super (context, attrs);} Public Scrollwebview (context Context) {super (context);} @Overrideprotected void onscrollchanged (int l, int t, int oldl, int oldt) {super.onscrollchanged (l, t, oldl, oldt), float W Ebcontent = getcontentheight () * Getscale ();//webview height float webnow = getheight () + getscrolly ();//current webview height log.i (" TAG1 "," webview.getscrolly () ====>> "+ getscrolly ()), if (math.abs (webcontent-webnow) < 1) {//already at the bottom//log.i (" T AG1 "," already at the bottom "); listener.onpageend (l, t, oldl, oldt);} else if (getscrolly () = = 0) {//log.i ("TAG1", "already at the top"), listener.onpagetop (l, t, oldl, oldt);} else {listener.onscrollchanged (l, t, oldl, oldt);} }public void Setonscrollchangelistener (onscrollchangelistener listener) {this.listener = listener;} public interface Onscrollchangelistener {public void onpageend (int l, int t, int oldl, int oldt);p ublic void Onpagetop (int l, int t, int oldl, int oldt);p ublic void onscrollchanged (int l, int t, int oldl, int oldt);}}</pre><p><p></p></p><p><p><br>then, with this enhanced webview, you can Listen.</p></p><pre code_snippet_id="659516" snippet_file_name="blog_20150505_2_7767684" class="java" name="code">Package Com.song.webviewtest;import Java.util.hashmap;import Java.util.map;import android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.webkit.websettings;import Com.song.webviewtest.scrollwebview.onscrollchangelistener;public class Webviewactivity extends Activity {private String URL = "http://192.168.30.199:8080/song/test.html";//execute action public static final int select_image = 0;//open Gallery Public STA Tic Final int open_page = 1;//jump Other specific page public static final int close_or_back = 2;//off or private Scrollwebview mwebview; @Ove rrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.activity_webviw); Getintentdatas (); Initview ();} private void Getintentdatas () {//TODO auto-generated Method stuburl = getintent (). Getstringextra ("url");} private void Initview () {mwebview = (scrollwebview) Findviewbyid (r.id.webview);//or webview settingwebsettings Settings = mwebview.getsettings ();//set support js, See squareThe name of the law knows what it means settings.setjavascriptenabled (true);//mwebview.addjavascriptinterface (new javascriptinterface (handler) , "Android"); mwebview.setonscrollchangelistener (new onscrollchangelistener () {@Overridepublic void onscrollchanged ( int l, int t, int oldl, int oldt) {//sliding} @Overridepublic void onpagetop (int l, int t, int oldl, int Oldt) {//swipe to top} @Overri depublic void onpageend (int l, int t, int oldl, int oldt) {//slide to bottom}});//load page path Mwebview.loadurl (url);}}</pre><p><p><br><strong><span style="font-size:18px">2. Let the Web page always show in WebView</span></strong></p></p><p><p><strong><span style="font-size:18px"></span></strong></p></p><p><p>This requires the use of setwebviewclient this method, in the above code to add this sentence can be</p></p><pre code_snippet_id="659516" snippet_file_name="blog_20150505_3_4863193" class="java" name="code"><pre code_snippet_id="659516" snippet_file_name="blog_20150505_3_4863193" class="java" name="code">Webview.setwebviewclient (new webviewclient () {public boolean shouldoverrideurlloading (webview view, String url) {// Override this method to indicate whether to click on the link in the page or in the current WebView jump, do not jump to the browser side//log.i ("TAG", "url--=====>>>" + url); View.loadurl (url); return true;} @Overridepublic void onpagestarted (WebView view, String url, Bitmap favicon) {bar.setvisibility (view.visible); Progbar.setvisibility (view.visible);//log.i ("TAG", "start");} @Overridepublic void onpagefinished (WebView view, String url) {//log.i ("TAG", "end"); bar.setvisibility (view.gone);});</pre></pre><p><p><br>Rewrite public boolean shouldoverrideurlloading (WebView view, String url), add view.loadurl (url) inside, ok.</p></p><p><p><span style="font-size:14px"><strong>3. Then the load progress bar</strong></span></p></p><p><p>Setwebviewclient and setwebchromeclient need to be used together,</p></p><p><p>First of all to add ProgressBar control in the layout, the concrete style defines</p></p><pre code_snippet_id="659516" snippet_file_name="blog_20150505_4_845886" class="html" name="code"><pre code_snippet_id="659516" snippet_file_name="blog_20150505_4_845886" class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" match_parent " android:orientation= "vertical" > <include layout= "@layout/web_top"/> <progressbar android:id= "@+id/webprogress" style= "? android:attr/progressbarstylehorizontal" android:layout_width= " Fill_parent " android:layout_height=" wrap_content " android:maxheight=" 3dip " android:minheight=" 3dip " /> <com.appsino.bingluo.fycz.ui.web.scrollwebview android:id=" @+id/wv_load " Android:layout_width= "match_parent" android:layout_height= "match_parent"/></pre></pre><p><p>Then you get the control,</p></p><pre code_snippet_id="659516" snippet_file_name="blog_20150505_5_449650" class="java" name="code"><pre code_snippet_id="659516" snippet_file_name="blog_20150505_5_449650" class="java" name="code">Bar = (ProgressBar) Findviewbyid (r.id.webprogress); bar.setmax (100);</pre></pre><p><p>Then it is set up, in Setwebchromeclien there is a specific method to return the page loading progress</p></p><pre code_snippet_id="659516" snippet_file_name="blog_20150505_6_7545157" class="java" name="code"><pre code_snippet_id="659516" snippet_file_name="blog_20150505_6_7545157" class="java" name="code">Webview.setwebchromeclient (new webchromeclient () {public boolean onconsolemessage (consolemessage cm) {return true;} @Overridepublic void onprogresschanged (WebView view, int Newprogress) {bar.setprogress (newprogress);} @Overridepublic void Onreceivedtitle (WebView view, String title) {super.onreceivedtitle (view, title);// Tvtitle.settext (title);}});</pre></pre><p><p><br>Onprogresschanged This method returns, and then setwebviewclient inside the onpagestarted and onpagefinished control the progress bar displayed in the hidden</p></p><p><p><br><span style="font-size:18px"><strong>4. Get the page title</strong></span></p></p><p><p>You can get the title of the page in the webview.setwebchromeclient (new webchromeclient () onreceivedtitle</p></p><p><p> Android Development--------------WebView (ii) WebView slide Bottom Top listening, loading progress bar and other settings </p> </p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.