我有一个带有WebView的片段活动,当我点击特定的url时,我想打开或重定向到不同的片段。
注意:如果选择了所需url以外的其他url,则应在相同的当前片段中打开该url。
发布于 2018-10-23 03:19:12
在你给出end视图代码的on-create中添加这个(最好是在oncreate方法的末尾添加)
webView.setWebViewClient(new MyWebViewClient());
然后在oncreate外部进行如下所示的函数调用
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals("your url here")) {
Fragment newFragment = YourNewFrag();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container(usually a frame layout) view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
return true;
}
return false;
}
}
发布于 2018-10-23 03:06:22
您应该覆盖url加载。使用如下内容:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("your_url")) { //you can also use indexOf, equales etc.
// do what's needed
return true;
}
return false;
}
});
https://stackoverflow.com/questions/52935908
复制相似问题