{"rsdb":{"rid":"386198","subhead":"","postdate":"0","aid":"269183","fid":"98","uid":"1","topic":"1","content":"
\n
\n

\u516c\u4f17\u53f7\u56de\u590d Compose<\/code> \u83b7\u53d6\u5b89\u88c5\u5305<\/p> \n

\u9879\u76ee\u5730\u5740\uff1a Wanandroid-Compose<\/a><\/p> \n <\/blockquote> \n

\u7ecf\u8fc7\u524d\u6bb5\u65f6\u95f4\u7684 Android Dev Summit \uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5927\u6982\u4e86\u89e3\u4e86 Jetpack Compose<\/strong> \u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u542c\u8bf4\u8fc7\uff0c\u53ef\u4ee5\u9605\u8bfb\u8fd9\u7bc7\u6587\u7ae0 Jetpack Compose \u6700\u65b0\u8fdb\u5c55<\/a> \u3002\u603b\u800c\u8a00\u4e4b\uff0cCompose \u662f\u4e00\u4e2a \u98a0\u8986\u6027<\/strong> \u7684 \u58f0\u660e\u5f0f UI \u6846\u67b6<\/strong> \uff0c\u5b83\u7684\u53e3\u53f7\u5c31\u662f \u6d88\u706d xml \u6587\u4ef6 \uff01<\/strong><\/p> \n

\u5c3d\u7ba1 Jetpack Compose \u8fd8\u53ea\u662f\u9884\u89c8\u7248\uff0cAPI \u53ef\u80fd\u53d1\u751f\u53d8\u5316\uff0c\u7f3a\u4e4f\u8db3\u591f\u7684\u63a7\u4ef6\u652f\u6301\uff0c\u751a\u81f3\u4e0d\u662f\u90a3\u4e48\u7a33\u5b9a\uff0c\u4f46\u8fd9\u963b\u6b62\u4e0d\u4e86\u6211\u8fd9\u9897\u597d\u5947\u7684\u5fc3\u3002\u6211\u5728\u7b2c\u4e00\u65f6\u95f4\u5c31\u4e0a\u624b\u64b8\u4e86\u4e00\u6b3e Compose \u7248\u672c Wanandroid \u5e94\u7528\uff0c\u529f\u80fd\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u4ec5\u5305\u62ec\u9996\u9875\uff0c\u5e7f\u544a\u548c\u6700\u65b0\u9879\u76ee\uff0c\u7c7b\u4f3c\u4e8e Android \u539f\u751f\u9875\u9762\u7684 Viewpager + TabLayout<\/code> \u3002\u4e0b\u9762\u7684 gif \u5c55\u793a\u4e86\u5e94\u7528\u7684\u57fa\u672c\u9875\u9762\uff1a<\/p> \n

<\/p> \n

\u53ef\u4ee5\u770b\u51fa\u6765\u9875\u9762\u5e76\u4e0d\u662f\u90a3\u4e48\u6d41\u7545\uff0cView \u7684\u590d\u7528\u5e94\u8be5\u662f\u4e2a\u95ee\u9898\uff0c\u751a\u81f3\u6211\u4e5f\u6ca1\u53d1\u73b0\u5e94\u8be5\u600e\u4e48\u505a\u4e0b\u62c9\u5237\u65b0\u3002\u90a3\u4e48\uff0cCompose \u7ed9\u6211\u4eec\u5e26\u6765\u4e86\u4ec0\u4e48\u5462\uff1f\u5728\u89e3\u7b54\u8fd9\u4e2a\u95ee\u9898\u4e4b\u524d\uff0c\u6211\u60f3\u5148\u6765\u8bf4\u8bf4 Android \u5e94\u7528\u67b6\u6784\u95ee\u9898\u3002<\/p> \n

\u8352\u829c\u5e74\u4ee3 \u2014\u2014 MVC<\/h2> \n

\u5728\u6211\u521a\u5165\u884c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8bf4\u662f Android \u5f00\u53d1\u7684\u9ec4\u91d1\u65f6\u4ee3\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u662f\u5f00\u53d1\u8005\u7684\u8352\u829c\u65f6\u4ee3\u3002\u4e00\u65b9\u9762\uff0c\u6beb\u4e0d\u5938\u5f20\u7684\u8bf4\uff0c\u57fa\u672c\u4f1a\u5199 xml \u90fd\u80fd\u8c0b\u5f97\u4e00\u4efd\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u8fdc\u8fdc\u6ca1\u6709\u73b0\u5728\u7684\u89c4\u8303\u7684\u5f00\u53d1\u67b6\u6784\u3002\u6ca1\u8bb0\u9519\u7684\u8bdd\uff0c\u6211\u5f53\u5e74\u7684\u4e3b\u529b\u5f00\u53d1\u6846\u67b6\u662f xUtils 2<\/code> \uff0c\u4e00\u4e2a\u7c7b\u5e93\u5927\u5305\u5e72\uff0c\u4ece \u5e03\u5c40\u548c ID \u7ed1\u5b9a\uff0c\u7f51\u7edc\u8bf7\u6c42\uff0c\u5230\u56fe\u7247\u5c55\u793a\uff0cORM \u64cd\u4f5c\uff0c\u4e00\u5e94\u4ff1\u5168\u3002\u5f53\u65f6\u7684 \u5e03\u5c40\u548c ID \u7ed1\u5b9a\u8fd8\u662f\u8fd0\u884c\u65f6\u53cd\u5c04\uff0c\u800c\u4e0d\u662f\u7f16\u8bd1\u671f\u6ce8\u89e3\u3002\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u4ee5\u6765\uff0cAndroid \u8fde\u4e00\u4e2a\u5b98\u65b9\u7684\u7f51\u7edc\u5e93\u90fd\u6ca1\u6709\u3002<\/p> \n

\u5728\u67b6\u6784\u65b9\u9762\uff0c\u5f88\u591a\u4eba\u90fd\u662f\u4e00\u4e2a Activity \u64b8\u5230\u6b7b\uff0c\u6211\u771f\u7684\u89c1\u8fc7\u4e0a\u5343\u884czouguolai\u7684 MainActivity \u3002\u5e76\u4e14\u89c9\u5f97\u8fd9\u5c31\u662f MVC \u67b6\u6784\uff0c\u5b9e\u4f53\u7c7b Entity \u5c31\u662f Model<\/code> \u5c42\uff0cActivity\/Fragment<\/code> \u5c31\u662f Controller<\/code> \u5c42\uff0c\u5e03\u5c40\u6587\u4ef6\u5c31\u662f View<\/code> \u5c42\u3002<\/p> \n

\u4f46\u8fd9\u5f53\u771f\u662f MVC<\/code> \u5417\uff1f\u5176\u5b9e\u5e76\u4e0d\u662f\u3002\u4e0d\u7ba1\u662f MVC<\/code>\uff0c MVP<\/code>\uff0c\u8fd8\u662f MVVM<\/code>\uff0c\u90fd\u5e94\u8be5\u9075\u5faa\u4e00\u4e2a\u6700\u8d77\u7801\u7684\u539f\u5219\uff0c\u8868\u73b0\u5c42\u548c\u4e1a\u52a1\u5c42\u5206\u79bb<\/strong> \uff0c\u4e5f\u5c31\u662f Android \u5b98\u7f51\u7ed9\u51fa\u7684 \u5e94\u7528\u67b6\u6784\u6307\u5357<\/a> \u4e2d\u5f3a\u8c03\u7684 \u5206\u79bb\u5173\u6ce8\u70b9<\/strong> \u3002Activity\/Fragment<\/code> \u65e2\u8981\u627f\u62c5\u89c6\u56fe\u5c42\u7684\u4efb\u52a1\uff0c\u5c55\u793a\u548c\u66f4\u65b0 UI\uff0c\u53c8\u8981\u5904\u7406\u4e1a\u52a1\u5c42\u903b\u8f91\uff0c\u83b7\u53d6\u6570\u636e\u7b49\u3002\u8fd9\u5e76\u4e0d\u7b26\u5408\u67b6\u6784\u8bbe\u8ba1\u7684\u57fa\u672c\u539f\u5219\u3002<\/p> \n

\u6b63\u786e\u7684 MVC \u6a21\u5f0f\u4e2d\uff0cModel \u5c42\u4e0d\u4ec5\u5305\u542b\u5b9e\u4f53\u7c7b Entity\uff0c\u66f4\u91cd\u8981\u7684\u4f5c\u7528\u662f\u5904\u7406\u4e1a\u52a1\u903b\u8f91\u3002View \u5c42\u8d1f\u8d23\u5904\u7406\u89c6\u56fe\u903b\u8f91\u3002\u800c Controller \u5c31\u662f Model \u548c View \u4e4b\u95f4\u7684\u6865\u6881\u3002\u6865\u600e\u4e48\u5efa\uff0c\u5176\u5b9e\u5e76\u6ca1\u6709\u6807\u51c6\uff0c\u6839\u636e\u4f60\u81ea\u5df1\u7684\u9700\u6c42\u5c31\u53ef\u4ee5\u4e86\u3002<\/p> \n

<\/p> \n

\u5f15\u7528\u4e00\u5f20\u962e\u4e00\u5cf0\u8001\u5e08\u7684\u56fe\uff0c\u5927\u81f4\u662f\u8fd9\u4e48\u4e2a\u610f\u601d\uff0c\u4f46\u662f\u4e5f\u4e0d\u4e00\u5b9a\u5c31\u5b8c\u5168\u90fd\u662f\u5355\u5411\u4f9d\u8d56\u3002<\/p> \n

\u4ece\u8352\u829c\u65f6\u4ee3\u8d70\u8fc7\u6765\uff0cMVC \u603b\u7b97\u6709\u70b9\u5206\u5c42\u7684\u5473\u9053\u5728\u91cc\u9762\u4e86\uff0c\u5206\u79bb\u4e86\u89c6\u56fe\u5c42\u548c\u4e1a\u52a1\u5c42\u3002\u4f46\u662f View \u5c42\u548c Model \u5c42\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u9020\u6210\u4ee3\u7801\u8026\u5408\uff0c\u7ec8\u5c06\u5bfc\u81f4 Activity \u65e5\u76ca\u81c3\u80bf\u3002\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u5c06 View \u5c42\u548c Model \u5c42\u5f7b\u5e95\u5206\u79bb\uff0c\u505a\u5230\u89c6\u56fe\u5c42\u548c\u6a21\u578b\u5c42\u5b8c\u5168\u5206\u79bb\u5462\uff1f MVP<\/strong> \u5c31\u5e94\u8fd0\u800c\u751f\u4e86\u3002<\/p> \n

\u9752\u94dc\u5e74\u4ee3 \u2014\u2014 MVP<\/h2> \n

\u4f9d\u65e7\u662f\u962e\u4e00\u5cf0\u8001\u5e08\u7684\u56fe\u7247\uff1a<\/p> \n

<\/p> \n

\u76f8\u8f83\u4e8e MVC \uff0cMVP \u7528 Presenter \u5c42<\/code> \u4ee3\u66ff\u4e86 Controller \u5c42<\/code> \uff0c\u4e14 View \u5c42<\/code> \u548c Model \u5c42<\/code> \u5b8c\u5168\u5206\u79bb\uff0c\u4f9d\u9760 Presenter \u8fdb\u884c\u901a\u4fe1 \u3002<\/p> \n

\u60f3\u8c61\u4e00\u4e2a\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u7684\u573a\u666f\u3002IView<\/code> \u63a5\u53e3\u4e2d\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u89c6\u56fe\u5c42\u63a5\u53e3 \uff0cView \u5c42\uff08Activity\uff09\u5b9e\u73b0 IView<\/code> \u63a5\u53e3\u4e2d\u76f8\u5e94\u89c6\u56fe\u903b\u8f91\u3002 View \u5c42\u901a\u8fc7\u6301\u6709\u7684 Presenter \u5904\u7406\u4e1a\u52a1\u903b\u8f91\uff0c\u5373\u8bf7\u6c42\u7528\u6237\u4fe1\u606f\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cPresenter \u4e5f\u4e0d\u76f4\u63a5\u5904\u7406\u4e1a\u52a1\u903b\u8f91\uff0c\u800c\u662f\u901a\u8fc7 Model \u5c42\uff0c\u4f8b\u5982\u6570\u636e\u4ed3\u5e93 Repository\uff0c \u6765\u83b7\u53d6\u6570\u636e\uff0c\u907f\u514d Presenter \u91cd\u8e48\u8986\u8f99\uff0c\u65e5\u6e10\u81c3\u80bf\u3002\u540c\u65f6\uff0cPresenter \u5c42\u4e5f\u662f\u6301\u6709 VIew \u7684\uff0c\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u4e4b\u540e\u518d\u8f6c\u53d1\u7ed9 View \u3002<\/p> \n

\u603b\u7ed3\u4e00\u4e0b\uff0cMVP \u4e2d View \u548c Model \u5b8c\u5168\u89e3\u8026\uff0c\u901a\u8fc7 Presenter \u901a\u4fe1\u3002View \u548c Presenter \u5171\u540c\u5904\u7406\u89c6\u56fe\u5c42\u903b\u8f91\uff0cModel \u5c42\u8d1f\u8d23\u4e1a\u52a1\u903b\u8f91\u3002<\/p> \n

\u5728 Github \u4e0a Android \u5b98\u65b9\u7684\u67b6\u6784\u793a\u4f8b architecture-samples<\/a> \u4e2d MVP \u4f5c\u4e3a\u4e3b\u5206\u652f\u575a\u633a\u4e86\u5f88\u4e45\u3002\u6211\u6700\u521d\u4e5f\u662f\u6839\u636e\u8fd9\u4e2a\u5b98\u65b9\u793a\u4f8b\u6539\u9020\u4e86\u81ea\u5df1\u7684 MVP \u67b6\u6784\uff0c\u5e76\u4e14\u4f7f\u7528\u4e86\u5f88\u957f\u65f6\u95f4\u3002\u4f46\u662f MVP \u4f5c\u4e3a\u4e00\u6b3e\u9762\u5411\u63a5\u53e3\u7f16\u7a0b<\/a>\u7684\u67b6\u6784\uff0c\u968f\u7740\u4e1a\u52a1\u7684\u590d\u6742\u7a0b\u5ea6\u4e0d\u65ad\u52a0\u5927\uff0c\u6709\u79cd\u904d\u5730\u90fd\u662f\u63a5\u53e3\u7684\u65e2\u89c6\u611f\uff0c\u5b9e\u5728\u663e\u5f97\u6709\u70b9\u7e41\u7410\u3002<\/p> \n

\u53e6\u5916\u4e00\u70b9\uff0cPresenter \u7684\u804c\u8d23\u8fb9\u754c\u4e0d\u591f\u6e05\u6670\uff0c\u5b83\u9664\u4e86\u627f\u62c5\u8c03\u7528 Model \u5c42\u83b7\u53d6\u4e1a\u52a1\u903b\u8f91\u4e4b\u5916\uff0c\u8fd8\u8981\u63a7\u5236 View \u5c42\u5904\u7406 UI\u3002\u7528\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\u8868\u793a\u4e00\u4e0b\uff1a<\/p> \n

class LoginPresenter(private val mView: LoginContract.View) : LoginContract.Presenter {\n\n    ......\n\n    override fun login(userName: String, passWord: String) {\n        CoroutineScope(Dispatchers.Main).launch {\n            val result = WanRetrofitClient.service.login(userName, passWord).await()\n            with(result) {\n                if (errorCode == -1)  mView.loginError(errorMsg) else mView.login(data)\n            }\n        }\n    }\n}<\/code><\/pre> \n 

\u4e00\u65e6 View \u5c42\u53d1\u751f\u4efb\u4f55\u53d8\u5316\uff0cPresenter \u5c42\u4e5f\u8981\u505a\u51fa\u76f8\u5e94\u6539\u52a8\u3002\u867d\u7136 View \u548c Model \u4e4b\u95f4\u89e3\u8026\u4e86\uff0c\u4f46\u662f View \u548c Presenter \u5374\u8026\u5408\u4e86\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0cPresenter \u5c42\u5e94\u8be5\u4ec5\u8d1f\u8d23\u6570\u636e\u7684\u83b7\u53d6\uff0cView \u5c42\u81ea\u52a8\u89c2\u5bdf\u6570\u636e\u7684\u53d8\u5316\u3002\u4e8e\u662f\uff0cMVVM<\/strong> \u6765\u4e86\u3002<\/p> \n

\u9ec4\u91d1\u65f6\u4ee3 \u2014\u2014 MVVM<\/h2> \n

Google \u5b98\u56fe\u9547\u697c \u3002<\/p> \n

<\/p> \n

MVP \u98ce\u5149\u65e9\u5df2\u4e0d\u5728\uff0c Android \u5b98\u65b9\u7684\u67b6\u6784\u793a\u4f8b architecture-samples<\/a> \u7684\u4e3b\u5206\u652f\u5df2\u7ecf\u5207\u6362\u5230 MVVM \u3002\u5728 Android \u7684 MVVM \u67b6\u6784\u4e2d\uff0cViewModel<\/strong> \u662f\u91cd\u4e2d\u4e4b\u91cd\uff0c\u5b83\u4e00\u65b9\u9762\u901a\u8fc7\u6570\u636e\u4ed3\u5e93 Repository \u83b7\u53d6\u6570\u636e\uff0c\u53e6\u4e00\u65b9\u9762\u6839\u636e\u83b7\u53d6\u7684\u6570\u636e\u66f4\u65b0 View \u5c42\u7684 Activity\/Fragment\u3002\u7b49\u7b49\uff0c\u8fd9\u53e5\u8bdd\u600e\u4e48\u542c\u7740\u8fd9\u4e48\u8033\u719f\uff0cPresenter \u4e0d\u4e5f\u662f\u5e72\u4e86\u8fd9\u4e9b\u4e8b\u5417\uff1f\u7684\u786e\uff0c\u5b83\u4eec\u5e72\u7684\u4e8b\u60c5\u90fd\u5dee\u4e0d\u591a\uff0c\u4f46\u662f\u5b9e\u73b0\u4e0a\u5b8c\u5168\u4e0d\u4e00\u6837\u3002<\/p> \n

\u4ee5\u6211\u7684\u5f00\u6e90\u9879\u76ee Wanandroid<\/a> \u4e2d\u7684 LoginViewModel<\/code> \u4e3a\u4f8b\uff1a<\/p> \n

class LoginViewModel(val repository: LoginRepository) : BaseViewModel() {\n\n    private val _uiState = MutableLiveData<LoginUiModel>()\n    val uiState: LiveData<LoginUiModel>\n        get() = _uiState\n\n\n    fun loginDataChanged(userName: String, passWord: String) {\n        emitUiState(enableLoginButton = isInputValid(userName, passWord))\n    }\n\n    \/\/ ViewModel \u53ea\u5904\u7406\u89c6\u56fe\u903b\u8f91\uff0c\u6570\u636e\u4ed3\u5e93 Repository \u8d1f\u8d23\u4e1a\u52a1\u903b\u8f91\n    fun login(userName: String, passWord: String) {\n        viewModelScope.","orderid":"0","title":"Jetpack Compse \u5b9e\u6218 \u2014\u2014 \u5168\u65b0\u7684\u5f00\u53d1\u4f53\u9a8c(\u4e00)","smalltitle":"","mid":"0","fname":"Android","special_id":"0","bak_id":"0","info":"0","hits":"32","pages":"3","comments":"0","posttime":"2019-11-18 11:20:36","list":"1574047236","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Jetpack<\/A> Compse<\/A> \u5b9e\u6218<\/A> \u5168\u65b0<\/A> \u5f00\u53d1<\/A> \u4f53\u9a8c<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.35","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Jetpack Compse \u5b9e\u6218 \u2014\u2014 \u5168\u65b0\u7684\u5f00\u53d1\u4f53\u9a8c","lastview":"1576148499","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}