首先,說放棄 MVP,肯定是夸大其詞了。MVP 很好,只是個人不習慣那么多的回調(diào),更喜歡 Flux 這種單向數(shù)據(jù)流模式。希望大家能多多點贊,多多拍磚!
下面以 com.huyingbao.simpel.main 包中的類作為講解,主要實現(xiàn) 主頁面->列表頁面->用戶信息頁面 跳轉。
一、包結構

image.png
Views
MainActivty:是模塊內(nèi)唯一 Acitvity,對應 MainStore,負責容納Fragment,控制 Fragment 之間跳轉。
MainFragment:主頁面,只有一個 button,點擊跳轉到列表頁面
GitRepoListFragment。GitRepoListFragment:列表頁面,展示網(wǎng)絡接口獲取回來的列表數(shù)據(jù),點擊其中一條數(shù)據(jù),跳轉到用戶信息頁面 GitUserFragment。
GitUserFragment:用戶信息頁面,展示根據(jù) userId獲取的用戶信息。
Store
- MainStore 模塊主 store,原則上只有一個。主要用來接收發(fā)送的 RxAction(Views 通過 ActionCreator 及其封裝的 Dispatcher 發(fā)送),封裝 RxAction 成 RxStoreChange,并發(fā)送(通過封裝的Dispatcher發(fā)送)。
其他
- GitRepoAdapter:RecyclerVIew適配器。極其推薦BaseRecyclerViewAdapterHelper。
- GitRepro、GitUser :model類,接口返回Json數(shù)據(jù)解析類。
二、業(yè)務流程實現(xiàn)
-
MainFragment 跳轉到 GitRepoListFragment
- MainFragment 中通過 mActionCreator 發(fā)送包含跳轉請求的 RxAction。
/**
* 到用戶信息頁面
*/
@OnClick(R.id.btn_main_to_list)
public void toGitRepoList() {
mActionCreator.postLocalAction(Actions.TO_GIT_REPO_LIST);
}
- BaseRxActionCreator 中 postLocalAction() 方法實現(xiàn),已經(jīng)封裝好,不需要修改。
/**
* 發(fā)送LocalAction
*
* @param actionId
* @param data
*/
public void postLocalAction(@NonNull String actionId, @NonNull Object... data) {
postRxAction(newRxAction(actionId, data));
}
- MainStore 在 onRxAction() 方法中接收包含跳轉請求 RxAction,通過 switch 語句判斷具體執(zhí)行什么后續(xù)操作,如果是當前模塊中需要處理邏輯,執(zhí)行 postChange() 方法,發(fā)送 RxStoreChange。
@Override
public void onRxAction(RxAction rxAction) {
switch (rxAction.getType()) {
...
case Actions.Actions.TO_GIT_REPO_LIST:
break;
default://此處不能省略,不是本模塊的邏輯,直接返回,不發(fā)送RxStoreChange
return;
}
postChange(new RxStoreChange(getClass().getSimpleName(), rxAction));
}
- MainActivity 在onRxStoreChanged() 方法中接收 RxStoreChange,獲取 RxStoreChange 中的 RxAction,通過 switch 語句判斷業(yè)務邏輯,控制跳轉到 GitRepoListFragment。
@Override
public void onRxStoreChanged(@NonNull RxStoreChange rxStoreChange) {
RxAction rxAction = rxStoreChange.getRxAction();
switch (rxAction.getType()) {
case Actions.TO_GIT_REPO_LIST:
toGitRepoList();
break;
...
}
}
/**
* 到列表頁面
*/
private void toGitRepoList() {
getFragmentTransaction(R.id.fl_content)
.add(R.id.fl_content, GitRepoListFragment.newInstance())
.commit();
}
注意: MainFragment 不需要響應 RxStoreChange,只是繼承 BaseFragment,沒有繼承BaseRxFluxFragment(實現(xiàn)RxViewDispatch 接口)。
-
GitRepoListFragment 獲取列表數(shù)據(jù)
GitRepoListFragment 繼承 BaseRxFluxListFragment,一個封裝了 RecyclerView 的 BaseRxFluxFragment,大家可以看看,但是可能不符合自己項目需要。
-
GitUserFragment 獲取用戶信息
- GitUserFragment 中調(diào)用 mActionCreator 中的 getGitUser() 方法獲取接口數(shù)據(jù)。
@Override
public void afterCreate(Bundle savedInstanceState) {
initActionBar("用戶信息");//Fragment 控制 Activity的ToolBar 展示
mActionCreator.getGitUser(mContext, getArguments().getInt(ActionsKeys.USER_ID));
}
- ActionCreator 中 getGitUser() 方法實現(xiàn)。
@Override
public void getGitUser(Context context, int userId) {
RxAction action = newRxAction(GET_GIT_USER);
postLoadingHttpAction(context,action, mHttpApi.getUser(userId));
}
- MainStore 在 onRxAction() 方法中接收包含接口返回數(shù)據(jù)的 RxAction,通過 switch 語句判斷具體執(zhí)行什么后續(xù)操作,如果是當前模塊中需要處理邏輯,緩存接口返回數(shù)據(jù),執(zhí)行 postChange() 方法,發(fā)送 RxStoreChange。
@Override
public void onRxAction(RxAction rxAction) {
switch (rxAction.getType()) {
...
case Actions.GET_GIT_USER:
mGitUser = rxAction.get(ActionsKeys.RESPONSE);
break;
...
default://此處不能省略,不是本模塊的邏輯,直接返回,不發(fā)送RxStoreChange
return;
}
postChange(new RxStoreChange(getClass().getSimpleName(), rxAction));
}
public GitUser getGitUser() {//供view使用
return mGitUser;
}
- GitUserFragment 在onRxStoreChanged() 方法中接收 RxStoreChange,獲取 RxStoreChange 中的 RxAction,通過 switch 語句判斷業(yè)務邏輯,從 MainStore 獲取數(shù)據(jù)并顯示。
@Override
public void onRxStoreChanged(@NonNull RxStoreChange rxStoreChange) {
switch (rxStoreChange.getRxAction().getType()) {
case Actions.GET_GIT_USER:
showGitUserInfo(mStore.getGitUser());
break;
}
}
/**
* 顯示用戶信息
*
* @param gitUser
*/
private void showGitUserInfo(GitUser gitUser) {
mTvGitUserLogin.setText(gitUser.getLogin());
mTvGitUserName.setText(gitUser.getName());
mTvGitUserStatistics.setText(gitUser.getName());
}
三、總結
- MainStore 通過 Dagger2 注入到模塊中需要用的 View 中。
- ActionCreator 通過 Dagger2 注入到 Base View中。
- 按流程用就好,僅僅是部分業(yè)務邏輯需要自定義方法實現(xiàn),代碼清晰明確。
全局用法請等待,等不及的直接看源碼...