我正在尝试将所有UI事件合并到一个Observable中,因此我可以对所有事件做出反应,例如:onCreate生命周期,滚动来自我的RecyclerView的事件,来自搜索视图的searchEvents和来自Action模式激活的deleteEvents。我正在使用Rxbinding来实现这一目标。我正试图从Jake Wharton的演讲中实现Redux模式:http://jakewharton.com/the-state-of-managing-state-with-rxjava/
实际结果:只触发GetUsersEvent,而其他事件则不触发。
预期结果:适当时会触发所有事件。
提前致谢。 我的代码:
public Observable events;
@Override
public void onCreate(Bundle savedInstanceState) {
events = Observable.just(new GetUsersEvent());
//... boiler plate
events.mergeWith(RxRecyclerView.scrollEvents(userRecycler).map(recyclerViewScrollEvent -> {
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if ((layoutManager.getChildCount() + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0 && totalItemCount >= PAGE_SIZE) {
uiModel = new UserListState.Builder(uiModel)
.setUsers(uiModel.getUsers())
.setYScroll(firstVisibleItemPosition)
.build();
return new UsersNextPageEvent(uiModel.getLastId());
}
return null;
}));
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.selected_list_menu, menu);
events.mergeWith(RxMenuItem.clicks(menu.findItem(R.id.delete_item))
.map(click -> new DeleteUsersEvent(usersAdapter.getSelectedItemsIds())));
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView mSearchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
events.mergeWith(RxSearchView.queryTextChanges(mSearchView)
.map(query -> new SearchUsersEvent(query.toString()))
.doOnNext(searchUsersEvent -> Log.d("searchEvent", "eventFired")));
return super.onCreateOptionsMenu(menu);
}
@Override
public void onResume() {
super.onResume();
events.compose(userListVM.uiModels(mapEventsToActions, mapActionsToExecutables, stateReducer,
DeleteUsersEvent.class, GetUsersEvent.class, SearchUsersEvent.class, UsersNextPageEvent.class))
.compose(bindToLifecycle()).subscribe(new BaseSubscriber<>(this, ERROR_WITH_RETRY));
}
答案 0 :(得分:1)
嗯,你有很多events.mergeWith(...)
,但你对结果有什么看法?该操作为您提供了新的可观察性。
你可以events = events.mergeWith(...)
。