2014-09-03 86 views
1

我需要操作html中的链接,所以我使用WebViewClient来完成它。问题是我有一些加载本地html页面的iframe,它们没有显示出来。 但是,如果我使用WebChromeClient他们的作品!使用WebViewClient和WebChromeClient的Android

所以,我想使用它们两个,但它不起作用。似乎只有webViewClient的作品,我不知道为什么。

我在清单中设置Internet权限并写入外部存储(因为我的html位于存储中),并启用了硬件加速。

这是我的代码:

public class BChromeClient : WebChromeClient 
    { 
     public BChromeClient() 
     {} 

     public bool OnShowCustomView(WebView view, string url) 
     { 
      return true; 
     } 

     public void OnHideCustomView() 
     { 
     } 


    } 

    public class BWebClient : WebViewClient 
    { 
     int _position; 
     string _path; 
     Activity _parent; 
     ViewPager _pager; 
     string _chapName; 
     public BWebClient (int position, string Path, Activity Parent, ViewPager Pager, string ChapName){ 
      _position = position; 
      _parent = Parent; 
      _path = Path; 
      _pager = Pager; 
      _chapName = ChapName; 
     } 

     public override void OnPageFinished (WebView view, string url) 
     { 
      base.OnPageFinished (view, url); 
      view.ScrollTo (0, _position); 
     } 

     public override bool ShouldOverrideUrlLoading (WebView view, string url) 
     { 

      if (url.StartsWith ("navigate")) { 
       string destination = url.Substring (url.IndexOf ("navigate://") + "navigate://".Length); 
       int DestinationChapter = Int32.Parse (destination.Substring (0, destination.IndexOf("_"))); 
       int l = destination.IndexOf("_") + 1; 
       int b = destination.Length - l; 
       int DestinationPage = Int32.Parse (destination.Substring (l,b)); 



       if (DestinationPage == 0) { 
        _pager.SetCurrentItem(DestinationChapter ,true); 
        WebView _web = (WebView)_pager.FindViewWithTag(300 + DestinationChapter); 
        _web.LoadUrl ("file://" + _path + "/" + _chapName); 
       } 



      } else if (url.StartsWith ("pdf")) { 
       string file_path = _path + url.Substring (5); 
       if (System.IO.File.Exists(file_path)) { 
        Android.Net.Uri pdfFile = Android.Net.Uri.FromFile (new Java.IO.File (file_path)); 
        Intent pdfIntent = new Intent (Intent.ActionView); 
        pdfIntent.SetDataAndType (pdfFile, "application/pdf"); 
        pdfIntent.SetFlags (ActivityFlags.NoHistory); 
        _parent.StartActivity (pdfIntent); 
       } 
      } 

      return true; 
     } 
    } 

而在片段OnCreateView方法(是的,我使用PageViewer内网页视图,但我不认为知道它是很重要的)。

web_view = view.FindViewById<WebView> (Resource.Id.webview); 
     web_view.SetWebChromeClient (new BChromeClient()); 
     web_view.SetWebViewClient(new BWebClient(_position,_path,_parent, _pager, _chap.Name)); 
     web_view.SetBackgroundColor(Color.Transparent); 
     web_view.Settings.JavaScriptEnabled = true; 
     web_view.Settings.AllowFileAccess = true; 
     //web_view.Settings.SetPluginState (WebSettings.PluginState.On); 

回答

0

在MainActivity.cs

web_view..webviewClient = new ExtendedWebViewClient(); 
web_view.SetWebChromeClient(new ExtendedChromeClient(MainActivity)); 
      web_view.SetWebViewClient(this.webviewClient);