Ban*_*vic 9 android coroutine kotlin kotlin-coroutines
我需要使 EditText 具有自动建议功能,并且我需要听取其输入。以编程方式设置时,我还需要忽略 EditText 更改。
想知道是否有解决方案可以在这种情况下使用 Coroutines 使 EditText 去抖动而不使用延迟。
Pet*_*Học 29
将文本更改事件转为 Flow
使用 kotlinx.coroutines 1.5.0
@ExperimentalCoroutinesApi
@CheckResult
fun EditText.textChanges(): Flow<CharSequence?> {
return callbackFlow<CharSequence?> {
val listener = object : TextWatcher {
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
trySend(s)
}
}
addTextChangedListener(listener)
awaitClose { removeTextChangedListener(listener) }
}.onStart { emit(text) }
}
Run Code Online (Sandbox Code Playgroud)
并使用:
editText.textChanges().debounce(300)
.onEach { ... }
.launchIn(lifecycleScope)
Run Code Online (Sandbox Code Playgroud)
和这样的事情:
fun executeSearch(term: String): Flow<SearchResult> { ... }
editText.textChanges()
.distinctUntilChanged()
.filterNot { it.isNullOrBlank() }
.debounce(300)
.flatMapLatest { executeSearch(it) }
.onEach { updateUI(it) }
.launchIn(lifecycleScope)
Run Code Online (Sandbox Code Playgroud)
源代码:https : //github.com/Kotlin-Android-Open-Source/MVI-Coroutines-Flow/blob/master/core/src/main/java/com/hoc/flowmvi/core/FlowBinding.kt#L116
在对协程和 Flow 进行了一些研究之后,我提出了创建自定义 EditText 的解决方案,该解决方案在其中保存去抖逻辑,并使我能够附加去抖 TextWatcher 并在需要时将其删除。这是我的解决方案的代码
class DebounceEditText @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatEditText(context, attributeSet, defStyleAttr) {
private val debouncePeriod = 600L
private var searchJob: Job? = null
@FlowPreview
@ExperimentalCoroutinesApi
fun setOnDebounceTextWatcher(lifecycle: Lifecycle, onDebounceAction: (String) -> Unit) {
searchJob?.cancel()
searchJob = onDebounceTextChanged()
.debounce(debouncePeriod)
.onEach { onDebounceAction(it) }
.launchIn(lifecycle.coroutineScope)
}
fun removeOnDebounceTextWatcher() {
searchJob?.cancel()
}
@ExperimentalCoroutinesApi
private fun onDebounceTextChanged(): Flow<String> = channelFlow {
val textWatcher = object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(p0: Editable?) {
offer(p0.toString())
}
}
addTextChangedListener(textWatcher)
awaitClose {
removeTextChangedListener(textWatcher)
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我想激活 Debounce TextWatcher 时,我只需调用
// lifecycle is passed from Activity/Fragment lifecycle, because we want to relate Coroutine lifecycle with the one DebounceEditText belongs
debounceEditText.setOnDebounceTextWatcher(lifecycle) { input ->
Log.d("DebounceEditText", input)
}
Run Code Online (Sandbox Code Playgroud)
我在 xml 中实现 DebounceEditText 时遇到了焦点问题,因此我必须将 clickable、selectable 和 focusableInTouchMode 设置为 true。
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)
如果我想在 DebounceEditText 中设置输入而不触发,只需通过调用删除 TextWatcher
debounceEditText.removeOnDebounceTextWatcher()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4215 次 |
| 最近记录: |