示例 这是运行的效果
1.列表使用的是RecyclerView,实现滑动3s之后再缓存网页HTML,每个Item对应于一个HTML。
每个HTML获得源码、缓存之后就给Item一个标记
1 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
2 CountDownTimer countDownTimer;
3
4 @Override
5 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
6 switch (newState) {
7 case RecyclerView.SCROLL_STATE_IDLE:
8 Log.e(TAG, "onScrollStateChanged: " + "SCROLL_STATE_IDLE");
9 //3s 之后就开始加载数据
10 countDownTimer = new CountDownTimer(3000, 3000) {
11
12 @Override
13 public void onTick(long millisUntilFinished) {
14
15 }
16
17 @Override
18 public void onFinish() {
19 mAdapter.loadData();
20 }
21 };
22 countDownTimer.start();
23 break;
24 case RecyclerView.SCROLL_STATE_DRAGGING:
25 Log.e(TAG, "onScrollStateChanged: " + "SCROLL_STATE_DRAGGING");
26 if (countDownTimer != null) {
27 countDownTimer.cancel();
28 }
29 break;
30 case RecyclerView.SCROLL_STATE_SETTLING:
31 Log.e(TAG, "onScrollStateChanged: " + "SCROLL_STATE_SETTLING");
32 break;
33 default:
34 break;
35 }
36 }
adapter中的调用方法
1 public void loadData() {
2 Log.e(TAG, "3s 后加载数据 ");
3 if (!isLoad) {
4 ThreadPoolManger.getInstance().run();
5 isLoad = true;
6 }
7 }
线程池中的添加任务和运行任务的方法:
1 /**
2 * 添加任务
3 */
4 public void addRunnable(Runnable runnable) {
5 if (runnable == null) {
6 return;
7 }
8 activeThread.add(runnable);
9 }
10
11 /**
12 * 真正开始执行
13 */
14 public void run() {
15 if (activeThread.size() <= 0) {
16 return;
17 }
18 for (int i = 0; i < activeThread.size(); i++) {
19 execute(activeThread.get(i));
20 }
21 }
下载HTML方法以及回调,切换主线程:
1public class NetWorkRequest implements Runnable {
2 private NetCallBack mCallback;
3 private String url;
4 @SuppressLint("HandlerLeak")
5 private Handler mHandler = new Handler() {
6 @Override
7 public void handleMessage(Message msg) {
8 switch (msg.arg1) {
9 case 0:
10 mCallback.onFail((Exception) msg.obj);
11 break;
12 case 1:
13 mCallback.success((String) msg.obj, url);
14 break;
15 default:
16 break;
17 }
18 }
19 };
20
21 /**
22 * @param url 网页链接
23 * @param callback 回调
24 */
25 public NetWorkRequest(String url, NetCallBack callback) {
26 this.mCallback = callback;
27 this.url = url;
28 }
29
30 @Override
31 public void run() {
32 try {
33 Message message1 = mHandler.obtainMessage();
34 message1.arg1 = 2;
35 mHandler.sendMessage(message1);
36 URL url = new URL(this.url);
37 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
38 connection.setConnectTimeout(5000);
39 connection.setRequestMethod("GET");
40 connection.connect();
41 if (connection.getResponseCode() == 200) {
42 InputStream inputStream = connection.getInputStream();
43 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
44 byte[] buffer = new byte[1024];
45 int len = 0;
46 while ((len = inputStream.read(buffer)) != -1) {
47 outputStream.write(buffer, 0, len);
48 }
49 String html = new String(outputStream.toByteArray(), "utf-8");
50 Message message = mHandler.obtainMessage();
51 message.obj = html;
52 //1 请求成功 0请求失败
53 message.arg1 = 1;
54 mHandler.sendMessage(message);
55 //关闭流
56 inputStream.close();
57 outputStream.close();
58 }
59 } catch (Exception e) {
60 Message message = mHandler.obtainMessage();
61 message.arg1 = 0;
62 message.obj = e;
63 mHandler.sendMessage(message);
64 }
65 }
66}
2.点击列表加载缓存HTML和缓存图片
核心代码
1//1.加载缓存中的HTML
2mWebView.loadDataWithBaseURL(url, htmlFromCache, "text/html", "utf-8", null);
3//2.缓存没有图片就下载,有就直接返回
4
5 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
6 @Nullable
7 @Override
8 public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
9 if (!GET.equals(request.getMethod())) {
10 return super.shouldInterceptRequest(view, request);
11 }
12 /*
13 * js: mimeType = "application/x-javascript";
14 css: mimeType = "text/css";
15 html: mimeType = "text/html";
16 jpg/png: mimeType = "image/png";
17 */
18 String url = request.getUrl().toString();
19 if (url.endsWith(PNG) || url.endsWith(JPG)) {
20 Log.e(TAG, "url====" + url);
21 Bitmap bitmap = LruCacheUtil.getInstance().getBitmapFromCache(url);
22 if (bitmap != null) {
23 String mimeType = "image/png";
24 String encoding = "utf-8";
25 //bitmap 转换成 inputStream
26 ByteArrayOutputStream baos = new ByteArrayOutputStream();
27 bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
28 InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
29 Log.e(TAG, "加载缓存中的图片");
30 WebResourceResponse response = new WebResourceResponse(mimeType, encoding, inputStream);
31 try {
32 //关闭流
33 baos.close();
34 inputStream.close();
35 } catch (Exception e) {
36 e.printStackTrace();
37 }
38 return response;
39 } else {
40 //缓存中不存在就需要下载
41 ThreadPoolManger.getInstance().execute(new DownLoadBitmap(url));
42 }
43 return super.shouldInterceptRequest(view, request);
44 } else {
45 return super.shouldInterceptRequest(view, request);
46 }
47 }