Skip to content

Commit

Permalink
add 与ToolBar联动,可设置自定义WebView
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jul 31, 2020
1 parent ab099ff commit 700a794
Show file tree
Hide file tree
Showing 17 changed files with 604 additions and 39 deletions.
5 changes: 5 additions & 0 deletions ByWebView/src/main/java/me/jingbin/web/ByWebTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
59 changes: 38 additions & 21 deletions ByWebView/src/main/java/me/jingbin/web/ByWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

/**
Expand Down Expand Up @@ -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();
// 视频、照片、进度条
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -262,15 +264,15 @@ 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
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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 0 additions & 3 deletions ByWebView/src/main/res/values/ids.xml

This file was deleted.

14 changes: 10 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</intent-filter>
</activity>
<activity
android:name=".WebViewActivity"
android:name=".ui.WebViewActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:launchMode="singleTask"
Expand All @@ -58,7 +58,7 @@


<!--用于DeepLink,html跳到此页面 scheme_Adr: 'will://link/testid',-->
<activity android:name=".DeepLinkActivity">
<activity android:name=".ui.DeepLinkActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

Expand Down Expand Up @@ -87,7 +87,7 @@
android:required="false" />

<activity
android:name=".ByWebViewActivity"
android:name=".ui.ByWebViewActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:launchMode="singleTask"
Expand All @@ -106,7 +106,13 @@
</intent-filter>

</activity>

<activity
android:name=".ui.CoordinatorWebActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:theme="@style/WebViewTheme"
tools:ignore="LockedOrientationActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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("感谢");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.jingbin.webviewstudy;
package com.example.jingbin.webviewstudy.ui;

import android.content.Context;
import android.content.Intent;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 700a794

Please sign in to comment.