Skip to content

Commit fd00763

Browse files
authored
Update README.md
1 parent 38be342 commit fd00763

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

README.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,69 @@
1-
# DataLoader
1+
# DataLoader
2+
![license](https://img.shields.io/github/license/JeremyLiao/DataLoader.svg) [![version](https://img.shields.io/badge/JCenter-v1.0.0-blue.svg)](https://mvnrepository.com/artifact/com.jeremyliao/)
3+
4+
DataLoader是一个Android异步数据加载框架。最常用的场景用于Activity打开之前预加载数据,在Activity的UI布局初始化完成后显示预加载的数据,大大缩短启动时间。
5+
6+
## DataLoader的特点
7+
1. 用于异步数据加载。
8+
2. 基于LiveData,生命周期感知,在Activity中使用的时候不用关注何时remove listener。
9+
10+
## 典型应用场景
11+
1. 在Application.onCreate中预加载数据,在需要用到的页面中获取预加载的数据
12+
2. 在启动页中预加载主页所需的数据,减少用户等待时间
13+
3. startActivity之前就开始预加载,UI初始化完成后显示预加载的数据
14+
4. 复杂页面(UI初始化耗时较多的页面)内部在UI初始化开始之前预加载数据,UI初始化完成后显示预加载的数据
15+
5. ListView/RecyclerView在上拉加载更多之前预加载下一页的数据
16+
17+
## 添加依赖
18+
Via Gradle:
19+
20+
```
21+
implementation 'com.jeremyliao:data-loader:1.0.0'
22+
```
23+
24+
## 使用
25+
#### 1. 预加载数据
26+
```
27+
int preLoaderId = DataLoader.load(new LoadTask<String>() {
28+
@Override
29+
public String loadData() {
30+
try {
31+
Thread.sleep(1000);
32+
} catch (InterruptedException ignored) {
33+
}
34+
return "data from network server";
35+
}
36+
});
37+
Intent intent = new Intent(this, PreLoadBeforeLaunchActivity.class);
38+
intent.putExtra("preLoaderId", preLoaderId);
39+
startActivity(intent);
40+
```
41+
42+
#### 2. 使用预加载的数据
43+
```
44+
int id = getIntent().getIntExtra("preLoaderId", -1);
45+
DataLoader.listen(id, this, new LoadListener<String>() {
46+
@Override
47+
public void onDataArrived(String data) {
48+
tvShow.setText(data);
49+
}
50+
});
51+
```
52+
53+
#### 3. 刷新数据
54+
```
55+
DataLoader.refresh(id);
56+
```
57+
58+
## 混淆规则
59+
60+
```
61+
-dontwarn com.jeremyliao.dataloader.**
62+
-keep class com.jeremyliao.dataloader.** { *; }
63+
```
64+
65+
## Demo
66+
简单的Demo可参考:[MainActivity.java](DataLoader/app/src/main/java/com/jeremyliao/dataloader/app/MainActivity.java)
67+
68+
## Reference
69+
本项目参考了开源项目[luckybilly/PreLoader](https://github.com/luckybilly/PreLoader),借鉴了PreLoader的设计思想和使用场景。只是利用LiveData重新实现,构架更为简单,使用起来也不用关注何时remove listener,使用更方便。在此对PreLoader的作者[@luckybilly](https://github.com/luckybilly)表示感谢。

0 commit comments

Comments
 (0)