Skip to content

Commit

Permalink
更新addData函数, 更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Sep 16, 2020
1 parent c3a8c74 commit c5776fa
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ allprojects {
在 module 的 build.gradle 添加依赖

```groovy
implementation 'com.github.liangjingkanji:BRV:1.3.11'
implementation 'com.github.liangjingkanji:BRV:1.3.12'
```


Expand Down
32 changes: 17 additions & 15 deletions brv/src/main/java/com/drake/brv/PageRefreshLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView
import com.drake.brv.listener.OnBindViewHolderListener
import com.drake.brv.listener.OnMultiStateListener
import com.drake.brv.utils.bindingAdapter
import com.drake.statelayout.StateConfig
import com.drake.statelayout.StateConfig.errorLayout
import com.drake.statelayout.StateLayout
Expand Down Expand Up @@ -210,37 +211,38 @@ open class PageRefreshLayout : SmartRefreshLayout, OnRefreshLoadMoreListener {
}

/**
* 直接接受数据, 自动判断当前属于下拉刷新还是上拉加载更多
* 自动分页自动加载数据, 自动判断当前属于下拉刷新还是上拉加载更多
*
* 此函数每次调用会导致[index]递增或者下拉刷新会导致[index]等于[startIndex]
* @param data 数据集
* @param adapter 假设PageRefreshLayout不能直接包裹RecyclerView, 然后也想使用自动分页, 请指定此参数, 因为自动分页需要[BindingAdapter]实例
* @param hasMore 在函数参数中返回布尔类型来判断是否还存在下一页数据, 默认值true表示始终存在
* @param isEmpty 返回true表示数据为空, 将显示缺省页 -> 空布局, 默认以[data.isNullOrEmpty()]则为空
*/
fun addData(
data: List<Any?>?,
isEmpty: () -> Boolean = { data.isNullOrEmpty() },
hasMore: BindingAdapter.() -> Boolean = { true }
) {

val rv = contentView as? RecyclerView
?: throw UnsupportedOperationException("PageRefreshLayout require content RecyclerView")

val adapter = rv.adapter as? BindingAdapter
?: throw UnsupportedOperationException("RecyclerView require use BindingAdapter")
fun addData(data: List<Any?>?,
adapter: BindingAdapter? = null,
isEmpty: () -> Boolean = { data.isNullOrEmpty() },
hasMore: BindingAdapter.() -> Boolean = { true }) {

val adjustAdapter = when {
adapter != null -> adapter
contentView is RecyclerView -> (contentView as RecyclerView).bindingAdapter
else -> throw UnsupportedOperationException("Use parameter [adapter] on [addData] function or PageRefreshLayout direct wrap RecyclerView")
}

val isRefreshState = state == RefreshState.Refreshing

if (isRefreshState) {
adapter.models = data
adjustAdapter.models = data
if (isEmpty()) {
showEmpty()
return
}
} else {
adapter.addModels(data)
adjustAdapter.addModels(data)
}

val hasMoreResult = adapter.hasMore()
val hasMoreResult = adjustAdapter.hasMore()
index += 1

if (isRefreshState) showContent(hasMoreResult) else finish(true, hasMoreResult)
Expand Down
2 changes: 1 addition & 1 deletion brv/src/main/java/com/drake/brv/utils/RecyclerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.drake.brv.layoutmanager.HoverLinearLayoutManager
import com.drake.brv.layoutmanager.HoverStaggeredGridLayoutManager

val RecyclerView.bindingAdapter
get() = adapter as? BindingAdapter ?: throw NullPointerException("BindingAdapter is null")
get() = adapter as? BindingAdapter ?: throw NullPointerException("RecyclerView has no BindingAdapter")

var RecyclerView.models
get() = bindingAdapter.models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

`fun addFastClickable(@IdRes vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)

添加点击事件(未使用防抖)
指定Id的视图将被监听点击事件(未使用防抖)

Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

`fun addLongClickable(@IdRes vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)

添加长按事件
指定Id的视图将被监听长按事件

6 changes: 3 additions & 3 deletions docs/api/brv/com.drake.brv/-binding-adapter/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
| [headerCount](header-count.md) | `val headerCount: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) |
| [headers](headers.md) | `var headers: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>` |
| [hoverEnabled](hover-enabled.md) | `var hoverEnabled: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) |
| [itemTouchHelper](item-touch-helper.md) | `var itemTouchHelper: ItemTouchHelper?` |
| [itemTouchHelper](item-touch-helper.md) | 等效于[RecyclerView.addItemDecoration](#)设置`var itemTouchHelper: ItemTouchHelper?` |
| [modelCount](model-count.md) | `val modelCount: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) |
| [models](models.md) | `var models: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>?` |
| [onHoverAttachListener](on-hover-attach-listener.md) | `var onHoverAttachListener: `[`OnHoverAttachListener`](../../com.drake.brv.listener/-on-hover-attach-listener/index.md)`?` |
Expand All @@ -60,10 +60,10 @@
| Name | Summary |
|---|---|
| [addClickable](add-clickable.md) | 添加点击事件 默认500ms防抖, 修改[clickPeriod](click-period.md)属性可以全局设置间隔时间, 单位毫秒`fun addClickable(vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addFastClickable](add-fast-clickable.md) | 添加点击事件(未使用防抖)`fun addFastClickable(vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addFastClickable](add-fast-clickable.md) | 指定Id的视图将被监听点击事件(未使用防抖)`fun addFastClickable(vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addFooter](add-footer.md) | `fun addFooter(model: `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?, index: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` = -1, animation: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = false): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addHeader](add-header.md) | `fun addHeader(model: `[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?, index: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` = -1, animation: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = false): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addLongClickable](add-long-clickable.md) | 添加长按事件`fun addLongClickable(vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addLongClickable](add-long-clickable.md) | 指定Id的视图将被监听长按事件`fun addLongClickable(vararg id: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addModels](add-models.md) | 添加新的数据`fun addModels(models: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>?, animation: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = true): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addType](add-type.md) | 添加多类型`fun <M> addType(layout: `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)<br>通过回调函数添加多类型, 一对多多类型`fun <M> addType(block: M.(`[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`) -> `[`Int`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [checkedAll](checked-all.md) | 单选模式下不支持全选, 但支持取消全部选择`fun checkedAll(checked: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = true): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

# itemTouchHelper

`var itemTouchHelper: ItemTouchHelper?`
`var itemTouchHelper: ItemTouchHelper?`

等效于[RecyclerView.addItemDecoration](#)设置

### Property

`itemTouchHelper` - 等效于[RecyclerView.addItemDecoration](#)设置
3 changes: 3 additions & 0 deletions docs/api/brv/com.drake.brv/-binding-adapter/on-click.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

点击事件回调

### Parameters

`id` - 添加监听点击事件视图的Id, 等效于[addClickable](add-clickable.md)
3 changes: 3 additions & 0 deletions docs/api/brv/com.drake.brv/-binding-adapter/on-long-click.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

长按点击事件回调

### Parameters

`id` - 添加监听长按事件视图的Id, 等效于[addLongClickable](add-long-clickable.md)
8 changes: 6 additions & 2 deletions docs/api/brv/com.drake.brv/-page-refresh-layout/add-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

# addData

`fun addData(data: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>?, isEmpty: () -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { data.isNullOrEmpty() }, hasMore: `[`BindingAdapter`](../-binding-adapter/index.md)`.() -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { true }): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)
`fun addData(data: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>?, adapter: `[`BindingAdapter`](../-binding-adapter/index.md)`? = null, isEmpty: () -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { data.isNullOrEmpty() }, hasMore: `[`BindingAdapter`](../-binding-adapter/index.md)`.() -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { true }): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)

直接接受数据, 自动判断当前属于下拉刷新还是上拉加载更多
自动分页自动加载数据, 自动判断当前属于下拉刷新还是上拉加载更多

此函数每次调用会导致[index](--index--.md)递增或者下拉刷新会导致[index](--index--.md)等于[startIndex](start-index.md)

### Parameters

`data` - 数据集

`adapter` - 假设PageRefreshLayout不能直接包裹RecyclerView, 然后也想使用自动分页, 请指定此参数, 因为自动分页需要[BindingAdapter](../-binding-adapter/index.md)实例

`hasMore` - 在函数参数中返回布尔类型来判断是否还存在下一页数据, 默认值true表示始终存在

`isEmpty` - 返回true表示数据为空, 将显示缺省页 -&gt; 空布局, 默认以[data.isNullOrEmpty](#)则为空
2 changes: 1 addition & 1 deletion docs/api/brv/com.drake.brv/-page-refresh-layout/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

| Name | Summary |
|---|---|
| [addData](add-data.md) | 直接接受数据, 自动判断当前属于下拉刷新还是上拉加载更多`fun addData(data: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>?, isEmpty: () -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { data.isNullOrEmpty() }, hasMore: `[`BindingAdapter`](../-binding-adapter/index.md)`.() -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { true }): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [addData](add-data.md) | 自动分页自动加载数据, 自动判断当前属于下拉刷新还是上拉加载更多`fun addData(data: `[`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)`<`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?>?, adapter: `[`BindingAdapter`](../-binding-adapter/index.md)`? = null, isEmpty: () -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { data.isNullOrEmpty() }, hasMore: `[`BindingAdapter`](../-binding-adapter/index.md)`.() -> `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = { true }): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [finish](finish.md) | 关闭下拉加载|上拉刷新`fun finish(success: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = true, hasMore: `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` = false): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [onAttachedToWindow](on-attached-to-window.md) | `open fun onAttachedToWindow(): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [onEmpty](on-empty.md) | `fun onEmpty(block: `[`View`](https://developer.android.com/reference/android/view/View.html)`.(`[`Any`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`?) -> `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)`): `[`PageRefreshLayout`](./index.md) |
Expand Down
22 changes: 13 additions & 9 deletions docs/refresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,21 @@ page.apply {
// 设置分页加载第一页的索引, 默认=1, 触发刷新会重置索引. 如果需要修改在Application设置一次即可
// PageRefreshLayout.startIndex = 1

pageLayout.onRefresh {
// 下拉刷新和上拉加载都会执行这个网络请求, 除非另外设置onLoadMore
get("/path") {
param("key", "value")
param("page", index) // 这里使用框架提供的属性
}.page(this) {
// 该回调函数参数返回一个布尔类型用于判断是否存在下一页, 决定上拉加载的状态. 以及当前属于刷新还是加载更多条目
addData(data){ adapter.itemCount < data.count // 这里是判断是否由更多页, 具体逻辑根据接口定义 }
}
// 下拉刷新和上拉加载都会执行onRefresh, 除非另外设置onLoadMore
pageLayout.onRefresh {
scope {
val data = Get<String>("/path").await()
addData(data.list){
// 该回调函数参数返回一个布尔类型用于判断是否存在下一页, 决定上拉加载是否关闭
adapter.itemCount < data.count // 这里是判断是否由更多页, 具体逻辑根据接口定义
}
}
}
```

这里的网络请求使用的是我开源的另一个项目Net, 支持扩展BRV. GitHub: [Net](https://github.com/liangjingkanji/Net).
<br>

!!! note
假设`PageRefreshLayout`没有直接包裹RecyclerView, 这个时候需要[addData](api/brv/com.drake.brv/-page-refresh-layout/add-data.md)函数指定参数adapter来使用自动分页, 否则将抛出异常

Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ class RefreshFragment : Fragment() {
addData(data) {
index < total // 判断是否有更多页
}
// addData(data, {
// addData(data, rv.bindingAdapter, isEmpty = {
// true // 此处判断是否存在下一页
// }, {
// }, hasMore = {
// false // 此处判断是否显示空布局
// })
}, 2000)
}, 2000)

toast("右上角菜单可以操作刷新结果, 默认2s结束")
}.autoRefresh()
Expand Down

0 comments on commit c5776fa

Please sign in to comment.