Skip to content

Commit

Permalink
feat: #412 支持全局点击防抖动
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Dec 16, 2023
1 parent 9ce8991 commit 347e3cc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Module 的 build.gradle 添加依赖框架

```groovy
dependencies {
implementation 'com.github.liangjingkanji:BRV:1.5.7'
implementation 'com.github.liangjingkanji:BRV:1.5.8'
}
```

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Then, add the framework dependency to the module's build.gradle file:

```groovy
dependencies {
implementation 'com.github.liangjingkanji:BRV:1.5.6'
implementation 'com.github.liangjingkanji:BRV:1.5.8'
}
```

Expand Down
4 changes: 2 additions & 2 deletions brv/src/main/java/com/drake/brv/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
}

/** 防抖动点击事件的间隔时间, 单位毫秒 */
var clickThrottle: Long = BRV.clickThrottle
var debounceClickInterval: Long = BRV.debounceClickInterval

/**
* 监听指定Id控件的点击事件, 包含防抖动
Expand Down Expand Up @@ -1096,7 +1096,7 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
(clickListener.value.first ?: onClick)?.invoke(this, it.id)
}
} else {
view.throttleClick(clickThrottle) {
view.setOnDebounceClickListener(debounceClickInterval) {
(clickListener.value.first ?: onClick)?.invoke(this@BindingViewHolder, id)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@
package com.drake.brv.listener

import android.view.View
import com.drake.brv.utils.BRV


internal fun View.throttleClick(period: Long = 500, block: View.() -> Unit) {
setOnClickListener(ThrottleClickListener(period, block))
internal fun View.setOnDebounceClickListener(interval: Long = 500, block: View.() -> Unit) {
setOnClickListener(OnDebounceClickListener(interval, block))
}

private class ThrottleClickListener(private val period: Long = 500, private var block: View.() -> Unit) :
View.OnClickListener {
private class OnDebounceClickListener(
private val interval: Long = 500,
private var block: View.() -> Unit
) : View.OnClickListener {

private var lastTime: Long = 0
private var _lastDebounceClickTime: Long = 0
private var lastDebounceClickTime: Long
get() = if (BRV.debounceGlobalEnabled) BRV.lastDebounceClickTime else _lastDebounceClickTime
set(value) = if (BRV.debounceGlobalEnabled) BRV.lastDebounceClickTime = value else _lastDebounceClickTime = value

override fun onClick(v: View) {
val currentTime = System.currentTimeMillis()
if (currentTime - lastTime > period) {
lastTime = currentTime
if (currentTime - lastDebounceClickTime > interval) {
lastDebounceClickTime = currentTime
block(v)
}
}
Expand Down
16 changes: 16 additions & 0 deletions brv/src/main/java/com/drake/brv/utils/BRV.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,21 @@ object BRV {
* 防抖动点击事件默认的间隔时间, 单位毫秒
* @see com.drake.brv.BindingAdapter.onClick
*/
var debounceClickInterval: Long = 500

@Deprecated("命名规范", ReplaceWith("BRV.debounceClickInterval"), DeprecationLevel.ERROR)
var clickThrottle: Long = 500

/**
* 启用item所有view点击事件共享防抖动间隔, BRV默认防抖动仅针对单个view
* @see com.drake.brv.BindingAdapter.onClick
*/
var debounceGlobalEnabled: Boolean = false

/**
* 此变量用于支持全局控件防抖动
* @see com.drake.brv.BindingAdapter.onClick
* @see debounceGlobalEnabled 要求先启用全局共享防抖动
*/
var lastDebounceClickTime: Long = 0
}
16 changes: 13 additions & 3 deletions docs/click.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,27 @@ BRV使用`onClick`函数设置监听事件即包含防抖动, 以下为修改防

=== "全局"
```kotlin
BRV.clickThrottle = 1000 // 单位毫秒
BRV.debounceClickInterval = 1000 // 单位毫秒
```

=== "单例"
```kotlin hl_lines="2"
binding.rv.linear().setup {
clickThrottle = 1000 // 覆盖全局设置
debounceClickInterval = 1000 // 覆盖全局设置

addType<SimpleModel>(R.layout.item_simple)
R.id.item.onClick {
toast("点击文本")
}
}.models = getData()
```
```
=== "共享防抖动"
```kotlin
// 启用item所有view点击事件共享防抖动间隔, BRV默认防抖动仅针对单个view
BRV.debounceGlobalEnabled = true
```

!!! question "全局防抖动"
使用`BRV.lastDebounceClickTime`来判断抖动间隔时间, 即可统一BRV和你实现的防抖动点击事件监听

- [防抖动示例](https://github.com/liangjingkanji/BRV/blob/master/brv/src/main/java/com/drake/brv/listener/OnDebounceClickListener.kt)
5 changes: 4 additions & 1 deletion docs/updates.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.5.8
- feat: #412 支持全局点击防抖动

## 1.5.7
fix: #409 修复单一展开模式下删除数据后展开导致崩溃
- fix: #409 修复单一展开模式下删除数据后展开导致崩溃

## 1.5.6
- fix: #402 修复Staggered布局+分割线添加数据崩溃
Expand Down

0 comments on commit 347e3cc

Please sign in to comment.