在我开始学习RxJava之前,我创建了一个带有Handler
的DelayAutoCompleteTextView,它处理了TextChanged事件。现在我重写代码,使用Rx。但我不能像以前那样做出同样的行为。
代码Handler
@Override
protected void performFiltering(CharSequence text, int keyCode) {
// remove pending query, because we have new one
mHandler.removeMessages(MESSAGE_TEXT_CHANGED);
// send with delay, to prevent big amount of network requests
// by removing unnecessary ones
mHandler.sendMessageDelayed(mHandler
.obtainMessage(MESSAGE_TEXT_CHANGED, text), mAutoCompleteDelay);
}
我的代码与RxJava:
getTextChangedObservable(searchView) // onNext(str) in onQueryTextChanged
// poor copy of old implementation
.throttleLast(750, TimeUnit.MILLISECONDS)
.distinctUntilChanged() // prevent duplicates
... // process
谢谢,
安东
答案 0 :(得分:2)
根据您的代码,看起来您正在寻找debounce运算符:
getTextChangedObservable(searchView)
.debounce(750, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.distinctUntilChanged()
.subscribe(query -> {
Log.d("Search", "Query " + query);
});