观察EditText上的文本更改 -
RxTextView.textChangeEvents(editText)
.subscribe(e -> log(e.text().toString()));
过滤列表 -
Observable.from(itemList)
.filter(item-> item.getName().toLowerCase().contains(search.toLowerCase()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Item>() {
@Override
public void onCompleted() {
Utils.crossfade(mProgressView, recyclerHotelOption);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Item item) {
if (mItemListAdapter == null) {
List<Item> itemList = new ArrayList<>();
itemList.add(item);
mItemListAdapter = new ItemListAdapter(mActivity, itemList);
recyclerHotelOption.setAdapter(mItemListAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerHotelOption.setLayoutManager(linearLayoutManager);
} else {
mItemListAdapter.notifyDataSetChanged(item);
recyclerHotelOption.getLayoutManager().scrollToPosition(0);
}
}
});
如何将两者结合起来过滤固定的项目清单?
更新
我根据答案更改了代码,但它仍无效。
RxSearchView.queryTextChangeEvents(mSearchView)
.map(textViewTextChangeEvent -> textViewTextChangeEvent.toString().toLowerCase())
.switchMap(s -> Observable.from(items)
.filter(item-> item.getItemName().toLowerCase().contains(s.toLowerCase()))
.toList()).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Item>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(List<Item> items) {
mItemListAdapter.notifyDataSetChanged(items);
recyclerView.getLayoutManager().scrollToPosition(0);
}
});
答案 0 :(得分:6)
我会按以下方式进行:
Observable<CharSequence> textChanges = RxTextView.textChangeEvents(editText);
textChanges
.map(search -> search.toString().toLowerCase())
.switchMap(search ->
Observable.from(itemList)
.filter(item -> item.getName().toLowerCase().contains(search))
.toList()
)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Observer<List<Item>>() {
// ...
});