我有一个列表,我有搜索过滤器。我已经完成了使用EditText监听器和使用for循环。但我希望过滤器应该由rxjava处理
HttpStatus.NOT_FOUND
答案 0 :(得分:0)
以下是我用AndroidRx
搜索的一段代码。
注意:在此示例中,我在等待1000毫秒后对每个字符更改进行网络调用。
PublishSubject<String> subject = PublishSubject.create();
subject.debounce(1000, TimeUnit.MILLISECONDS)
.filter(new Predicate<String>() {
@Override
public boolean test(@NonNull String s) throws Exception {
return s.length() > 0;
}
})
.switchMap(new Function<String, Observable<SearchViewResponseModel>>() {
@Override
public Observable<SearchViewResponseModel> apply(@NonNull String o) throws Exception {
return api.getSearchResult(PrefsManager.getToken(SearchActivity.this), o, 0, 10);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<SearchViewResponseModel>() {
@Override
public void onNext(@NonNull SearchViewResponseModel s) {
adapter.clear();
for (SearchViewResponseModel.Data model : s.getData())
adapter.add(new SearchViewModel(model.get_id(), model.getThumbnail(), model.getTitle()));
}
@Override
public void onError(@NonNull Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
Log.i("Completed", "");
}
});
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(final String newText) {
subject.onNext(newText);
return true;
}
});
}