From 700a79433d55e41b500e5ea63464a5f4ba0687f9 Mon Sep 17 00:00:00 2001
From: jingbin <770413277@qq.com>
Date: Fri, 31 Jul 2020 11:47:23 +0800
Subject: [PATCH] =?UTF-8?q?add=20=E4=B8=8EToolBar=E8=81=94=E5=8A=A8?=
=?UTF-8?q?=EF=BC=8C=E5=8F=AF=E8=AE=BE=E7=BD=AE=E8=87=AA=E5=AE=9A=E4=B9=89?=
=?UTF-8?q?WebView?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../main/java/me/jingbin/web/ByWebTools.java | 5 +
.../main/java/me/jingbin/web/ByWebView.java | 59 ++--
ByWebView/src/main/res/values/ids.xml | 3 -
app/src/main/AndroidManifest.xml | 14 +-
.../jingbin/webviewstudy/MainActivity.java | 6 +
.../{ => ui}/ByWebViewActivity.java | 5 +-
.../ui/CoordinatorWebActivity.java | 312 ++++++++++++++++++
.../{ => ui}/DeepLinkActivity.java | 4 +-
.../{ => ui}/WebViewActivity.java | 6 +-
.../view/NestedScrollWebView.java | 167 ++++++++++
.../main/res/layout/activity_by_webview.xml | 2 +-
.../res/layout/activity_coordinator_web.xml | 38 +++
.../main/res/layout/activity_deep_link.xml | 2 +-
app/src/main/res/layout/activity_main.xml | 16 +-
app/src/main/res/layout/activity_web_view.xml | 2 +-
app/src/main/res/values-en/strings.xml | 1 +
app/src/main/res/values/strings.xml | 1 +
17 files changed, 604 insertions(+), 39 deletions(-)
delete mode 100755 ByWebView/src/main/res/values/ids.xml
rename app/src/main/java/com/example/jingbin/webviewstudy/{ => ui}/ByWebViewActivity.java (98%)
create mode 100644 app/src/main/java/com/example/jingbin/webviewstudy/ui/CoordinatorWebActivity.java
rename app/src/main/java/com/example/jingbin/webviewstudy/{ => ui}/DeepLinkActivity.java (93%)
rename app/src/main/java/com/example/jingbin/webviewstudy/{ => ui}/WebViewActivity.java (99%)
create mode 100644 app/src/main/java/com/example/jingbin/webviewstudy/view/NestedScrollWebView.java
create mode 100644 app/src/main/res/layout/activity_coordinator_web.xml
diff --git a/ByWebView/src/main/java/me/jingbin/web/ByWebTools.java b/ByWebView/src/main/java/me/jingbin/web/ByWebTools.java
index 7f07975..bc0b3db 100644
--- a/ByWebView/src/main/java/me/jingbin/web/ByWebTools.java
+++ b/ByWebView/src/main/java/me/jingbin/web/ByWebTools.java
@@ -136,6 +136,11 @@ static boolean isNetworkConnected(Context context) {
}
}
+ static int dip2px(Context context, float dpValue) {
+ final float scale = context.getResources().getDisplayMetrics().density;
+ return (int) (dpValue * scale + 0.5f);
+ }
+
public static String getUrl(String url) {
String urlResult = "";
if (TextUtils.isEmpty(url)) {
diff --git a/ByWebView/src/main/java/me/jingbin/web/ByWebView.java b/ByWebView/src/main/java/me/jingbin/web/ByWebView.java
index b5ee901..e96d730 100644
--- a/ByWebView/src/main/java/me/jingbin/web/ByWebView.java
+++ b/ByWebView/src/main/java/me/jingbin/web/ByWebView.java
@@ -14,6 +14,7 @@
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
+import android.widget.FrameLayout;
import android.widget.RelativeLayout;
/**
@@ -48,14 +49,17 @@ private ByWebView(Builder builder) {
this.mErrorTitle = builder.mErrorTitle;
this.mErrorLayoutId = builder.mErrorLayoutId;
- RelativeLayout relativeLayout = new RelativeLayout(activity);
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
+ FrameLayout parentLayout = new FrameLayout(activity);
// 设置WebView
- setWebView(builder.mCustomWebViewId);
- relativeLayout.addView(mWebView, layoutParams);
+ setWebView(builder.mCustomWebView);
+ parentLayout.addView(mWebView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// 进度条布局
- handleWebProgress(builder, relativeLayout);
- builder.mWebContainer.addView(relativeLayout, builder.mLayoutParams);
+ handleWebProgress(builder, parentLayout);
+ if (builder.mIndex != -1) {
+ builder.mWebContainer.addView(parentLayout, builder.mIndex, builder.mLayoutParams);
+ } else {
+ builder.mWebContainer.addView(parentLayout, builder.mLayoutParams);
+ }
// 配置
handleSetting();
// 视频、照片、进度条
@@ -74,13 +78,9 @@ private ByWebView(Builder builder) {
/**
* 配置自定义的WebView
*/
- private void setWebView(int mCustomWebViewId) {
- if (mCustomWebViewId != 0) {
- try {
- mWebView = LayoutInflater.from(activity).inflate(mCustomWebViewId, null).findViewById(R.id.by_custom_webview);
- } catch (Exception e) {
- throw new IllegalStateException("Sorry, ByWebView setWebView() is Error!");
- }
+ private void setWebView(WebView mCustomWebView) {
+ if (mCustomWebView != null) {
+ mWebView = mCustomWebView;
} else {
mWebView = new WebView(activity);
}
@@ -147,7 +147,7 @@ public void setTextZoom(int textZoom) {
mWebView.getSettings().setTextZoom(textZoom);
}
- private void handleWebProgress(Builder builder, RelativeLayout relativeLayout) {
+ private void handleWebProgress(Builder builder, FrameLayout parentLayout) {
if (builder.mUseWebProgress) {
mProgressBar = new WebProgress(activity);
if (builder.mProgressStartColor != 0 && builder.mProgressEndColor != 0) {
@@ -161,11 +161,13 @@ private void handleWebProgress(Builder builder, RelativeLayout relativeLayout) {
&& TextUtils.isEmpty(builder.mProgressEndColorString)) {
mProgressBar.setColor(builder.mProgressStartColorString, builder.mProgressStartColorString);
}
+ int progressHeight = ByWebTools.dip2px(parentLayout.getContext(), WebProgress.WEB_PROGRESS_DEFAULT_HEIGHT);
if (builder.mProgressHeightDp != 0) {
mProgressBar.setHeight(builder.mProgressHeightDp);
+ progressHeight = ByWebTools.dip2px(parentLayout.getContext(), builder.mProgressHeightDp);
}
mProgressBar.setVisibility(View.GONE);
- relativeLayout.addView(mProgressBar);
+ parentLayout.addView(mProgressBar, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, progressHeight));
}
}
@@ -262,7 +264,7 @@ public WebProgress getProgressBar() {
public void showErrorView() {
try {
if (mErrorView == null) {
- RelativeLayout parent = (RelativeLayout) mWebView.getParent();
+ FrameLayout parent = (FrameLayout) mWebView.getParent();
mErrorView = LayoutInflater.from(parent.getContext()).inflate((mErrorLayoutId == 0) ? R.layout.by_load_url_error : mErrorLayoutId, null);
mErrorView.setOnClickListener(new View.OnClickListener() {
@Override
@@ -270,7 +272,7 @@ public void onClick(View v) {
reload();
}
});
- parent.addView(mErrorView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+ parent.addView(mErrorView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} else {
mErrorView.setVisibility(View.VISIBLE);
}
@@ -319,8 +321,9 @@ public static class Builder {
// 进度条 高度
private int mProgressHeightDp;
private int mErrorLayoutId;
- private int mCustomWebViewId;
+ private int mIndex = -1;
private String mErrorTitle;
+ private WebView mCustomWebView;
private String mInterfaceName;
private Object mInterfaceObj;
private ViewGroup mWebContainer;
@@ -346,6 +349,20 @@ public Builder setWebParent(@NonNull ViewGroup webContainer, ViewGroup.LayoutPar
return this;
}
+ /**
+ * WebView容器
+ *
+ * @param webContainer 外部WebView容器
+ * @param index 加入的位置
+ * @param layoutParams 对应的LayoutParams
+ */
+ public Builder setWebParent(@NonNull ViewGroup webContainer, int index, ViewGroup.LayoutParams layoutParams) {
+ this.mWebContainer = webContainer;
+ this.mIndex = index;
+ this.mLayoutParams = layoutParams;
+ return this;
+ }
+
/**
* @param isUse 是否使用进度条,默认true
*/
@@ -394,10 +411,10 @@ public Builder useWebProgress(String startColor, String endColor, int heightDp)
}
/**
- * @param customWebViewId 三方WebView,注意一定要使用id = by_custom_webview
+ * @param customWebView 自定义的WebView
*/
- public Builder setCustomWebViewLayout(@LayoutRes int customWebViewId) {
- mCustomWebViewId = customWebViewId;
+ public Builder setCustomWebView(WebView customWebView) {
+ mCustomWebView = customWebView;
return this;
}
diff --git a/ByWebView/src/main/res/values/ids.xml b/ByWebView/src/main/res/values/ids.xml
deleted file mode 100755
index 6e9434a..0000000
--- a/ByWebView/src/main/res/values/ids.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-
-
-
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index db1155a..da820fd 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -48,7 +48,7 @@
-
+
@@ -87,7 +87,7 @@
android:required="false" />
-
+
\ No newline at end of file
diff --git a/app/src/main/java/com/example/jingbin/webviewstudy/MainActivity.java b/app/src/main/java/com/example/jingbin/webviewstudy/MainActivity.java
index d42dcdd..2f3cb7b 100644
--- a/app/src/main/java/com/example/jingbin/webviewstudy/MainActivity.java
+++ b/app/src/main/java/com/example/jingbin/webviewstudy/MainActivity.java
@@ -19,6 +19,8 @@
import android.widget.Toast;
import com.example.jingbin.webviewstudy.tencentx5.X5WebViewActivity;
+import com.example.jingbin.webviewstudy.ui.ByWebViewActivity;
+import com.example.jingbin.webviewstudy.ui.CoordinatorWebActivity;
import com.example.jingbin.webviewstudy.utils.StatusBarUtil;
import me.jingbin.web.ByWebTools;
@@ -53,6 +55,7 @@ private void initView() {
findViewById(R.id.bt_upload_photo).setOnClickListener(this);
findViewById(R.id.bt_call).setOnClickListener(this);
findViewById(R.id.bt_java_js).setOnClickListener(this);
+ findViewById(R.id.bt_toolbar).setOnClickListener(this);
rbSystem = findViewById(R.id.rb_system);
etSearch = findViewById(R.id.et_search);
@@ -108,6 +111,9 @@ public void onClick(View v) {
String deepLinkUrl = "file:///android_asset/deeplink.html";
loadUrl(deepLinkUrl, getString(R.string.deeplink));
break;
+ case R.id.bt_toolbar:// 与ToolBar联动,自定义WebView
+ CoordinatorWebActivity.loadUrl(this, "http://www.baidu.com", "百度一下", 0);
+ break;
case R.id.tv_version:
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("感谢");
diff --git a/app/src/main/java/com/example/jingbin/webviewstudy/ByWebViewActivity.java b/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java
similarity index 98%
rename from app/src/main/java/com/example/jingbin/webviewstudy/ByWebViewActivity.java
rename to app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java
index ca120c8..bab9512 100644
--- a/app/src/main/java/com/example/jingbin/webviewstudy/ByWebViewActivity.java
+++ b/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java
@@ -1,4 +1,4 @@
-package com.example.jingbin.webviewstudy;
+package com.example.jingbin.webviewstudy.ui;
import android.content.Context;
import android.content.Intent;
@@ -17,6 +17,8 @@
import android.widget.TextView;
import android.widget.Toast;
+import com.example.jingbin.webviewstudy.MainActivity;
+import com.example.jingbin.webviewstudy.R;
import com.example.jingbin.webviewstudy.config.MyJavascriptInterface;
import com.example.jingbin.webviewstudy.utils.StatusBarUtil;
import com.example.jingbin.webviewstudy.utils.WebTools;
@@ -74,7 +76,6 @@ private void initTitle() {
LinearLayout container = findViewById(R.id.ll_container);
byWebView = ByWebView
.with(this)
- .setCustomWebViewLayout(R.layout.layout_custom_webview)
.setWebParent(container, new LinearLayout.LayoutParams(-1, -1))
.useWebProgress(ContextCompat.getColor(this, R.color.coloRed))
.setOnTitleProgressCallback(onTitleProgressCallback)
diff --git a/app/src/main/java/com/example/jingbin/webviewstudy/ui/CoordinatorWebActivity.java b/app/src/main/java/com/example/jingbin/webviewstudy/ui/CoordinatorWebActivity.java
new file mode 100644
index 0000000..fd1492d
--- /dev/null
+++ b/app/src/main/java/com/example/jingbin/webviewstudy/ui/CoordinatorWebActivity.java
@@ -0,0 +1,312 @@
+package com.example.jingbin.webviewstudy.ui;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+import android.support.design.widget.AppBarLayout;
+import android.support.design.widget.CoordinatorLayout;
+import android.support.v4.content.ContextCompat;
+import android.support.v7.app.ActionBar;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.util.Log;
+import android.view.KeyEvent;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.webkit.WebView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.example.jingbin.webviewstudy.MainActivity;
+import com.example.jingbin.webviewstudy.R;
+import com.example.jingbin.webviewstudy.config.MyJavascriptInterface;
+import com.example.jingbin.webviewstudy.utils.StatusBarUtil;
+import com.example.jingbin.webviewstudy.utils.WebTools;
+import com.example.jingbin.webviewstudy.view.NestedScrollWebView;
+
+import me.jingbin.web.ByWebTools;
+import me.jingbin.web.ByWebView;
+import me.jingbin.web.OnByWebClientCallback;
+import me.jingbin.web.OnTitleProgressCallback;
+
+/**
+ * @author jingbin
+ * 与ToolBar联动的WebView,自定义WebView
+ * link to https://github.com/youlookwhat/ByWebView
+ */
+public class CoordinatorWebActivity extends AppCompatActivity {
+
+ // 网页链接
+ private int mState;
+ private String mUrl;
+ private String mTitle;
+ private WebView webView;
+ private ByWebView byWebView;
+ private TextView tvGunTitle;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_coordinator_web);
+ getIntentData();
+ initTitle();
+ getDataFromBrowser(getIntent());
+ }
+
+ private void getIntentData() {
+ mUrl = getIntent().getStringExtra("url");
+ mTitle = getIntent().getStringExtra("title");
+ mState = getIntent().getIntExtra("state", 0);
+ }
+
+ private void initTitle() {
+ StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.colorPrimary), 0);
+ initToolBar();
+ CoordinatorLayout container = findViewById(R.id.coordinatorLayout);
+ CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(-1, -1);
+ // 设置behavior属性 和 ToolBar属性里设置 layout_scrollFlags="scroll|enterAlways"
+ lp.setBehavior(new AppBarLayout.ScrollingViewBehavior());
+
+ NestedScrollWebView nestedScrollWebView = new NestedScrollWebView(this);
+
+ byWebView = ByWebView
+ .with(this)
+ .setWebParent(container, 1, lp)
+ .setCustomWebView(nestedScrollWebView)// 设置自定义WebView
+ .useWebProgress(ContextCompat.getColor(this, R.color.coloRed))
+ .setOnTitleProgressCallback(onTitleProgressCallback)
+ .setOnByWebClientCallback(onByWebClientCallback)
+ .addJavascriptInterface("injectedObject", new MyJavascriptInterface(this))
+ .loadUrl(mUrl);
+ webView = byWebView.getWebView();
+ }
+
+ private void initToolBar() {
+ // 可滚动的title 使用简单 没有渐变效果,文字两旁有阴影
+ Toolbar mTitleToolBar = findViewById(R.id.title_tool_bar);
+ tvGunTitle = findViewById(R.id.tv_gun_title);
+ setSupportActionBar(mTitleToolBar);
+ ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ //去除默认Title显示
+ actionBar.setDisplayShowTitleEnabled(false);
+ }
+ mTitleToolBar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.actionbar_more));
+ tvGunTitle.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ tvGunTitle.setSelected(true);
+ }
+ }, 1900);
+ tvGunTitle.setText(mTitle);
+ }
+
+ private OnTitleProgressCallback onTitleProgressCallback = new OnTitleProgressCallback() {
+ @Override
+ public void onReceivedTitle(String title) {
+ Log.e("---title", title);
+ tvGunTitle.setText(title);
+ }
+ };
+
+ private OnByWebClientCallback onByWebClientCallback = new OnByWebClientCallback() {
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ // 网页加载完成后的回调
+ if (mState == 1) {
+ loadImageClickJs();
+ loadTextClickJs();
+ loadWebsiteSourceCodeJs();
+ } else if (mState == 2) {
+ loadCallJs();
+ }
+ }
+
+ @Override
+ public boolean isOpenThirdApp(String url) {
+ // 处理三方链接
+ Log.e("---url", url);
+ return ByWebTools.handleThirdApp(CoordinatorWebActivity.this, url);
+ }
+ };
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.menu_webview, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case android.R.id.home:// 返回键
+ handleFinish();
+ break;
+ case R.id.actionbar_share:// 分享到
+ String shareText = webView.getTitle() + webView.getUrl();
+ WebTools.share(CoordinatorWebActivity.this, shareText);
+ break;
+ case R.id.actionbar_cope:// 复制链接
+ WebTools.copy(webView.getUrl());
+ Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
+ break;
+ case R.id.actionbar_open:// 打开链接
+ WebTools.openLink(CoordinatorWebActivity.this, webView.getUrl());
+ break;
+ case R.id.actionbar_webview_refresh:// 刷新页面
+ byWebView.reload();
+ break;
+ default:
+ break;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+
+ /**
+ * 前端注入JS:
+ * 这段js函数的功能就是,遍历所有的img节点,并添加onclick函数,函数的功能是在图片点击的时候调用本地java接口并传递url过去
+ */
+ private void loadImageClickJs() {
+ byWebView.getLoadJsHolder().loadJs("javascript:(function(){" +
+ "var objs = document.getElementsByTagName(\"img\");" +
+ "for(var i=0;i节点,将节点里的属性传递过去(属性自定义,用于页面跳转)
+ */
+ private void loadTextClickJs() {
+ byWebView.getLoadJsHolder().loadJs("javascript:(function(){" +
+ "var objs =document.getElementsByTagName(\"li\");" +
+ "for(var i=0;i
+ tools:context="com.example.jingbin.webviewstudy.ui.ByWebViewActivity">
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_deep_link.xml b/app/src/main/res/layout/activity_deep_link.xml
index beba45d..50d418e 100644
--- a/app/src/main/res/layout/activity_deep_link.xml
+++ b/app/src/main/res/layout/activity_deep_link.xml
@@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
- tools:context="com.example.jingbin.webviewstudy.DeepLinkActivity">
+ tools:context="com.example.jingbin.webviewstudy.ui.DeepLinkActivity">
+ android:layout_marginTop="10dp"
+ android:focusable="true"
+ android:focusableInTouchMode="true">
+
+
+
+
+ tools:context="com.example.jingbin.webviewstudy.ui.WebViewActivity">
Call, SMS, email, js injection
Js intermediates with android native code
DeepLink test
+ setCustomView
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index fc6e2c3..ed9267b 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -19,4 +19,5 @@
电话、短信、邮件、注入js
js与android原生代码互调
DeepLink 测试
+ 与ToolBar联动,自定义WebView