本文整理了Java中rx.Observable.debounce()
方法的一些代码示例,展示了Observable.debounce()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.debounce()
方法的具体详情如下:
包路径:rx.Observable
类名称:Observable
方法名:debounce
[英]Returns an Observable that mirrors the source Observable, except that it drops items emitted by the source Observable that are followed by newer items before a timeout value expires. The timer resets on each emission.
Note: If items keep being emitted by the source Observable faster than the timeout then no items will be emitted by the resulting Observable.
Information on debounce vs throttle:
代码示例来源:origin: BaronZ88/MinimalistWeather
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
RxSearchView.queryTextChanges(searchView)
.map(charSequence -> charSequence == null ? null : charSequence.toString().trim())
.throttleLast(100, TimeUnit.MILLISECONDS)
.debounce(100, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(searchText -> selectCityFragment.cityListAdapter.getFilter().filter(searchText));
return true;
}
return super.onOptionsItemSelected(item);
}
代码示例来源:origin: THEONE10211024/RxJavaSamples
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
_setupLogger();
_subscription = RxTextView.textChangeEvents(_inputSearchText)//
.debounce(400, TimeUnit.MILLISECONDS)// default Scheduler is Computation
.observeOn(AndroidSchedulers.mainThread())//
.subscribe(_getSearchObserver());
}
代码示例来源:origin: cn-ljb/rxjava_for_android
.debounce(150, TimeUnit.MILLISECONDS) //debounce:每次文本更改后有150毫秒的缓冲时间,默认在computation调度器
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Action1<TextViewTextChangeEvent>() {
代码示例来源:origin: jhusain/learnrxjava
public static void main(String args[]) {
// debounce to the last value in each burst
//intermittentBursts().debounce(10, TimeUnit.MILLISECONDS).toBlocking().forEach(System.out::println);
/* The following will emit a buffered list as it is debounced */
// first we multicast the stream ... using refCount so it handles the subscribe/unsubscribe
Observable<Integer> burstStream = intermittentBursts().take(20).publish().refCount();
// then we get the debounced version
Observable<Integer> debounced = burstStream.debounce(10, TimeUnit.MILLISECONDS);
// then the buffered one that uses the debounced stream to demark window start/stop
Observable<List<Integer>> buffered = burstStream.buffer(debounced);
// then we subscribe to the buffered stream so it does what we want
buffered.toBlocking().forEach(System.out::println);
}
代码示例来源:origin: THEONE10211024/RxJavaSamples
@Override
public void onStart() {
super.onStart();
_subscriptions = new CompositeSubscription();
Observable<Object> tapEventEmitter = _rxBus.toObserverable().share();
_subscriptions//
.add(tapEventEmitter.subscribe(new Action1<Object>() {
@Override
public void call(Object event) {
if (event instanceof RxBusDemoFragment.TapEvent) {
_showTapText();
}
}
}));
Observable<Object> debouncedEmitter = tapEventEmitter.debounce(1, TimeUnit.SECONDS);
Observable<List<Object>> debouncedBufferEmitter = tapEventEmitter.buffer(debouncedEmitter);
_subscriptions//
.add(debouncedBufferEmitter//
.observeOn(AndroidSchedulers.mainThread())//
.subscribe(new Action1<List<Object>>() {
@Override
public void call(List<Object> taps) {
_showTapCount(taps.size());
}
}));
}
代码示例来源:origin: SmartDengg/sug-debounce
@Override public Object call(Object observable) {
return ((Observable) observable).debounce(time, unit)
.observeOn(AndroidSchedulers.mainThread());
}
}
代码示例来源:origin: leeowenowen/rxjava-examples
@Override
public void run() {
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(1);
sleep(500);
subscriber.onNext(2);
sleep(1000);
subscriber.onNext(3);
sleep(2000);
subscriber.onNext(4);
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.newThread())
.debounce(1, TimeUnit.SECONDS)
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
log(integer);
}
});
}
});
代码示例来源:origin: androidmalin/RengwuxianRxjava
/**
* EditText,每隔500ms,去响应变化
*/
private void method18() {
mSearchEditText.setVisibility(View.VISIBLE);
RxTextView.textChangeEvents(mSearchEditText)
.debounce(500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<TextViewTextChangeEvent>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
String changedMessage = textViewTextChangeEvent.text().toString();
Logger.d(TAG, changedMessage);
if (!TextUtils.isEmpty(changedMessage)) {
ToastUtil.getInstance().showToast(MainActivity.this, changedMessage);
}
}
});
}
代码示例来源:origin: com.netflix.rxjava/rxjava-core
return debounce(timeout, unit, Schedulers.computation());
代码示例来源:origin: eo/mdx
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_icons, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint(getString(R.string.icon_search_hint));
// reset max width to device width to make search view full width
searchView.setMaxWidth(getResources().getDisplayMetrics().widthPixels);
RxSearchView.queryTextChanges(searchView)
.debounce(200, TimeUnit.MILLISECONDS)
.skip(1) // skip initial unnecessary change
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence query) {
mIconsPresenter.loadIcons(query);
}
});
}
代码示例来源:origin: gitskarios/GithubAndroidSdk
public Observable<K> observable() {
return getApiObservable(getRestAdapter()).retry(this::retry).debounce(100, TimeUnit.MILLISECONDS);
}
代码示例来源:origin: chiclaim/AndroidMVVMSample
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SearchDebounceBinding binding = DataBindingUtil.setContentView(this,
R.layout.activity_search_debounce);
adapter = new SearchAdapter(this);
RecyclerViewUtils.setLinearManagerAndAdapter(binding.recyclerView, adapter);
binding.recyclerView.addItemDecoration(DividerItemDecoration.newVertical(this,
R.dimen.list_divider_height, R.color.divider_color));
//===========================@TODO
//1,避免EditText没改变一次就请求一次.
//2,避免频繁的请求,多个导致结果顺序错乱,最终的结果也就有问题.
// 但是对于第二个问题,也不能彻底的解决. 比如停止输入400毫秒后,
// 那么肯定会开始请求Search接口, 但是用户又会输入新的关键字,
// 这个时候上个请求还没有返回, 新的请求又去请求Search接口.
// 这个时候有可能最后的一个请求返回, 第一个请求最后返回,导致搜索结果不是想要的.
//===========================@TODO
subscription = RxTextView.textChangeEvents(binding.inputSearch)
.debounce(400, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getSearchObserver());
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_225() throws Exception {
Observable<BigDecimal> prices = tradingPlatform.pricesOf("NFLX");
Observable<BigDecimal> debounced = prices.debounce(100, MILLISECONDS);
prices
.debounce(x -> {
boolean goodPrice = x.compareTo(BigDecimal.valueOf(150)) > 0;
return Observable
.empty()
.delay(goodPrice? 10 : 100, MILLISECONDS);
});
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_242() throws Exception {
Observable
.interval(99, MILLISECONDS)
.debounce(100, MILLISECONDS);
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_60() throws Exception {
final Observable<Long> upstream = Observable.interval(99, MILLISECONDS);
upstream
.debounce(100, MILLISECONDS)
.timeout(1, SECONDS, upstream
.take(1)
.concatWith(
upstream.debounce(100, MILLISECONDS)));
}
代码示例来源:origin: SmartDengg/RxBlur
@Override public void setListener() {
Observable<TextViewTextChangeEvent> radiusObservable =
RxTextView.textChangeEvents(radiusEt).skip(1);
Observable<TextViewTextChangeEvent> durationObservable =
RxTextView.textChangeEvents(durationEt).skip(1);
Observable.combineLatest(radiusObservable, durationObservable,
new Func2<TextViewTextChangeEvent, TextViewTextChangeEvent, Boolean>() {
@Override public Boolean call(TextViewTextChangeEvent radiusEvent,
TextViewTextChangeEvent durationEvent) {
radius = radiusEvent.text().toString();
duration = durationEvent.text().toString();
return !TextUtils.isEmpty(radius) && !TextUtils.isEmpty(duration);
}
})
.debounce(300, TimeUnit.MILLISECONDS)
.compose(RxAnimatorBlurActivity.this.<Boolean>bindUntilEvent(ActivityEvent.DESTROY))
.observeOn(AndroidSchedulers.mainThread())
.startWith(false)
.subscribe(new Action1<Boolean>() {
@Override public void call(Boolean aBoolean) {
blurBtn.setEnabled(aBoolean);
}
});
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_249() throws Exception {
Observable
.interval(99, MILLISECONDS)
.debounce(100, MILLISECONDS)
.timeout(1, SECONDS);
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_72() throws Exception {
final Observable<Long> upstream = Observable.interval(99, MILLISECONDS);
upstream
.debounce(100, MILLISECONDS)
.timeout(1, SECONDS, upstream
.take(1)
.concatWith(
upstream
.debounce(100, MILLISECONDS)
.timeout(1, SECONDS, upstream)));
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
@Test
public void sample_121() throws Exception {
EditText latText = null;//...
EditText lonText = null;//...
Observable<Double> latChanges = RxTextView
.afterTextChangeEvents(latText)
.flatMap(toDouble());
Observable<Double> lonChanges = RxTextView
.afterTextChangeEvents(lonText)
.flatMap(toDouble());
Observable<Cities> cities = Observable
.combineLatest(latChanges, lonChanges, toPair())
.debounce(1, TimeUnit.SECONDS)
.flatMap(listCitiesNear());
}
代码示例来源:origin: nurkiewicz/rxjava-book-examples
Observable<Long> timedDebounce(Observable<Long> upstream) {
Observable<Long> onTimeout = upstream
.take(1)
.concatWith(defer(() -> timedDebounce(upstream)));
return upstream
.debounce(100, MILLISECONDS)
.timeout(1, SECONDS, onTimeout);
}
内容来源于网络,如有侵权,请联系作者删除!