Skip to content

Commit a597749

Browse files
Optimize docs
1 parent 6bf9d3f commit a597749

13 files changed

+192
-200
lines changed

docs/README.md

Lines changed: 9 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![](https://www.jitpack.io/v/DylanCaiCoding/LoadingStateView.svg)](https://www.jitpack.io/#DylanCaiCoding/LoadingLoadingStateView)
44
[![License](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/LICENSE)
5+
[![GitHub Repo stars](https://img.shields.io/github/stars/DylanCaiCoding/LoadingStateView?style=social)](https://github.com/DylanCaiCoding/LoadingStateView)
56

67
`LoadingStateView` 是一个深度解耦加载界面和标题栏的工具,只用了一个 Kotlin 文件实现,不算上注释少于 300 行代码。不仅能在请求网络数据时**显示加载中、加载成功、加载失败、无数据的视图或自定义视图**,还可以**对标题栏进行管理**
78

@@ -21,18 +22,17 @@
2122

2223
动态添加加载状态的布局:
2324

24-
| [Activity(error)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/ActErrorActivity.java) | [View(placeholder)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/ViewPlaceholderActivity.java) | [ViewPager(timeout)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/ViewPagerActivity.java) | [RecyclerView(cool loading)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/RecyclerViewActivity.java) |
25+
| [Activity(error)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/ActErrorActivity.java) | [View(placeholder)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/ViewPlaceholderActivity.java) | [ViewPager(timeout)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/ViewPagerActivity.java) | [RecyclerView(cool loading)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/RecyclerViewActivity.java) |
2526
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
2627
| ![](gif/activity_error.gif) | ![](gif/view_placeholder.gif) | ![](gif/viewpager_timeout.gif) | ![](gif/recyclerview_loading.gif) |
2728

2829
动态添加标题栏或装饰容器:
2930

30-
| [SpecialHeader(custom)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/CustomHeaderActivity.java) | [MultipleHeader(search)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/MultipleHeaderActivity.java) | [SpecialDecorView(scrolling)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/ScrollingToolbarActivity.java) | [BottomDecorView(editor)](app/src/main/java/com/dylanc/loadingstateview/sample/ui/BottomEditorActivity.java) |
31+
| [SpecialHeader(custom)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/CustomHeaderActivity.java) | [MultipleHeader(search)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/MultipleHeaderActivity.java) | [SpecialDecorView(scrolling)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/ScrollingToolbarActivity.java) | [BottomDecorView(editor)](https://github.com/DylanCaiCoding/LoadingStateView/blob/master/sample-java/src/main/java/com/dylanc/loadingstateview/sample/java/ui/BottomEditorActivity.java) |
3132
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
3233
| ![](gif/special_header_custom.gif) | ![](gif/multiple_header_search.gif) | ![](gif/special_decor_scrolling.gif) | ![](gif/bottom_decor_editor.gif) |
3334

34-
35-
## 开始使用
35+
## Gradle
3636

3737
在根目录的 `build.gradle` 添加:
3838

@@ -49,152 +49,14 @@ allprojects {
4949

5050
```groovy
5151
dependencies {
52-
implementation 'com.github.DylanCaiCoding:LoadingStateView:3.0.1'
53-
}
54-
```
55-
56-
### 基础用法
57-
58-
第一步,创建一个类继承 `LoadingStateView.ViewDelegate<VH extends ViewHolder>`,写法与 `RecyclerView.Adapter` 类似。如果需要实现点击重新请求数据,可以在点击事件调用 holder.getOnReloadListener.onReload() 方法。
59-
60-
```java
61-
public class LoadingViewDelegate extends LoadingStateView.ViewDelegate<LoadingStateView.ViewHolder> {
62-
63-
@NonNull
64-
@Override
65-
public LoadingStateView.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
66-
return new LoadingStateView.ViewHolder(inflater.inflate(R.layout.layout_loading_view, parent, false));
67-
}
68-
69-
@Override
70-
public void onBindViewHolder(@NonNull LoadingStateView.ViewHolder holder) {
71-
}
72-
}
73-
```
74-
75-
第二步,注册 `ViewDelegate`,关联一个视图类型。有五个默认类型,也可以传任意类型数据进行注册。
76-
77-
```java
78-
LoadingStateView loadingStateView = new LoadingStateView(this); // 可传 Activity 或 View
79-
loadingStateView.register(ViewType.LOADING, new LoadingViewDelegate());
80-
// 当需要支持点击重新请求数据时
81-
loadingStateView.setOnReloadListener(() -> {})
82-
```
83-
84-
如果需要注册成全局的 `ViewDelegate`
85-
86-
```java
87-
loadingStateView.setViewDelegatePool(pool -> {
88-
pool.register(ViewType.LOADING, new LoadingViewDelegate());
89-
return null;
90-
});
91-
```
92-
93-
第三步,显示对应类型的视图。
94-
95-
```java
96-
loadingStateView.showView(viewType);
97-
loadingStateView.showLoadingView(); // 对应视图类型 ViewType.LOADING
98-
loadingStateView.showContentView(); // 对应视图类型 ViewType.CONTENT
99-
loadingStateView.showErrorView(); // 对应视图类型 ViewType.ERROR
100-
loadingStateView.showEmptyView(); // 对应视图类型 ViewType.EMPTY
101-
```
102-
103-
**动态更新已显示视图**
104-
105-
在显示了视图之后,可以对视图进行更改刷新。和 `RecyclerView.Adapter` 类似,会执行适配器的 `onBindViewHolder()` 方法。
106-
107-
```java
108-
loadingStateView.updateView(ViewType.ERROR, (ErrorViewDelegate delegate) -> {
109-
delegate.msg = "服务器繁忙,请稍后重试";
110-
});
111-
```
112-
113-
### 高级用法
114-
115-
#### 添加标题栏
116-
117-
如果是**普通的标题栏**,就是简单地在内容的上方添加标题栏:
118-
119-
和前面的用法类似,先创建一个继承 `LoadingStateView.ViewDelegate<VH extends ViewHolder>` 的标题栏适配器,然后就能在内容的上方添加标题栏了,可以添加多个头部。
120-
121-
```java
122-
loadingStateView.setDecorHeader(new TitleViewDelegate("标题名"), new SearchHeaderViewDelegate());
123-
```
124-
125-
如果是**特殊的标题栏**,比如有联动效果,就不能直接使用上面的方式了。这时我们要给内容增加个装饰的容器。
126-
127-
先实现一个不含内容的布局。
128-
129-
```xml
130-
<?xml version="1.0" encoding="utf-8"?>
131-
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
132-
xmlns:app="http://schemas.android.com/apk/res-auto"
133-
android:layout_width="match_parent"
134-
android:layout_height="match_parent"
135-
android:fitsSystemWindows="true">
136-
137-
<com.google.android.material.appbar.AppBarLayout
138-
android:id="@+id/app_bar"
139-
android:layout_width="match_parent"
140-
android:layout_height="@dimen/app_bar_height"
141-
android:fitsSystemWindows="true"
142-
android:theme="@style/AppTheme.AppBarOverlay">
143-
144-
<com.google.android.material.appbar.CollapsingToolbarLayout
145-
android:id="@+id/toolbar_layout"
146-
android:layout_width="match_parent"
147-
android:layout_height="match_parent"
148-
android:fitsSystemWindows="true"
149-
app:contentScrim="?attr/colorPrimary"
150-
app:layout_scrollFlags="scroll|exitUntilCollapsed"
151-
app:toolbarId="@+id/toolbar">
152-
153-
<androidx.appcompat.widget.Toolbar
154-
android:id="@+id/toolbar"
155-
android:layout_width="match_parent"
156-
android:layout_height="?attr/actionBarSize"
157-
app:layout_collapseMode="pin"
158-
app:popupTheme="@style/AppTheme.PopupOverlay" />
159-
</com.google.android.material.appbar.CollapsingToolbarLayout>
160-
</com.google.android.material.appbar.AppBarLayout>
161-
162-
<FrameLayout
163-
android:id="@+id/content_parent"
164-
android:layout_width="match_parent"
165-
android:layout_height="match_parent"
166-
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
167-
</androidx.coordinatorlayout.widget.CoordinatorLayout>
168-
```
169-
170-
创建一个类继承 `LoadingStateView.DecorViewDelegate` ,加载实现的布局,并指定一个添加内容的容器。
171-
172-
```java
173-
class ScrollingDecorViewDelegate : LoadingStateView.DecorViewDelegate() {
174-
@NotNull
175-
override fun onCreateDecorView(@NotNull inflater: LayoutInflater): View {
176-
return inflater.inflate(R.layout.layout_scrolling, null)
177-
}
178-
179-
@NotNull
180-
fun getContentParent(@NotNull decorView: View): ViewGroup {
181-
return decorView.findViewById(R.id.content_parent)
182-
}
52+
// java
53+
implementation 'com.github.DylanCaiCoding.LoadingStateView:loadingstateview:4.0.0'
54+
55+
// kotlin
56+
implementation 'com.github.DylanCaiCoding.LoadingStateView:loadingstateview-ktx:4.0.0'
18357
}
18458
```
18559

186-
最后设置一下就可以了。
187-
188-
```java
189-
loadingStateView.setDecorView(new ScrollingDecorViewDelegate());
190-
```
191-
192-
上述的两种使用方式都是可以进行多次设置,不过每次设置会把上一次设置的样式给替换掉。
193-
194-
## LoadingHelper 迁移指南
195-
196-
本库原名是 `LoadingHelper`,后面对部分类名和方法名进行修改。老用户可以查看[迁移指南](https://github.com/DylanCaiCoding/LoadingHelper/blob/main/README_CN.md)改为最新的用法,如果觉得麻烦并且不是强迫症患者,也可以不迁移。
197-
19860
## 作者其它的库
19961

20062
|| 简介 |

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
* [基础用法](/zh/basic-usage)
33
* [Kotlin 委托用法](/zh/delegate)
44
* [结合 ViewBinding 使用](/zh/viewbinding)
5+
* [老版本迁移指南](/zh/migration-guide)
56
* [Q&A](/zh/q&a)

docs/img/base_activity_code.png

-2.48 KB
Loading
32.2 KB
Loading
35.1 KB
Loading
39.6 KB
Loading
49.7 KB
Loading

docs/img/base_fragment_code.png

-2.6 KB
Loading

docs/zh/basic-usage.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 基础用法
22

3-
使用 Kotlin 开发的话推荐用委托的用法,不过建议先把基础的 `LoadingStateView` 用法看了
3+
使用 Kotlin 开发推荐用[委托用法](/zh/delegate)
44

55
## 显示加载中、加载失败等缺省页
66

@@ -128,9 +128,7 @@ loadingStateView.showEmptyView(); // 对应视图类型 ViewType.EMPTY
128128

129129
## 更新视图样式
130130

131-
需要在 `ViewDelegate` 自行增加更新的方法,然后获取到对应的 `ViewDelegate` 进行更新。
132-
133-
比如在 `ErrorViewDelegate` 增加了 `updateMsg(msg)` 方法修改请求失败的文字:
131+
需要在 `ViewDelegate` 自行增加更新的方法,然后更新对应的 `ViewDelegate`。比如在 `ErrorViewDelegate` 增加了 `updateMsg(msg)` 方法修改请求失败的文字:
134132

135133
<!-- tabs:start -->
136134

@@ -171,11 +169,11 @@ loadingStateView.setHeaders(new ToolbarViewDelegate("消息"), new SearchViewDel
171169

172170
<!-- tabs:end -->
173171

174-
## 给内容的外层增加装饰布局
172+
## 给内容增加装饰
175173

176-
可以给内容布局添加一层装饰,实现更复杂的样式,非简单地在头部增加控件,比如带联动效果的标题栏、DrawerLayout、底部输入框等布局。
174+
可以给内容布局外层添加一层装饰,实现更复杂的样式,非简单地在内容上方增加控件,比如带联动效果的标题栏、DrawerLayout、底部输入框等布局。
177175

178-
我们来实现滑动隐藏标题栏的效果,写一个带联动效果的标题栏布局,其中有个 FragmentLayout 是用于填充内容和切换缺省页
176+
接下来以滑动隐藏标题栏的效果为例,先写一个带联动效果的标题栏布局,其中有个 FragmentLayout 是用于填充内容和显示缺省页
179177

180178
```xml
181179
<?xml version="1.0" encoding="utf-8"?>
@@ -218,15 +216,16 @@ loadingStateView.setHeaders(new ToolbarViewDelegate("消息"), new SearchViewDel
218216
#### **Kotlin**
219217

220218
```kotlin
221-
class ScrollingDecorViewDelegate(private val title: String) : LoadingStateView.DecorViewDelegate() {
219+
class ScrollingDecorViewDelegate(
220+
private val activity: Activity,
221+
private val title: String
222+
) : LoadingStateView.DecorViewDelegate() {
222223

223224
override fun onCreateDecorView(context: Context, inflater: LayoutInflater): View {
224225
val view = inflater.inflate(R.layout.layout_scrolling_toolbar, null)
225226
val toolbar: Toolbar = view.findViewById(R.id.toolbar)
226227
toolbar.title = title
227-
toolbar.setNavigationOnClickListener {
228-
if (context is Activity) context.finish()
229-
}
228+
toolbar.setNavigationOnClickListener { activity.finish() }
230229
return view
231230
}
232231

@@ -240,17 +239,18 @@ class ScrollingDecorViewDelegate(private val title: String) : LoadingStateView.D
240239

241240
```java
242241
public class ScrollingDecorViewDelegate extends LoadingStateView.DecorViewDelegate {
242+
private final Activity activity;
243243
private final String title;
244244

245-
public ScrollingDecorViewDelegate(String title) {
245+
public ScrollingDecorViewDelegate(Activity activity, String title) {
246+
this.activity = activity;
246247
this.title = title;
247248
}
248249

249250
@NotNull
250251
@Override
251252
public View onCreateDecorView(@NonNull Context context, @NotNull LayoutInflater inflater) {
252253
View view = inflater.inflate(R.layout.layout_scrolling_toolbar, null);
253-
Activity activity = (Activity) inflater.getContext();
254254
Toolbar toolbar = view.findViewById(R.id.toolbar);
255255
toolbar.setTitle(title);
256256
toolbar.setNavigationOnClickListener(v -> activity.finish());
@@ -277,13 +277,13 @@ public class ScrollingDecorViewDelegate extends LoadingStateView.DecorViewDelega
277277
#### **Kotlin**
278278

279279
```kotlin
280-
loadingStateView.setDecorView(ScrollingDecorViewDelegate("Test"))
280+
loadingStateView.setDecorView(ScrollingDecorViewDelegate(this, "title"))
281281
```
282282

283283
#### **Java**
284284

285285
```java
286-
loadingStateView.setDecorView(new ScrollingDecorViewDelegate("Test"));
286+
loadingStateView.setDecorView(new ScrollingDecorViewDelegate(this, "title"));
287287
```
288288

289289
<!-- tabs:end -->

0 commit comments

Comments
 (0)