{"id":109957,"date":"2020-06-01T04:59:38","date_gmt":"2020-05-31T20:59:38","guid":{"rendered":"http:\/\/4563.org\/?p=109957"},"modified":"2020-06-01T04:59:38","modified_gmt":"2020-05-31T20:59:38","slug":"jetpack-%e5%ae%9e%e8%b7%b5%e6%97%b6%e7%9a%84%e7%96%91%e9%97%ae%ef%bc%8c%e6%af%94%e8%be%83%e5%a4%8d%e6%9d%82%e7%9a%84%e9%a1%b5%e9%9d%a2-livedata-%e8%a6%81%e6%80%8e%e4%b9%88%e7%94%a8%e6%89%8d%e5%a5%bd","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=109957","title":{"rendered":"jetpack \u5b9e\u8df5\u65f6\u7684\u7591\u95ee\uff0c\u6bd4\u8f83\u590d\u6742\u7684\u9875\u9762 Livedata \u8981\u600e\u4e48\u7528\u624d\u597d\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  jetpack \u5b9e\u8df5\u65f6\u7684\u7591\u95ee\uff0c\u6bd4\u8f83\u590d\u6742\u7684\u9875\u9762 Livedata \u8981\u600e\u4e48\u7528\u624d\u597d\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ppg01065 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u91cc\u6307\u7684\u590d\u6742\u4e3b\u8981\u662f\u8868\u5355\u8981\u586b\u7684\u5185\u5bb9\u5f88\u591a\uff0c\u518d\u52a0\u4e0a\u591a\u4e2a\u5b57\u6bb5\u90fd\u9700\u8981\u8054\u52a8\u7684\u60c5\u51b5\u4e0b\u600e\u4e48\u624d\u7b97\u662f\u6bd4\u8f83\u597d\u7684\u5199\u6cd5\uff1f<\/p>\n<p>\u9879\u76ee\u63a5\u8fd1\u4e8e OA \u529e\u516c\u7c7b\u7684 app \u3002\u67d0\u4e9b\u9875\u9762\u8981\u586b\u5199\u7f16\u8f91\u8868\u5355\uff0c\u4e00\u4e2a\u8868\u5355\u5e38\u5e38\u6709 20-30 \u4e2a\u53ef\u7f16\u8f91\u9879<\/p>\n<p>\u5047\u8bbe FormBean \u8fd9\u4e2a\u5b9e\u4f53\u7c7b\u5bf9\u5e94\u7684\u8868\u5355\u5bf9\u8c61 UserForm \u6709 30 \u4e2a\u5b57\u6bb5\u3002\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u80fd\u7f16\u8f91<\/p>\n<p>\u90a3\u4e48\u5982\u679c\u6211\u5728\u9875\u9762\u4e0a\u53ea\u5bf9 Name \u5b57\u6bb5\u8fdb\u884c\u4fee\u6539\u7684\u8bdd\uff0c\u600e\u4e48\u66f4\u65b0 UserForm \u5e76\u4e14\u89e6\u53d1 LiveData \u7684 onChanged \u4e8b\u4ef6\u5462\uff1f<\/p>\n<p>\u76ee\u524d\u5728\u7f51\u4e0a\u7684\u6587\u7ae0 Livedata \u90fd\u662f\u7528 MutableLiveData&lt;String&gt;\u4e4b\u7c7b\u7684\u57fa\u672c\u7c7b\u578b\u505a\u4f8b\u5b50\uff0c\u6ca1\u627e\u5230\u600e\u4e48\u66f4\u65b0\u67d0\u4e2a\u5177\u4f53\u7684\u5b9e\u4f53\u7c7b\u7684\u65b9\u5f0f<\/p>\n<pre><code>public class MainViewModel extends ViewModel {     \/*     \u516c\u53f8\u7684\u9879\u76ee\u63a5\u8fd1 OA \u529e\u516c\u7c7b\u4ea7\u54c1\uff0c\u6709\u5f88\u591a\u8868\u5355\u9700\u8981\u586b\u5199\u548c\u4fee\u6539     \u5047\u8bbe\u8fd9\u4e2a FormBean \u8868\u5355\u5bf9\u8c61\u6709 30 \u4e2a\u5b57\u6bb5\u3002\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u80fd\u7f16\u8f91\uff0c\u5982\u679c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u5355\u72ec\u521b\u5efa\u4e00\u4e2a livedata \u5bf9\u8c61\u7684\u8bdd\uff0c\u4f1a\u53d8\u6210\u5f88\u957f\u7684\u4ee3\u7801     private MutableLiveData&lt;String&gt; field1;     private MutableLiveData&lt;String&gt; field2;     private MutableLiveData&lt;String&gt; field3;     private MutableLiveData&lt;String&gt; field4;     private MutableLiveData&lt;String&gt; field5;     ........     private MutableLiveData&lt;String&gt; field29;     private MutableLiveData&lt;String&gt; field30;          \u7136\u540e\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u8981\u5199 get \u548c set \u65b9\u6cd5\u3002\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u8981\u8bbe\u7f6e observe \u76d1\u542c\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u7b97\u662f\u6700\u4f73\u5b9e\u8df5\u5417\uff1f\u611f\u89c9\u8981\u5199\u5f88\u591a\u91cd\u590d\u4ee3\u7801\u3002     *\/      \/\/\u5982\u679c\u53ea\u521b\u5efa\u4e00\u4e2a FormBean \u7684\u5b9e\u4f53\u7c7b\uff0c\u53ef\u4ee5\u53ea\u8bbe\u7f6e\u4e00\u6b21 observe \u76d1\u542c\uff0c\u4f46\u662f\u8981\u600e\u4e48\u66f4\u65b0\u5176\u4e2d\u7684\u67d0\u4e00\u4e2a\u5b57\u6bb5\u5462\uff1f     private MutableLiveData&lt;FormBean&gt; UserForm;      public LiveData&lt;FormBean&gt; getCurrentUserForm() {         if (UserForm == null) {             UserForm = new MutableLiveData&lt;FormBean&gt;();             FormBean formBean = new FormBean();             formBean.setName(\"aaabbb\");             formBean.setId(123);             UserForm.setValue(formBean);         }         return UserForm;     }      public void setField1(String userName) {         \/\/\u6211\u76ee\u524d\u60f3\u7684\u65b9\u6cd5\u662f\u5728 set \u65b9\u6cd5\u91cc\u5148\u901a\u5173 getValue()\u83b7\u53d6 livedata \u6301\u6709\u7684\u5b9e\u4f53\u7c7b\uff0c\u66f4\u65b0\u5bf9\u5e94\u7684\u5b57\u6bb5\u540e\u518d\u91cd\u65b0 setValue \u56de\u53bb\u3002\u6211\u4e0d\u77e5\u9053\u8fd9\u7b97\u4e0d\u7b97\u597d\u529e\u6cd5\uff0c\u4f46\u662f\u6682\u65f6\u60f3\u4e0d\u51fa\u5176\u4ed6\u529e\u6cd5\u4e86         UserForm.getValue().setName(userName);         UserForm.setValue(UserForm.getValue());     } } <\/code><\/pre>\n<p>\u6211\u76ee\u524d\u89c9\u5f97\u6211\u7684\u5199\u6cd5\u6709\u70b9\u8fc7\u4e8e\u6d6a\u8d39\uff0c\u56e0\u4e3a\u8fd9\u6837\u5199\u5c31\u7b97\u53ea\u66f4\u65b0\u4e00\u4e2a Name \u5b57\u6bb5\u4e5f\u4f1a\u4f7f\u5f97\u6240\u6709\u89c2\u5bdf\u4e86 UserForm \u7684\u63a7\u4ef6\u5237\u65b0\u4e00\u6b21\u3002<\/p>\n<p>\u6709\u6ca1\u6709\u5144\u5f1f\u53ef\u4ee5\u544a\u8bc9\u6211\u8fd9\u79cd\u60c5\u51b5\u600e\u4e48\u529e\uff0c\u6216\u8005\u7ed9\u4e00\u4e2a\u601d\u8def\u3002<\/p>\n<p>\u6628\u5929\u770b\u4e86\u4e00\u5929\u7684 github\uff0c\u5927\u90e8\u5206\u7528\u4e86 jetpack \u7684\u793a\u4f8b\u90fd\u662f\u83b7\u53d6\u6570\u636e &#8212;\u300b \u5c55\u793a\u6570\u636e\u3002\u6700\u591a\u5c31\u4fee\u6539\u4e00\u4e2a id \u4e4b\u7c7b\u7684\u3002\u5f88\u7b80\u6d01\u4e5f\u5f88\u6f02\u4eae\u3002\u4f46\u662f\u6211\u53d1\u73b0\u6211\u5982\u679c\u8981\u7167\u8fd9\u6837\u5199\uff0c\u6211\u5c31\u80fd\u5728\u9875\u9762\u4e0a\u5199\u51fa 30 \u4e2a MutableLiveData&lt;String&gt; field1 \u4e86\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1833131\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sankemao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u7528 livedata \u5728 xml \u91cc\u9762\u53cc\u5411\u7ed1\u5b9a<br \/>https:\/\/developer.android.com\/topic\/libraries\/data-binding\/two-way                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833132\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ppg01065 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sankemao \u53ea\u80fd\u7528 DataBinding \u5417\uff1f\u76ee\u524d\u53ea\u60f3\u7528 ViewBinding\uff0c\u611f\u89c9 DataBinding \u4e0d\u9999\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833133\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ppg01065 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u800c\u4e14\u8868\u5355\u91cc\u9762\u7684\u5f88\u591a\u6570\u636e\u90fd\u662f\u6709\u4e00\u5b9a\u7684\u8054\u52a8\u53d8\u5316\u8981\u6c42\uff0c\u6240\u4ee5\u66f4\u65b0\u5728\u4ee3\u7801\u4e2d\u5904\u7406\u6570\u636e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833134\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : winterbells <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             databinding \u554a<br \/>viewbinding \u6211\u8bb0\u5f97\u6587\u6863\u8bf4\u4e0d\u652f\u6301\u6570\u636e\u53cc\u5411\u7ed1\u5b9a\u7684\u5427<\/p>\n<p>UI model \u5355\u72ec\u4e00\u4e2a\u7c7b\uff0c\u5728 viewmodel \u91cc\u58f0\u660e\uff0c\u90fd\u653e viewmodel \u592a\u4e71\u4e86\u3002\u3002<\/p>\n<p>\u53ef\u4ee5\u53c2\u8003\u6211\u5199\u7684\u8fd9\u4e2a\uff0c\u53ef\u80fd\u4e0d\u662f\u592a\u597d\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u4e0a\u5bb6\u6bd4\u8f83\u5927\u7684\u5e94\u7528\u4e5f\u662f\u6309\u8fd9\u4e2a\u6765\u7684<br \/>https:\/\/github.com\/ohyooo\/MVVMBaseProject                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833135\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ppg01065 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @winterbells \u76ee\u524d\u662f\u4e0d\u6253\u7b97\u7528\u53cc\u5411\u7ed1\u5b9a\uff0c\u624d\u7528\u7684 viewbinding \u3002\u6240\u4ee5\u4e00\u76f4\u5728\u5bfb\u627e\u89e3\u51b3\u7684\u601d\u8def                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833136\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GuilinLR <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ppg01065 databinding \u9999\u7684\u72e0\uff0c\u6253\u4e2a\u6bd4\u65b9\uff0c\u81ea\u5df1\u5199\u4e2a adapter\uff0c\u8bbe\u7f6e\u63a7\u4ef6\u7684\u9690\u85cf\u52a8\u753b\uff0c\u90fd\u4e0d\u7528\u52a8 java \u4ee3\u7801\u3002\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5999\u7528\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833137\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fromzero <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a\u8868\u5355\u7684\u6570\u636e\u5c3d\u91cf\u5408\u5e76\u6210\u4e00\u4e2a\u7c7b\uff0c\u6216\u8005\u6309\u5206\u7c7b\u5408\u5e76\u6210\u51e0\u4e2a\u7c7b\u300220 30 \u4e2a\u53ef\u7f16\u8f91\u9879\u6309\u529f\u80fd\u62bd\u6210\u51e0\u4e2a\u7c7b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833138\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fromzero <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 kotlin \u7684\u8bdd\uff0c\u5b57\u6bb5\u6539\u52a8\uff0c\u5c31\u76f4\u63a5 livedata.value = data.copy                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1833139\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sukaidev <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 databinding \u53cc\u5411\u7ed1\u5b9a\u662f\u6700\u4f18\u96c5\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>jetpack \u5b9e\u8df5\u65f6\u7684\u7591\u95ee\uff0c\u6bd4\u8f83&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/109957"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109957"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/109957\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109957"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}