11# LoadingStateView
22
3- English | [ 中文] ( README_ZH_CN.md )
3+ [ ![ ] ( https://www.jitpack.io/v/DylanCaiCoding/LoadingStateView.svg )] ( https://www.jitpack.io/#DylanCaiCoding/LoadingLoadingStateView )
4+ [ ![ License] ( https://img.shields.io/badge/License-Apache--2.0-blue.svg )] ( https://github.com/DylanCaiCoding/LoadingStateView/blob/master/LICENSE )
45
5- [ ![ ] ( https://www.jitpack.io/v/DylanCaiCoding/ LoadingStateView.svg )] ( https://www.jitpack.io/#DylanCaiCoding/LoadingLoadingStateView ) [ ![ License ] ( https://img.shields.io/badge/License-Apache--2.0-blue.svg )] ( https://github.com/DylanCaiCoding/LoadingStateView/blob/master/LICENSE )
6+ ` LoadingStateView ` 是一个深度解耦加载界面和标题栏的工具,只用了一个 Kotlin 文件实现,不算上注释少于 300 行代码。不仅能在请求网络数据时 ** 显示加载中、加载成功、加载失败、无数据的视图或自定义视图 ** ,还可以 ** 对标题栏进行管理 ** 。
67
7- ` LoadingStateView ` is a highly expandable Android library for showing loading status view on the low-coupling way, it is implemented with a Kotlin code of less than 300 lines without comment statement . it not only ** shows different view like loading, content, error, empty and customized view** when loading network data, but also ** manages title bar.**
8+ - 无需在布局添加视图代码
9+ - 可显示自定义视图
10+ - 可用于 Activity、Fragment、列表或指定的 View
11+ - 可管理标题栏和添加多个头部控件
12+ - 可设置重新请求数据的事件
13+ - 可动态更新视图样式
14+ - 可结合绝大部分第三方控件使用
815
9- ## Feature
16+ ## 示例
1017
11- - No need to add view code to the layout.
12- - Support for show custom views.
13- - Support for use for Activity, Fragment, RecyclerView, View.
14- - Support for managing the title bar and add multiple headers.
15- - Support for set reload event.
16- - Support for update views anytime.
17- - Support for use with most third-party libraries.
18+ 点击或者扫描二维码下载
1819
19- ## Demo
20+ [ ![ QR code ] ( https://github.com/DylanCaiCoding/LoadingStateView/img/app_download_qr_code.png )] ( https://www.pgyer.com/loadinghelper )
2021
21- Click or scan QR code to download
22-
23- [ ![ QR code] ( img/app_download_qr_code.png )] ( https://www.pgyer.com/loadinghelper )
22+ 动态添加加载状态的布局:
2423
2524| [ 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 ) |
2625| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
2726| ![ ] ( gif/activity_error.gif ) | ![ ] ( gif/view_placeholder.gif ) | ![ ] ( gif/viewpager_timeout.gif ) | ![ ] ( gif/recyclerview_loading.gif ) |
2827
28+ 动态添加标题栏或装饰容器:
29+
2930| [ 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 ) |
3031| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
3132| ![ ] ( gif/special_header_custom.gif ) | ![ ] ( gif/multiple_header_search.gif ) | ![ ] ( gif/special_decor_scrolling.gif ) | ![ ] ( gif/bottom_decor_editor.gif ) |
3233
3334
34- ## Getting started
35+ ## 开始使用
3536
36- Add it in your root ` build.gradle ` at the end of repositories :
37+ 在根目录的 ` build.gradle ` 添加 :
3738
3839``` groovy
3940allprojects {
4041 repositories {
41- ...
42+ // ...
4243 maven { url 'https://www.jitpack.io' }
4344 }
4445}
4546```
4647
47- Add dependencies in your module ` build.gradle ` :
48+ 在模块的 ` build.gradle ` 添加依赖:
4849
4950``` groovy
5051dependencies {
51- implementation 'com.github.DylanCaiCoding:LoadingStateView:3.0.0 '
52+ implementation 'com.github.DylanCaiCoding:LoadingStateView:3.0.1 '
5253}
5354```
5455
55- ### Usage
56+ ### 基础用法
5657
57- #### Step 1. Create a class extends ` LoadingStateView.ViewDelegate<VH extends ViewHolder> ` , for example:
58+ 第一步,创建一个类继承 ` LoadingStateView.ViewDelegate<VH extends ViewHolder> ` ,写法与 ` RecyclerView.Adapter ` 类似。如果需要实现点击重新请求数据,可以在点击事件调用 holder.getOnReloadListener.onReload() 方法。
5859
5960``` java
6061public class LoadingViewDelegate extends LoadingStateView .ViewDelegate<LoadingStateView .ViewHolder > {
@@ -71,14 +72,16 @@ public class LoadingViewDelegate extends LoadingStateView.ViewDelegate<LoadingSt
7172}
7273```
7374
74- #### Step 2. Register ` ViewDelegate ` with a view type, for example:
75+ 第二步,注册 ` ViewDelegate ` ,关联一个视图类型。有五个默认类型,也可以传任意类型数据进行注册。
7576
7677``` java
77- LoadingStateView loadingStateView = new LoadingStateView (this );
78+ LoadingStateView loadingStateView = new LoadingStateView (this ); // 可传 Activity 或 View
7879loadingStateView. register(ViewType . LOADING , new LoadingViewDelegate ());
80+ // 当需要支持点击重新请求数据时
81+ loadingStateView. setOnReloadListener(() - > {})
7982```
8083
81- ##### Or if you want to register a global ` ViewDelegate ` .
84+ 如果需要注册成全局的 ` ViewDelegate ` 。
8285
8386``` java
8487loadingStateView. setViewDelegatePool(pool - > {
@@ -87,53 +90,84 @@ loadingStateView.setViewDelegatePool(pool -> {
8790});
8891```
8992
90- #### Step 3. Show view by view type, for example:
93+ 第三步,显示对应类型的视图。
9194
9295``` java
9396loadingStateView. showView(viewType);
94- loadingStateView. showLoadingView(); // view type is ViewType.LOADING
95- loadingStateView. showContentView(); // view type is ViewType.CONTENT
96- loadingStateView. showErrorView(); // view type is ViewType.ERROR
97- loadingStateView. showEmptyView(); // view type is ViewType.EMPTY
97+ loadingStateView. showLoadingView(); // 对应视图类型 ViewType.LOADING
98+ loadingStateView. showContentView(); // 对应视图类型 ViewType.CONTENT
99+ loadingStateView. showErrorView(); // 对应视图类型 ViewType.ERROR
100+ loadingStateView. showEmptyView(); // 对应视图类型 ViewType.EMPTY
98101```
99102
100- #### When you need to reload data.
103+ ** 动态更新已显示视图 **
101104
102- ``` java
103- loadingStateView. setOnReloadListener(new LoadingStateView .OnReloadListener () {
104- @Override
105- public void onReload () {
106- // request data again
107- }
108- });
109-
110- // In the adapter
111- holder. getOnReloadListener. onReload();
112- ```
113-
114- #### When you need to change view after view showed.
105+ 在显示了视图之后,可以对视图进行更改刷新。和 ` RecyclerView.Adapter ` 类似,会执行适配器的 ` onBindViewHolder() ` 方法。
115106
116107``` java
117- ErrorViewDelegate errorViewDelegate = loadingStateView. getViewDelegate (ViewType . ERROR);
118- errorViewDelegate . errorText = " Fail to load, please wait " ;
119- loadingStateView . notifyDataSetChanged( ViewType . ERROR );
108+ loadingStateView. updateView (ViewType . ERROR, ( ErrorViewDelegate delegate) - > {
109+ delegate . msg = " 服务器繁忙,请稍后重试 " ;
110+ } );
120111```
121112
122- ### Advanced usage
113+ ### 高级用法
123114
124- #### Add title view
115+ #### 添加标题栏
125116
126- If you want to add an ordinary title bar above the content.
117+ 如果是 ** 普通的标题栏 ** ,就是简单地在内容的上方添加标题栏:
127118
128- Similar to the previous usage, create a class extends ` LoadingStateView.ViewDelegate<VH extends ViewHolder> ` and set header.
119+ 和前面的用法类似,先创建一个继承 ` LoadingStateView.ViewDelegate<VH extends ViewHolder> ` 的标题栏适配器,然后就能在内容的上方添加标题栏了,可以添加多个头部。
129120
130121``` java
131- loadingStateView. setDecorHeader(new TitleViewDelegate (" title " ), new SearchHeaderViewDelegate ());
122+ loadingStateView. setDecorHeader(new TitleViewDelegate (" 标题名 " ), new SearchHeaderViewDelegate ());
132123```
133124
134- If you want to add an special title bar with linkage effect.
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+ ```
135169
136- Create a class extends ` LoadingStateView.DecorViewDelegate ` to create a decorated view and specify a loading container.
170+ 创建一个类继承 ` LoadingStateView.DecorViewDelegate ` ,加载实现的布局,并指定一个添加内容的容器。
137171
138172``` java
139173class ScrollingDecorViewDelegate : LoadingStateView.DecorViewDelegate() {
@@ -149,35 +183,35 @@ class ScrollingDecorViewDelegate : LoadingStateView.DecorViewDelegate() {
149183}
150184```
151185
152- Then set it up.
186+ 最后设置一下就可以了。
153187
154188``` java
155189loadingStateView. setDecorView(new ScrollingDecorViewDelegate ());
156190```
157191
158- ## LoadingHelper's migration Guide
192+ 上述的两种使用方式都是可以进行多次设置,不过每次设置会把上一次设置的样式给替换掉。
193+
194+ ## LoadingHelper 迁移指南
159195
160- The original name of this library is ` LoadingHelper ` , and some class and method names should be changed later. Old users can check the [ migration guide ] ( https://github.com/DylanCaiCoding/LoadingHelper ) and change it to the latest usage. If you feel troublesome and are not obsessive-compulsive, you can also not migrate.
196+ 本库原名是 ` LoadingHelper ` ,后面对部分类名和方法名进行修改。老用户可以查看 [ 迁移指南 ] ( https://github.com/DylanCaiCoding/LoadingHelper/blob/main/README_CN.md ) 改为最新的用法,如果觉得麻烦并且不是强迫症患者,也可以不迁移。
161197
162- ## Author's other libraries
198+ ## 作者其它的库
163199
164- | Library | Description |
165- | ------------------------------------------------------------ | ------------------------------------------------------------ |
166- | [ Longan] ( https://github.com/DylanCaiCoding/Longan ) | A collection of Kotlin utils which makes Android application development faster and easier. |
167- | [ ViewBindingKTX] ( https://github.com/DylanCaiCoding/ViewBindingKTX ) | The most comprehensive utils of ViewBinding. |
168- | [ MMKV-KTX] ( https://github.com/DylanCaiCoding/MMKV-KTX ) | Easier to use the MMKV. |
169- | [ ActivityResultLauncher] ( https://github.com/DylanCaiCoding/ActivityResultLauncher ) | Perfect replacement for ` startActivityForResult() ` |
200+ | 库 | 简介 |
201+ | ------------------------------------------------------------ | ---------------------------------------------- |
202+ | [ Longan] ( https://github.com/DylanCaiCoding/Longan ) | 可能是最好用的 Kotlin 工具库 |
203+ | [ ViewBindingKTX] ( https://github.com/DylanCaiCoding/ViewBindingKTX ) | 最全面的 ViewBinding 工具 |
204+ | [ MMKV-KTX] ( https://github.com/DylanCaiCoding/MMKV-KTX ) | 用属性委托的方式使用 MMKV |
170205
171- ## Thanks
206+ ## 感谢
172207
173- - [ luckbilly/Gloading] ( https://github.com/luckybilly/Gloading ) Optimize my library standing on the shoulders of giants.
174- - [ drakeet/MultiType] ( https://github.com/drakeet/MultiType ) Referenced the usage of multiple adapters.
175- - [ dinuscxj/LoadingDrawable] ( https://github.com/dinuscxj/LoadingDrawable ) The cool loading effect in the demo.
208+ - [ luckbilly/Gloading] ( https://github.com/luckybilly/Gloading ) 站在了巨人肩膀上优化了本库,非常感谢!
209+ - [ drakeet/MultiType] ( https://github.com/drakeet/MultiType ) 参考了注册配置多适配器的思想和用法
210+ - [ dinuscxj/LoadingDrawable] ( https://github.com/dinuscxj/LoadingDrawable ) 示例中的自定义加载动画
176211
177212## License
178213
179214```
180-
181215Copyright (C) 2019. Dylan Cai
182216
183217Licensed under the Apache License, Version 2.0 (the "License");
0 commit comments