Skip to content

Commit df16781

Browse files
committed
新增WebFragment,优化SplashActivity
1 parent e554e6e commit df16781

File tree

7 files changed

+318
-9
lines changed

7 files changed

+318
-9
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,34 @@ Base是针对于Android开发封装好一些常用的基类,主要包括通用
1414
<dependency>
1515
<groupId>com.king.base</groupId>
1616
<artifactId>base</artifactId>
17-
<version>1.2</version>
17+
<version>1.3</version>
1818
<type>pom</type>
1919
</dependency>
2020
```
2121
###Gradle:
2222
```gradle
23-
compile 'com.king.base:base:1.2'
23+
compile 'com.king.base:base:1.3'
2424
```
2525
###Lvy:
2626
```lvy
27-
<dependency org='com.king.base' name='base' rev='1.2'>
27+
<dependency org='com.king.base' name='base' rev='1.3'>
2828
<artifact name='$AID' ext='pom'></artifact>
2929
</dependency>
3030
```
3131

3232
###引入的库:
3333
```gradle
34-
compile 'com.android.support:recyclerview-v7:24.0.0'
34+
compile 'com.android.support:recyclerview-v7:24.0.+'
3535
```
3636

3737
```gradle
3838
compile 'org.greenrobot:eventbus:3.0.0'
3939
```
4040

41+
```gradle
42+
compile 'com.android.support:appcompat-v7:24.0.+'
43+
```
44+
4145
##简要说明:
4246
Base主要实用地方体现在:出统一的代码风格,实用的各种基类,BaseActivity和BaseFragment里面还有许多实用的代码封装,只要用了Base,使用Fragment就感觉跟使用Activtiy基本是一样的。
4347

@@ -232,6 +236,11 @@ public class TestDialogFragment extends BaseDialogFragment {
232236
}
233237
}
234238
```
239+
240+
###WebFragment
241+
```Java
242+
WebFragment实现基本webView功能
243+
```
235244
###其他小功能
236245

