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);