{"id":166,"date":"2023-12-12T17:46:18","date_gmt":"2023-12-12T09:46:18","guid":{"rendered":"https:\/\/www.jishuge.cn\/?p=166"},"modified":"2023-12-12T17:48:02","modified_gmt":"2023-12-12T09:48:02","slug":"livedata%e5%92%8cviewmodel%e8%81%94%e5%90%88%e4%bd%bf%e7%94%a8%ef%bc%8c%e7%ae%80%e5%8d%95%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/blog.jishuge.cn\/?p=166","title":{"rendered":"LiveData\u548cViewModel\u8054\u5408\u4f7f\u7528\uff0c\u7b80\u5355\u5165\u95e8"},"content":{"rendered":"\n<p>\u6211\u4eec\u5f97\u9996\u5148\u660e\u767dViewModel\u53ea\u662f\u4e00\u4e2a\u8bb0\u5f55\u4e1a\u52a1\u903b\u8f91\u548c\u5c4f\u5e55\u72b6\u6001\u7684<strong>\u5bb9\u5668<\/strong>\uff0cLiveData\u662f\u4e00\u4e2a<strong>\u53ef\u4ee5\u88ab\u89c2\u5bdf<\/strong>\u7684<strong>\u6570\u636e\u96c6\u5408<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff0c\u521b\u5efa\u5bb9\u5668<\/h3>\n\n\n\n<p>\u6211\u4eec\u65b0\u5efa\u4e00\u4e2aViewModel\u5bb9\u5668\u7c7b\u3002\u6bd4\u5982MyViewModel<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class MyViewModel : ViewModel() {}  <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e8c\u6b65\uff0c\u5728\u5bb9\u5668\u4e2d\u521b\u5efaLiveData\u53ef\u88ab\u89c2\u5bdf\u7684\u6570\u636e\uff0c\u6bd4\u5982\u4e00\u4e2aString\u7c7b\u578b\u7684\u6570\u636e\u3002<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class MyViewModel : ViewModel() {\n\n    val currentName: MutableLiveData&lt;String> by lazy {\n        MutableLiveData&lt;String>()\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e09\u6b65\uff0c\u5728\u754c\u9762\u7ec4\u4ef6\uff08Activity\u6216Fragment\uff09\u4e2d\u53bb\u4f7f\u7528\u3002<\/h3>\n\n\n\n<p>\u9996\u5148\u6dfb\u52a0\u4e00\u4e2a\u89c2\u5bdf\u8005\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u65f6\u7684\u53bb\u5173\u952ecurrentName\u7684\u53d8\u52a8\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u6211\u4eec\u5148\u5728Activity\u4e2d\u6dfb\u52a0ViewModel \nprivate val model: MyViewModel by viewModels()\n\n\/\/\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u3002\n@Composable\nfun GetMessage(model: MyViewModel, cotnent: ComponentActivity) {\n    var message by remember {\n        mutableStateOf(\"\")\n    }\n\/\/\u5efa\u7acb\u4e00\u4e2a\u89c2\u5bdf\u8005\n    var observer = Observer&lt;String> {\n        message = it\n    }\n\n\/\/\u6dfb\u52a0\u89c2\u5bdf\u8005\uff0ccotnent:Activity\u5bf9\u8c61\uff0cobserver\u89c2\u5bdf\u8005\u5bf9\u8c61\nmodel.currentName.observe(cotnent, observer)\n\n    Text(text = \"\u5185\u5bb9\uff1a$message\")\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u56db\u6b65\uff0c\u8bbe\u7f6e\u6539\u53d8LiveData\u6570\u636e<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> model.currentName.setValue(\"\u8fd9\u662f\u66f4\u65b0\u7684\u5185\u5bb9\")<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5f97\u9996\u5148\u660e\u767dViewModel\u53ea\u662f\u4e00\u4e2a\u8bb0\u5f55\u4e1a\u52a1\u903b\u8f91\u548c\u5c4f\u5e55\u72b6\u6001\u7684\u5bb9\u5668\uff0cLiveData\u662f\u4e00\u4e2a\u53ef\u4ee5\u88ab\u89c2\u5bdf\u7684\u6570\u636e\u96c6\u5408\u3002<\/p>\n","protected":false},"author":1,"featured_media":159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=166"}],"version-history":[{"count":3,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions\/169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/media\/159"}],"wp:attachment":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}