
{"code":1,"message":"","data":{"id":119251,"article":{"id":28662,"publicAccount":{"id":335,"name":"郭霖","weixin":"guolin_blog","intro":"Android技术分享平台,每天都有优质技术文章推送.你还可以向公众号投稿,将自己总结的技术心得分享给大家.","body":"Android技术分享平台,每天都有优质技术文章推送。你还可以向公众号投稿,将自己总结的技术心得分享给大家。","image":"http://ss.csdn.net/p?http://wx.qlogo.cn/mmhead/Q3auHgzwzM5k5uLLlia9IDgibtUdGlhAGgkMBWRwFY8kN1djTAXYFh8g/0","href":null,"biz":"MzA5MzI3NjE2MA==","category":{"id":1,"keyName":"ydkf","displayName":"移动开发","createdAt":1484041113000,"updatedAt":1487578193000,"status":1},"createdAt":1488452951000,"updatedAt":1497420532000,"status":1,"recommend":1,"qrcode":"http://mp.weixin.qq.com/mp/qrcode?scene=10000001&size=120&__biz=MzA5MzI3NjE2MA==&mid=2650238737&idx=1&sn=e28e0ca6a112c730f157483dd6d83a41","articleCount":0,"viewCount":0},"category":{"id":1,"keyName":"ydkf","displayName":"移动开发","createdAt":1484041113000,"updatedAt":1487578193000,"status":1},"biz":"MzA5MzI3NjE2MA==","mid":"2650239446","idx":1,"sn":"71fdd6c9e63040251a5f4efc0b887cae","author":"CSDN_LQR","content":"","contentUrl":"http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650239446&idx=1&sn=71fdd6c9e63040251a5f4efc0b887cae&chksm=886380b9bf1409af40a2e3c84f19ac605f2c57de0dfdf25466ebc41378f8372c7b25f87ff084&scene=27#wechat_redirect","cover":"http://mmbiz.qpic.cn/mmbiz_jpg/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMm8l2QeicqZibAslkNzX2ZzlEdRdpEoZLv6ia7NQuBYwW4ICcF2lKYH5Khg/0?wx_fmt=jpeg","digest":"遇到问题不用怕,一步步分析问题,解决问题。","isMulti":0,"sourceUrl":"http://www.jianshu.com/p/8a0f6b627e37","title":"RecyclerView遇到notifyDataSetChanged无效时的解决方案","description":"遇到问题不用怕,一步步分析问题,解决问题。","datetime":1493856007000,"status":1,"viewCount":373,"praiseCount":17,"commentCount":0,"createdAt":1493888990000,"updatedAt":1496278724000,"origin":"proxy","recommend":0},"url":"http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650239446&idx=1&sn=71fdd6c9e63040251a5f4efc0b887cae&chksm=886380b9bf1409af40a2e3c84f19ac605f2c57de0dfdf25466ebc41378f8372c7b25f87ff084&scene=27#wechat_redirect","title":"RecyclerView遇到notifyDataSetChanged无效时的解决方案","source":"<p style=\"text-align: center;\"><img data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt4hS75rECCEiakbrpaQcLmuFMQhAqetW9hFoAGY8RJdgwr464AunYSbvEH1ic9fIZ8LzugaFaQhj7mQ/640?wx_fmt=png\" data-type=\"png\" data-ratio=\"0.1296875\" data-w=\"640\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt4hS75rECCEiakbrpaQcLmuFMQhAqetW9hFoAGY8RJdgwr464AunYSbvEH1ic9fIZ8LzugaFaQhj7mQ/640?wx_fmt=png\"></p> \n<section class=\"\" data-source=\"bj.96weixin.com\" style=\"white-space: normal; max-width: 100%; color: rgb(62, 62, 62); font-size: 18px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-top: 0.5em; margin-bottom: 0.5em; max-width: 100%; border-bottom: 2px solid rgb(105, 135, 166); clear: both; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"padding-top: 3px; padding-right: 10px; padding-left: 10px; max-width: 100%; display: inline-block; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; height: 28px; background-color: rgb(105, 135, 166); box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <span style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">今日科技快讯</span> \n </section> \n <section style=\"max-width: 100%; border-top: 28px solid transparent; border-right-color: rgb(206, 206, 206); border-bottom: 0px solid transparent; border-left: 15px solid rgb(206, 206, 206); display: inline-block; vertical-align: top; box-sizing: border-box !important; word-wrap: break-word !important;\"></section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n</section> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">近日有消息称:苹果原定于今年秋季和iPhone 7s、7s Plus一同发布的十周年纪念版本(被猜测为:iPhone 8、iPhone X、iPhone 7c等),因为遭遇了重要零部件短缺以及技术挑战,可能要延期到明年年初才能和消费者见面。另外,由于三星和国产品牌在全球市场的不断夹击,苹果的这次跳票可能会蒙受到极大的损失。</span></p> \n<section class=\"\" data-source=\"bj.96weixin.com\" style=\"white-space: normal; max-width: 100%; color: rgb(62, 62, 62); font-size: 18px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-top: 0.5em; margin-bottom: 0.5em; max-width: 100%; border-bottom: 2px solid rgb(105, 135, 166); clear: both; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"padding-top: 3px; padding-right: 10px; padding-left: 10px; max-width: 100%; display: inline-block; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; height: 28px; background-color: rgb(105, 135, 166); box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <span style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">作者简介</span> \n </section> \n <section style=\"max-width: 100%; border-top: 28px solid transparent; border-right-color: rgb(206, 206, 206); border-bottom: 0px solid transparent; border-left: 15px solid rgb(206, 206, 206); display: inline-block; vertical-align: top; box-sizing: border-box !important; word-wrap: break-word !important;\"></section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n</section> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">本篇来自 <strong style=\"white-space: normal;\"><span style=\"font-size: 14px;\">CSDN_LQR</span></strong><span style=\"font-size: 14px;\"> 的投稿,分析了Fragment中使用RecyclerView时notifyDataSetChanged可能出现的问题,希望能够帮助到大家。</span></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><strong><span style=\"font-size: 14px;\">CSDN_LQR</span></strong><span style=\"font-size: 14px;\"> 的博客地址:</span></p> \n<blockquote> \n <p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px; color: rgb(0, 82, 255);\">http://www.jianshu.com/u/f9de259236a3</span></p> \n</blockquote> \n<section class=\"\" data-source=\"bj.96weixin.com\" style=\"white-space: normal; max-width: 100%; color: rgb(62, 62, 62); font-size: 18px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-top: 0.5em; margin-bottom: 0.5em; max-width: 100%; border-bottom: 2px solid rgb(105, 135, 166); clear: both; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"padding-top: 3px; padding-right: 10px; padding-left: 10px; max-width: 100%; display: inline-block; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; height: 28px; background-color: rgb(105, 135, 166); box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <span style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">简述</span> \n </section> \n <section style=\"max-width: 100%; border-top: 28px solid transparent; border-right-color: rgb(206, 206, 206); border-bottom: 0px solid transparent; border-left: 15px solid rgb(206, 206, 206); display: inline-block; vertical-align: top; box-sizing: border-box !important; word-wrap: break-word !important;\"></section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n</section> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">不管 AbsListView(ListView、GridView) 或是 新出的RecyclerView,在使用 notifyDataSetChanged方法 更新列表数据时,一定要保证数据为同个对象(即hashCode要一致)。对于这个问题的论证,可以去看官方源代码,或是看我之前写的一篇博文</span></p> \n<blockquote> \n <p style=\"margin-top: 10px;\"><strong><span style=\"font-size: 14px;\">解决ListViews适配器notifyDataSetChanged()无效问题</span></strong></p> \n <p style=\"margin-bottom: 10px;\"><span style=\"font-size: 14px; color: rgb(0, 82, 255);\">http://blog.csdn.net/csdn_lqr/article/details/50947825</span></p> \n</blockquote> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">相信可以帮到你。但是,这个不是本文的重点,本文重点讲解在 Fragment 中,RecyclerView 遇到 notifyDataSetChanged 无效的问题。如果你赶时间,可以直接看第三部分(\"总结\")。</span></p> \n<p><span style=\"color: rgb(255, 172, 170);\"></span></p> \n<section class=\"\" data-source=\"bj.96weixin.com\" style=\"white-space: normal; max-width: 100%; color: rgb(62, 62, 62); font-size: 18px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-top: 0.5em; margin-bottom: 0.5em; max-width: 100%; border-bottom: 2px solid rgb(105, 135, 166); clear: both; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"padding-top: 3px; padding-right: 10px; padding-left: 10px; max-width: 100%; display: inline-block; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; height: 28px; background-color: rgb(105, 135, 166); box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <span style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">探索</span> \n </section> \n <section style=\"max-width: 100%; border-top: 28px solid transparent; border-right-color: rgb(206, 206, 206); border-bottom: 0px solid transparent; border-left: 15px solid rgb(206, 206, 206); display: inline-block; vertical-align: top; box-sizing: border-box !important; word-wrap: break-word !important;\"></section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n</section> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(255, 76, 0);\"><strong>1、查看数据(mData)是否是同个对象</strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(64, 118, 0);\"><em><strong><span style=\"font-size: 14px;\">tip:</span></strong></em></span><em><strong><span style=\"font-size: 14px;\">java中可以通过打印 hashCode 的方式判断 mData 是否为同个对象。</span></strong></em></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(217, 33, 66);\"><strong><span style=\"font-size: 14px;\">注意:</span></strong></span><span style=\"font-size: 14px;\">initData方法 在 onActivityCreated() 中被调用。</span></p> \n<p style=\"text-align: center;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmVH6yeAnPRxdkDrJvwlxd8qTFoXWR9qL9R7zplNw1jHiaZ88nr3bVZ3A/0?wx_fmt=png\" data-ratio=\"0.9670542635658915\" data-w=\"516\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmVH6yeAnPRxdkDrJvwlxd8qTFoXWR9qL9R7zplNw1jHiaZ88nr3bVZ3A/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(255, 76, 0);\"><strong>2、操作与结果</strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(64, 118, 0);\"><em><strong><span style=\"font-size: 14px;\">tip:</span></strong></em></span><span style=\"color: rgb(0, 0, 0);\"><em><strong><span style=\"font-size: 14px;\">常规对 Fragment 的使用,会对其进行缓存,也可能使用单例模式,反正就是短时间内不会重新创建。</span></strong></em></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(61, 167, 66);\"><strong><span style=\"font-size: 14px;\">①操作一</span></strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">打开 Activity 后,切换 Fragment(第一次初始化 Fragment)。显示效果如下:</span></p> \n<p style=\"text-align: center;\"><img data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMm1ldKVPV2iaEiaZkwaicndFG5MVLPVian0uo2xkoINyz3Dk5eK1Z9TSqCyw/0?wx_fmt=png\" data-type=\"png\" data-ratio=\"1.7770833333333333\" data-w=\"480\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMm1ldKVPV2iaEiaZkwaicndFG5MVLPVian0uo2xkoINyz3Dk5eK1Z9TSqCyw/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(61, 167, 66);\"><strong><span style=\"font-size: 14px;\">②操作二</span></strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">切换别的 Fragment 后,再切回刚才的 Fragment(此前该 Fragment 已经在存在,所以不会再次创建)。显示效果如下:</span></p> \n<p style=\"text-align: center;\"><img data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmPaFXqZeSDBRMf2tcZBcU5sK6VT0I1aSrjUnibibAKfb7ZNvGddYLibAlw/0?wx_fmt=png\" data-type=\"png\" data-ratio=\"1.7770833333333333\" data-w=\"480\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmPaFXqZeSDBRMf2tcZBcU5sK6VT0I1aSrjUnibibAKfb7ZNvGddYLibAlw/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(61, 167, 66);\"><strong><span style=\"font-size: 14px;\">③看控制台</span></strong></span></p> \n<p style=\"text-align: center;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmpibeeI20zEoBZn4TrdEbuR849Cf6QqhOdxoSiaOgA2PooibpcbibsSAepw/0?wx_fmt=png\" data-ratio=\"0.12307692307692308\" data-w=\"845\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmpibeeI20zEoBZn4TrdEbuR849Cf6QqhOdxoSiaOgA2PooibpcbibsSAepw/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">可以看到数据对象地址一样,即为同一个。</span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(255, 76, 0);\"><strong>3、查看RecyclerView是否是同个对象</strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">说实话,这个是踩坑经验丰富的网友在群里说的,如果不是他说出来,打死我也没想到,居然还有这么一个坑。从上面的结果可以看出,adapter 中是有数据的没错,而且数据地址没变,所以理应 notifyDataSetChanged()方法 会生效。但是为什么会这样呢,这里先卖个关子,先看下面的操作。</span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(61, 167, 66);\"><strong><span style=\"font-size: 14px;\">①改下上面的代码,打印RecyclerView的地址</span></strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">代码如下:</span></p> \n<p style=\"text-align: center;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMm8kMrYky7F9KicM13Cb8QxrxRu2NEeicVRpEcZELs2MoRxSthStZAibbtw/0?wx_fmt=png\" data-ratio=\"0.9707602339181286\" data-w=\"513\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMm8kMrYky7F9KicM13Cb8QxrxRu2NEeicVRpEcZELs2MoRxSthStZAibbtw/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(61, 167, 66);\"><strong><span style=\"font-size: 14px;\">②同上述操作一致</span></strong><strong><span style=\"font-size: 14px;\"></span></strong></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">对同一个 Fragment 来回切换,看控制台输出。</span></p> \n<p style=\"text-align: center;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmBFlErXl2ODicXcqNVuaEBiaVjiaEXUpkwESLSa7trhfV5gIOt9D1s24Lg/0?wx_fmt=png\" data-ratio=\"0.12408759124087591\" data-w=\"822\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmBFlErXl2ODicXcqNVuaEBiaVjiaEXUpkwESLSa7trhfV5gIOt9D1s24Lg/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">果然不一样!!!</span></p> \n<p><span style=\"color: rgb(215, 171, 169);\"></span></p> \n<section class=\"\" data-source=\"bj.96weixin.com\" style=\"white-space: normal; max-width: 100%; color: rgb(62, 62, 62); font-size: 18px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-top: 0.5em; margin-bottom: 0.5em; max-width: 100%; border-bottom: 2px solid rgb(105, 135, 166); clear: both; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"padding-top: 3px; padding-right: 10px; padding-left: 10px; max-width: 100%; display: inline-block; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; height: 28px; background-color: rgb(105, 135, 166); box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <span style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">总结</span> \n </section> \n <section style=\"max-width: 100%; border-top: 28px solid transparent; border-right-color: rgb(206, 206, 206); border-bottom: 0px solid transparent; border-left: 15px solid rgb(206, 206, 206); display: inline-block; vertical-align: top; box-sizing: border-box !important; word-wrap: break-word !important;\"></section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n</section> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">为什么在 Fragment 中 RecyclerView 的地址会发生变化呢?我们先理清一下 Fragment 生命周期会陆续调用的几个方法:</span></p> \n<pre style=\" box-sizing: border-box; ; ; ; ; ; ; ; ; ; ; ; ; ; ; \"><span style=\"font-size: 12px;\">onCreate() <span class=\"pl-k\" style=\"box-sizing: border-box; color: rgb(167, 29, 93);\">-></span> onCreateView() <span class=\"pl-k\" style=\"box-sizing: border-box; color: rgb(167, 29, 93);\">-></span> onActivityCreated() <span class=\"pl-k\" style=\"box-sizing: border-box; color: rgb(167, 29, 93);\">-></span> onDestroy()</span></pre> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">中间少了几个方法,请不用在意,下面贴下我的 BaseFragment 代码:</span></p> \n<p style=\"text-align: center;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmuP4gEA3wibJSScNhtw9ibNfx6TAMw1ebQH9PTA1QtSPB8OvGvheZYFkA/0?wx_fmt=png\" data-ratio=\"0.9154704944178629\" data-w=\"627\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmuP4gEA3wibJSScNhtw9ibNfx6TAMw1ebQH9PTA1QtSPB8OvGvheZYFkA/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">当一个 Fragment 在来回切换时,分别调用的方法如下:</span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">第一次显示:</span></p> \n<pre style=\" box-sizing: border-box; ; ; ; ; ; ; ; ; ; ; ; ; ; ; \"><span style=\"font-size: 12px;\">onCreate() <span class=\"pl-k\" style=\"box-sizing: border-box; color: rgb(167, 29, 93);\">-></span> onCreateView() <span class=\"pl-k\" style=\"box-sizing: border-box; color: rgb(167, 29, 93);\">-></span> onActivityCreated()</span></pre> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">第二次显示:</span></p> \n<pre style=\" box-sizing: border-box; ; ; ; ; ; ; ; ; ; ; ; ; ; ; \"><span style=\"font-size: 12px;\">onCreateView() <span class=\"pl-k\" style=\"box-sizing: border-box; color: rgb(167, 29, 93);\">-></span> onActivityCreated()</span></pre> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">这里不难理解,因为 Fragment 一般使用的时候会被缓存,所以,当第二次显示的时候,不会调用 onCreate()。只会调用 onCreateView() 和 onActivityCreated(),这也就是 RecyclerView 地址不一样的原因所在,因为控件获取操作是在 initView() 中进行的,即 RecyclerView 的获取操作在 onCreateView() 中,而 Fragment 的每次显示都会调用 onCreateView(),所以 RecyclerView 控件会被再次获取,即重新创建一个对象(此时hashCode就变化了)。</span></p> \n<h2 style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(255, 76, 0); font-size: 16px;\"><strong><span style=\"color: rgb(255, 76, 0);\">结论</span></strong></span></h2> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">所以,在 Fragment 中使用 RecyclerView 或 AbsListView 控件的 notifyDataSetChanged()方法 时,除了保证数据(mData对象)不能变以外,控件本身一样也不能变。</span></p> \n<h2 style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"color: rgb(255, 76, 0); font-size: 16px;\"><strong><span style=\"color: rgb(255, 76, 0);\">解决方案</span></strong></span></h2> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><span style=\"font-size: 14px;\">因为 Fragment 的 onCreateView() 和 onActivityCreated() 方法在每次 Fragment 显示的时候会被调用,控件会被重新创建一次,所以,解决方法只能是在这两个方法中重新对RecyclerView 设置适配器,而不要使用 notifyDataSetChanged(),故代码改为如下:</span></p> \n<p style=\"text-align: center;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmTxytcQIq68iafCM5zicIurkXFENnKp4dhC1560vECjiavzh5T7DB6W4mQ/0?wx_fmt=png\" data-ratio=\"0.788785046728972\" data-w=\"535\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt6SfVNrQKyyEgQHbUaC9tMmTxytcQIq68iafCM5zicIurkXFENnKp4dhC1560vECjiavzh5T7DB6W4mQ/640?wx_fmt=png\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; line-height: 1.75em;\"><strong><span style=\"font-size: 14px;\">注:</span></strong><span style=\"font-size: 14px;\">只是建议不要在上述两个生命周期方法中使用 notifyDataSetChanged() 而已,只要在保证 RecyclerView 等列表控件设置完适配器后,可以在任意地方继续使用 notifyDataSetChanged()。</span></p> \n<section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; color: rgb(62, 62, 62); white-space: normal; font-size: 18px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" data-source=\"bj.96weixin.com\" style=\"max-width: 100%; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-right: auto; margin-left: auto; max-width: 100%; clear: both; width: 556px; overflow: hidden; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"margin-top: 0.5em; margin-bottom: 0.5em; max-width: 100%; border-bottom: 2px solid rgb(105, 135, 166); clear: both; box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <section class=\"\" style=\"padding-top: 3px; padding-right: 10px; padding-left: 10px; max-width: 100%; display: inline-block; color: rgb(255, 255, 255); border-top-left-radius: 3px; border-top-right-radius: 3px; height: 28px; background-color: rgb(105, 135, 166); box-sizing: border-box !important; word-wrap: break-word !important;\"> \n <span style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">更多</span> \n </section> \n <section style=\"max-width: 100%; border-top: 28px solid transparent; border-right-color: rgb(206, 206, 206); border-bottom: 0px solid transparent; border-left: 15px solid rgb(206, 206, 206); display: inline-block; vertical-align: top; box-sizing: border-box !important; word-wrap: break-word !important;\"></section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n </section> \n</section> \n<p style=\"max-width: 100%; min-height: 1em; color: rgb(62, 62, 62); font-size: 16px; white-space: normal; box-sizing: border-box !important; word-wrap: break-word !important;\"><span style=\"max-width: 100%; font-size: 14px; box-sizing: border-box !important; word-wrap: break-word !important;\">每天学习累了,看些搞笑的段子放松一下吧。</span><span style=\"max-width: 100%; font-size: 14px; line-height: 31.5px; box-sizing: border-box !important; word-wrap: break-word !important;\">关注最具娱乐精神的公众号,每天都有好心情。</span><br style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\"></p> \n<p style=\"max-width: 100%; min-height: 1em; color: rgb(62, 62, 62); font-size: 16px; white-space: normal; line-height: 25.6px; text-align: center; box-sizing: border-box !important; word-wrap: break-word !important;\"><img class=\"\" data-ratio=\"0.6024096385542169\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt5EgQ1mqPQjwro5T3dibeDb9oUpttPkuvVib8s3qRPicXN9iaKlEliafgzEs3SLapeiagEDhzLXw3qOp7yA/0?wx_fmt=gif\" data-type=\"gif\" data-w=\"581\" style=\"line-height: 1.6; box-sizing: border-box !important; word-wrap: break-word !important; visibility: visible !important; width: 581px !important;\" width=\"auto\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/v1LbPPWiaSt5EgQ1mqPQjwro5T3dibeDb9oUpttPkuvVib8s3qRPicXN9iaKlEliafgzEs3SLapeiagEDhzLXw3qOp7yA/640?wx_fmt=gif\"></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; max-width: 100%; min-height: 1em; color: rgb(62, 62, 62); white-space: normal; font-size: 18px; line-height: 1.75em; box-sizing: border-box !important; word-wrap: break-word !important;\"><span style=\"max-width: 100%; font-size: 14px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\">如果你有好的技术文章想和大家分享,欢迎向我的公众号投稿,投稿具体细节请在公众号主页点击</span><strong style=\"max-width: 100%; font-size: 14px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\">“投稿”</strong><span style=\"max-width: 100%; font-size: 14px; line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;\">菜单查看。</span><br style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\"><span style=\"max-width: 100%; font-size: 14px; box-sizing: border-box !important; word-wrap: break-word !important;\"></span></p> \n<p style=\"margin-top: 10px; margin-bottom: 10px; max-width: 100%; min-height: 1em; color: rgb(62, 62, 62); white-space: normal; font-size: 18px; line-height: 1.75em; box-sizing: border-box !important; word-wrap: break-word !important;\"><span style=\"max-width: 100%; font-size: 14px; box-sizing: border-box !important; word-wrap: break-word !important;\">欢迎<strong style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">长按下图 -> 识别图中二维码</strong>或者<strong style=\"max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;\">扫一扫</strong>关注我的公众号:</span></p> \n<p style=\"max-width: 100%; min-height: 1em; color: rgb(62, 62, 62); white-space: normal; font-size: 18px; line-height: 25.6px; text-align: center; box-sizing: border-box !important; word-wrap: break-word !important;\"><img class=\"\" data-ratio=\"1\" data-s=\"300,640\" data-src=\"http://mmbiz.qpic.cn/mmbiz/v1LbPPWiaSt7tNCYnLibalRPewwfNeEJV1FGVviaqzIdSEH0A3TgIVia5KQK5rbhAkDFez4ibOcrhDPblgNxMayCnQw/640?wx_fmt=jpeg\" data-type=\"jpeg\" data-w=\"258\" style=\"box-sizing: border-box !important; word-wrap: break-word !important; visibility: visible !important; width: 258px !important;\" width=\"auto\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz/v1LbPPWiaSt7tNCYnLibalRPewwfNeEJV1FGVviaqzIdSEH0A3TgIVia5KQK5rbhAkDFez4ibOcrhDPblgNxMayCnQw/640?wx_fmt=jpeg\"></p>","level":3,"createdAt":1493888990000,"updatedAt":1496278724000}}
公众号