237246
使用Log:

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ android {
2828
dependencies {
2929
compile fileTree(include: ['*.jar'], dir: 'libs')
3030
testCompile 'junit:junit:4.12'
31-
compile 'com.android.support:appcompat-v7:24.0.0'
32-
compile 'de.greenrobot:eventbus:3.0.0-beta1'
33-
compile 'com.android.support:recyclerview-v7:24.0.0'
31+
provided 'com.android.support:appcompat-v7:24.0.+'
32+
provided 'com.android.support:recyclerview-v7:24.0.+'
33+
provided 'de.greenrobot:eventbus:3.0.0'
3434

3535
}
3636

@@ -39,7 +39,7 @@ publish {
3939
userOrg = 'jenly'//bintray.com用户名
4040
groupId = 'com.king.base'//jcenter上的路径
4141
artifactId = 'base'//项目名称
42-
publishVersion = '1.2'//版本号
42+
publishVersion = '1.3'//版本号
4343
desc = 'Base for Android'//描述
4444
website = 'https://github.com/jenly1314/Base'//网站
4545
}

src/main/java/com/king/base/BaseActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.king.base;
1818

19+
import android.app.Activity;
1920
import android.app.Dialog;
2021
import android.support.annotation.IdRes;
2122
import android.support.annotation.LayoutRes;
@@ -38,6 +39,7 @@
3839
import android.view.animation.AnimationUtils;
3940
import android.view.inputmethod.InputMethodManager;
4041
import android.widget.EditText;
42+
import android.widget.FrameLayout;
4143
import android.widget.ProgressBar;
4244
import android.widget.TextView;
4345
import android.widget.Toast;
@@ -99,6 +101,12 @@ protected void onStop() {
99101
dismissProgressDialog();
100102
}
101103

104+
public static View getContentView(Activity activity){
105+
ViewGroup view = (ViewGroup)activity.getWindow().getDecorView();
106+
FrameLayout content = (FrameLayout)view.findViewById(android.R.id.content);
107+
return content.getChildAt(0);
108+
}
109+
102110

103111
protected View inflate(@LayoutRes int id){
104112
return inflate(id,null);

src/main/java/com/king/base/SplashActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public abstract class SplashActivity extends BaseActivity {
3333

3434
public abstract Animation.AnimationListener getAnimationListener();
3535

36+
public View getRootView(){
37+
return getContentView(this);
38+
}
39+
3640
protected Animation getAnimation(){
3741
return AnimationUtils.loadAnimation(context, R.anim.splash_alpha);
3842
}
@@ -48,9 +52,10 @@ public void initUI() {
4852
setContentView(getContentViewId());
4953
}
5054

55+
5156
@Override
5257
public void initData() {
53-
startAnimation(getWindow().getDecorView().getRootView());
58+
startAnimation(getRootView());
5459
}
5560

5661
@Override
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
package com.king.base.fragment;
2+
3+
import android.graphics.Bitmap;
4+
import android.net.http.SslError;
5+
import android.os.Bundle;
6+
import android.text.TextUtils;
7+
import android.view.KeyEvent;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.webkit.SslErrorHandler;
11+
import android.webkit.WebChromeClient;
12+
import android.webkit.WebResourceError;
13+
import android.webkit.WebResourceRequest;
14+
import android.webkit.WebSettings;
15+
import android.webkit.WebView;
16+
import android.webkit.WebViewClient;
17+
import android.widget.LinearLayout;
18+
import android.widget.ProgressBar;
19+
20+
import com.king.base.BaseFragment;
21+
import com.king.base.R;
22+
import com.king.base.model.EventMessage;
23+
import com.king.base.util.LogUtils;
24+
import com.king.base.util.SystemUtils;
25+
26+
/**
27+
* @author Jenly <a href="mailto:[email protected]">Jenly</a>
28+
*/
29+
public class WebFragment extends BaseFragment{
30+
31+
public static final int OPR_WEBVIEW_BACK = 0x01;
32+
33+
protected ProgressBar progressBar;
34+
35+
protected LinearLayout vError;
36+
37+
protected WebView webView;
38+
39+
protected String url;
40+
41+
protected boolean isError;
42+
43+
private boolean isShowError;
44+
45+
public static WebFragment newInstance(String url) {
46+
47+
Bundle args = new Bundle();
48+
args.putString(KEY_URL,url);
49+
WebFragment fragment = new WebFragment();
50+
fragment.url = url;
51+
fragment.setArguments(args);
52+
return fragment;
53+
}
54+
55+
@Override
56+
public int inflaterRootView() {
57+
return R.layout.fragment_web;
58+
}
59+
60+
@Override
61+
public void initUI() {
62+
rootView.setFocusable(true);
63+
rootView.setFocusableInTouchMode(true);
64+
65+
progressBar = findView(R.id.progressBar);
66+
progressBar.setMax(100);
67+
webView = findView(R.id.webView);
68+
vError = findView(R.id.vError);
69+
70+
isShowError = addErrorView(vError);
71+
}
72+
73+
@Override
74+
public void addListeners() {
75+
76+
rootView.setOnKeyListener(new View.OnKeyListener() {
77+
@Override
78+
public boolean onKey(View view, int i, KeyEvent keyEvent) {
79+
80+
if(keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK){
81+
82+
if(isGoBack()){
83+
webView.goBack();
84+
}else{
85+
finish();
86+
}
87+
return true;
88+
}
89+
90+
return false;
91+
}
92+
});
93+
}
94+
95+
@Override
96+
public void initData() {
97+
WebSettings ws = webView.getSettings();
98+
//是否允许脚本支持
99+
ws.setJavaScriptEnabled(true);
100+
101+
102+
ws.setJavaScriptCanOpenWindowsAutomatically(true);
103+
104+
ws.setSaveFormData(false);
105+
// ws.setAppCacheEnabled(false);
106+
ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
107+
108+
109+
webView.setHorizontalScrollBarEnabled(false);
110+
111+
// webView.addJavascriptInterface(new WebJavascriptInterface(),"android");
112+
113+
String str = getIntent().getStringExtra(KEY_URL);
114+
if(!TextUtils.isEmpty(str)){
115+
this.url = str;
116+
}
117+
118+
webView.setWebChromeClient(new WebChromeClient(){
119+
@Override
120+
public void onProgressChanged(WebView view, int newProgress) {
121+
super.onProgressChanged(view, newProgress);
122+
updateProgressBar(newProgress);
123+
}
124+
});
125+
webView.setWebViewClient(getWebViewClient());
126+
127+
load(webView,url);
128+
}
129+
130+
131+
public WebViewClient getWebViewClient(){
132+
return new WebViewClient(){
133+
134+
@Override
135+
public void onPageStarted(WebView view, String url, Bitmap favicon) {
136+
super.onPageStarted(view, url, favicon);
137+
LogUtils.d("startUrl:" + url);
138+
isError = false;
139+
}
140+
141+
@Override
142+
public boolean shouldOverrideUrlLoading(WebView view, String url) {
143+
LogUtils.d("url:" + url);
144+
145+
return super.shouldOverrideUrlLoading(view,url);
146+
147+
}
148+
149+
150+
@Override
151+
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
152+
super.onReceivedError(view, request, error);
153+
updateProgressBar(100);
154+
}
155+
156+
157+
@Override
158+
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
159+
super.onReceivedError(view, errorCode, description, failingUrl);
160+
LogUtils.w("errorCode:" + errorCode + "|failingUrl:" + failingUrl);
161+
showReceiveError();
162+
}
163+
164+
165+
@Override
166+
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
167+
super.onReceivedSslError(view, handler, error);
168+
handler.cancel();
169+
handler.proceed();
170+
}
171+
172+
@Override
173+
public void onPageFinished(WebView view, String url) {
174+
super.onPageFinished(view, url);
175+
hideReceiveError();
176+
}
177+
};
178+
}
179+
180+
/**
181+
*
182+
* @param group
183+
* @return true表示已添加ErrorView并显示ErrorView/false表示不处理
184+
*/
185+
public boolean addErrorView(ViewGroup group){
186+
187+
return false;
188+
}
189+
190+
private void showReceiveError(){
191+
isError = true;
192+
if(SystemUtils.isNetWorkActive(context)){
193+
LogUtils.w("Page loading failed.");
194+
}else{
195+
LogUtils.w("Network unavailable.");
196+
}
197+
198+
if(isShowError){
199+
vError.setVisibility(View.VISIBLE);
200+
}
201+
202+
203+
}
204+
205+
private void hideReceiveError(){
206+
if(isError){
207+
showReceiveError();
208+
}else{
209+
vError.setVisibility(View.GONE);
210+
}
211+
212+
}
213+
214+
/**
215+
* 加载url
216+
* @param webView
217+
* @param url
218+
*/
219+
private void load(WebView webView,String url){
220+
LogUtils.d("url:" + url);
221+
if(!TextUtils.isEmpty(url)){
222+
webView.loadUrl(url);
223+
}
224+
225+
}
226+
227+
private boolean isGoBack(){
228+
return webView!=null && webView.canGoBack();
229+
}
230+
231+
232+
private void updateProgressBar(int progress){
233+
updateProgressBar(true,progress);
234+
}
235+
236+
237+
private void updateProgressBar(boolean isVisibility,int progress){
238+
239+
progressBar.setVisibility((isVisibility && progress<100) ? View.VISIBLE : View.GONE);
240+
progressBar.setProgress(progress);
241+
}
242+
243+
244+
@Override
245+
public void onEventMessage(EventMessage em) {
246+
if(isStop){
247+
return;
248+
}
249+
250+
switch (em.what){
251+
case OPR_WEBVIEW_BACK:
252+
if(isGoBack()){
253+
webView.goBack();
254+
}else{
255+
finish();
256+
}
257+
break;
258+
}
259+
}
260+
261+
}
262+

0 commit comments

Comments
 (0)