├── .gitignore ├── License.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── shizhefei │ │ └── mutitypedemo │ │ ├── ExampleApplication.java │ │ ├── activity │ │ ├── BoomActivity.java │ │ ├── ChatActivity.java │ │ ├── LoadActivity.java │ │ ├── LongPageActivity.java │ │ ├── LongPageStateActivity.java │ │ ├── MainActivity.java │ │ ├── longpage │ │ │ ├── AirlineTicketFragment.java │ │ │ ├── CultureFragment.java │ │ │ ├── EditFragment.java │ │ │ ├── FoodFragment.java │ │ │ ├── HotelFragment.java │ │ │ ├── InfoFragment.java │ │ │ ├── LoadFragment.java │ │ │ ├── RecommendFragment.java │ │ │ └── ShopFragment.java │ │ └── longpagelazy │ │ │ ├── AirlineTicketLazyFragment.java │ │ │ ├── CultureLazyFragment.java │ │ │ ├── EditLazyFragment.java │ │ │ ├── FoodLazyFragment.java │ │ │ ├── HotelLazyFragment.java │ │ │ ├── InfoLazyFragment.java │ │ │ ├── LoadLazyFragment.java │ │ │ ├── RecommendLazyFragment.java │ │ │ └── ShopLazyFragment.java │ │ ├── type │ │ ├── Food.java │ │ ├── FoodProvider.java │ │ ├── Good.java │ │ ├── GoodProvider.java │ │ ├── ImageItem.java │ │ ├── ImageItemProvider.java │ │ ├── Message.java │ │ ├── MessageProvider.java │ │ ├── RichItem.java │ │ ├── RichItemProvider.java │ │ └── StringProvider.java │ │ └── util │ │ └── DisplayUtils.java │ └── res │ ├── drawable-xhdpi │ ├── airlinetiket.png │ ├── back.png │ ├── food_1.png │ ├── go.png │ ├── good_1.png │ ├── hotel.png │ ├── hotel_pic.png │ ├── message_left_bg.9.png │ ├── message_right_bg.9.png │ ├── sanya.png │ ├── to.png │ ├── user_a.png │ └── user_b.png │ ├── drawable │ └── square_border.xml │ ├── layout │ ├── activity_chat.xml │ ├── activity_long_page.xml │ ├── activity_main.xml │ ├── fragment_airlineticket.xml │ ├── fragment_culture.xml │ ├── fragment_edit.xml │ ├── fragment_food.xml │ ├── fragment_hotel.xml │ ├── fragment_info.xml │ ├── fragment_recommend.xml │ ├── fragment_shop.xml │ ├── item_food.xml │ ├── item_good.xml │ ├── item_image.xml │ ├── item_message_left.xml │ ├── item_message_right.xml │ ├── item_rich.xml │ ├── item_string.xml │ ├── item_text.xml │ └── test.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ ├── avatar.gif │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── drawable-xhdpi.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── shizhefei │ │ └── view │ │ └── multitype │ │ ├── ItemBinder.java │ │ ├── ItemBinderFactory.java │ │ ├── ItemViewProvider.java │ │ ├── ItemViewProviderSet.java │ │ ├── MultiTypeAdapter.java │ │ ├── MultiTypeView.java │ │ ├── data │ │ ├── IParcelableData.java │ │ └── SerializableData.java │ │ └── provider │ │ ├── ChildViewHeightLayout.java │ │ ├── FragmentData.java │ │ ├── FragmentDataProvider.java │ │ ├── ViewProvider.java │ │ └── ViewUtils.java │ └── res │ └── values │ ├── ids.xml │ └── strings.xml ├── raw ├── 1.png ├── 2.png └── MutiTypeDemo.apk └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | /.idea/ 8 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MultiTypeView 2 | ================== 3 | 4 | #1.简化RecyclerView的多种type的adapter 5 | #2.ViewHolder的创建和绑定被提取出来变成ItemViewProvider,可以被多个adapter复用 6 | #3.支持一种数据对应多种ItemViewProvider 7 | #4.Fragment可以添加到RecyclerView上,实现复杂的界面 8 | 对于复杂的界面非常有利,一个复杂的界面可以分成多个Fragment,一个项目组分配给多个人开发. 9 | #5.RecyclerView上的Fragment是显示时候才加载 10 | 具有懒加载的效果.假设RecyclerView上放了10个Fragment,你进去的时候只加载到开始的1,2个Fragment 11 | #6.支持全局注册ItemViewProvider,和局部注册,和局部覆盖注册. 12 | #7.列表的数据保存和恢复,以及fragment的保存和恢复 13 | 14 | Download sample [Apk](https://github.com/LuckyJayce/MultiTypeView/blob/master/raw/MutiTypeDemo.apk?raw=true) 15 | 16 | # 效果图 # 17 | ![image](https://github.com/LuckyJayce/MultiTypeView/blob/master/raw/1.png) 18 | # 关系图 # 19 | ![image](https://github.com/LuckyJayce/MultiTypeView/blob/master/raw/2.png) 20 | # 使用方法 21 | 22 | 实现ItemViewProvider 23 | 24 | public class MessageLeftProvider extends ItemViewProvider { 25 | 26 | @Override 27 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int providerType) { 28 | return new ItemViewHolder(inflater.inflate(R.layout.item_message_left, parent, false)); 29 | } 30 | 31 | @Override 32 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Message message) { 33 | ItemViewHolder vh = (ItemViewHolder) viewHolder; 34 | vh.textView.setText(message.text); 35 | } 36 | 37 | private class ItemViewHolder extends RecyclerView.ViewHolder { 38 | 39 | private final TextView textView; 40 | 41 | public ItemViewHolder(View itemView) { 42 | super(itemView); 43 | textView = (TextView) itemView.findViewById(R.id.item_message_textView); 44 | } 45 | } 46 | } 47 | 48 | Activity代码 49 | 50 | public class LongPageActivity extends AppCompatActivity { 51 | private MultiTypeAdapter multiTypeAdapter; 52 | private MultiTypeView multiTypeView; 53 | private String myUserId = "1"; 54 | 55 | @Override 56 | protected void onCreate(Bundle savedInstanceState) { 57 | super.onCreate(savedInstanceState); 58 | setContentView(R.layout.activity_long_page); 59 | multiTypeView = (MultiTypeView) findViewById(R.id.multiTypeView); 60 | //带有FragmentManager的构造函数,默认帮您添加支持Fragment数据的FragmentHolderProvider 61 | //也就是说你可以把Fragment放在adapter上使用,这里的Fragment只有第一次滑动到对应位置才会onCreateView的方法 62 | ItemBinderFactory itemBinderFactory = new ItemBinderFactory(getSupportFragmentManager()); 63 | //有时候需要根据TextItem里面的某个字段,生成不同的布局.比如聊天界面的message是一样的,但是有区分左右布局 64 | //ItemProviderSet可以通过数据类型区分无数种情况的Provider 65 | itemBinderFactory.registerProvider(Message.class, new ItemViewProviderSet(new MessageProvider(MessageProvider.ALIGN_LEFT), new MessageProvider(MessageProvider.ALIGN_RIGHT)) { 66 | @Override 67 | protected int selectIndex(Message message) { 68 | return myUserId.equals(message.userId) ? 1 : 0; 69 | } 70 | }); 71 | multiTypeAdapter = new MultiTypeAdapter<>(loadData(0), itemBinderFactory); 72 | multiTypeView.setAdapter(multiTypeAdapter); 73 | } 74 | 75 | private List loadData(int page) { 76 | List data = new ArrayList<>(); 77 | TextView textView = new TextView(this); 78 | textView.setText("第" + page + "页"); 79 | textView.setGravity(Gravity.CENTER); 80 | textView.setBackgroundColor(Color.GRAY); 81 | textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dipToPix(this, 100))); 82 | data.add(textView); 83 | data.add(new Message("1", "今天这个状态有所保留么?")); 84 | data.add(new Message("2", "没有保留!我已经,我已经用了洪荒之力啦!")); 85 | data.add(new FragmentData(InfoLazyFragment.class, "InfoLazyFragment" + page)); 86 | data.add(new FragmentData(EditLazyFragment.class, "EditLazyFragment" + page)); 87 | data.add(new FragmentData(HotelLazyFragment.class, "HotelLazyFragment" + page)); 88 | data.add(new FragmentData(AirlineTicketLazyFragment.class, "AirlineTicketLazyFragment" + page)); 89 | data.add(new FragmentData(ShopLazyFragment.class, "ShopLazyFragment" + page)); 90 | data.add(new FragmentData(RecommendLazyFragment.class, "RecommendLazyFragment" + page)); 91 | data.add(new FragmentData(FoodLazyFragment.class, "FoodLazyFragment" + page)); 92 | data.add(new FragmentData(CultureLazyFragment.class, "CultureLazyFragment" + page)); 93 | this.page = page; 94 | return data; 95 | } 96 | } 97 | ## 很简单吧,三个步骤 98 | **1.实现ItemViewProvider并注册到ItemBinderFactory上** 99 | 100 | **2.然后设置MultiTypeAdapter** 101 | 102 | **3.添加数据** 103 | 104 | # Gradle # 105 | 106 | compile 'com.shizhefei:MultiTypeView:1.0.1' 107 | 由于用到了v4和recyclerview所以也要导入他们 108 | compile 'com.android.support:support-v4:23.4.0' 109 | compile 'com.android.support:recyclerview-v7:23.2.1' 110 | 111 | 112 | # 说明 # 113 | 114 | **本项目是根据 https://github.com/drakeet/MultiType 类库的想法 115 | 把ViewHolder的创建以ItemViewProvider的形式分离出来 116 | ,以及参考了部分代码实现.** 117 | 118 | **之后我添加了Fragment添加到RecyclerView上功能,以及view以数据的形式也可以添加上去.还有就是全局注册和局部注册的方案,ItemViewProviderSet实现了相同数据类型不同的ItemViewProvider.** 119 | 120 | **非常感谢** **@drakeet** 121 | 122 | ##主力类库## 123 | 124 | **1.https://github.com/LuckyJayce/ViewPagerIndicator** 125 | Indicator 取代 tabhost,实现网易顶部tab,新浪微博主页底部tab,引导页,无限轮播banner等效果,高度自定义tab和特效 126 | 127 | **2.https://github.com/LuckyJayce/MVCHelper** 128 | 实现下拉刷新,滚动底部自动加载更多,分页加载,自动切换显示网络失败布局,暂无数据布局,支持任意view,支持切换主流下拉刷新框架。 129 | 130 | **3.https://github.com/LuckyJayce/MultiTypeView** 131 | 简化RecyclerView的多种type的adapter,Fragment可以动态添加到RecyclerView上,实现复杂的界面分多个模块开发 132 | 133 | **4.https://github.com/LuckyJayce/EventBus** 134 | 事件总线,通过动态代理接口的形式发布,接收事件。定义一个接口把事件发给注册并实现接口的类 135 | 136 | **5.https://github.com/LuckyJayce/LargeImage** 137 | 大图加载,可供学习 138 | 139 | **6.https://github.com/LuckyJayce/GuideHelper** 140 | 新手引导页,轻松的实现对应的view上面的显示提示信息和展示功能给用户 141 | 142 | **7.https://github.com/LuckyJayce/HVScrollView** 143 | 可以双向滚动的ScrollView,支持嵌套ScrollView联级滑动,支持设置支持的滚动方向 144 | 145 | **8.https://github.com/LuckyJayce/CoolRefreshView** 146 | 下拉刷新RefreshView,支持任意View的刷新 ,支持自定义Header,支持NestedScrollingParent,NestedScrollingChild的事件分发,嵌套ViewPager不会有事件冲突 147 | 148 | 有了这些类库,让你6的飞起 149 | 150 | # 联系方式和问题建议 151 | 152 | * 微博:http://weibo.com/u/3181073384 153 | * QQ 群: 开源项目使用交流 :549284336 154 | 155 | License 156 | ======= 157 | 158 | Copyright 2016 shizhefei(LuckyJayce) 159 | Copyright 2016 drakeet. 160 | 161 | Licensed under the Apache License, Version 2.0 (the "License"); 162 | you may not use this file except in compliance with the License. 163 | You may obtain a copy of the License at 164 | 165 | http://www.apache.org/licenses/LICENSE-2.0 166 | 167 | Unless required by applicable law or agreed to in writing, software 168 | distributed under the License is distributed on an "AS IS" BASIS, 169 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 170 | See the License for the specific language governing permissions and 171 | limitations under the License. 172 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /build/ 3 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.shizhefei.mutitypedemo" 9 | minSdkVersion 10 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:24.1.1' 26 | compile 'com.android.support:recyclerview-v7:24.1.1' 27 | compile 'com.android.support:cardview-v7:24.1.1' 28 | debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2' 29 | releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2' 30 | testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2' 31 | compile 'de.hdodenhof:circleimageview:2.1.0' 32 | compile 'com.shizhefei:ViewPagerIndicator:1.1.2' 33 | compile 'com.shizhefei:MVCHelper-Library:1.0.6' 34 | compile project(path: ':library') 35 | } 36 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 28 | 32 | 36 | 40 | 41 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/ExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo; 2 | 3 | import android.app.Application; 4 | 5 | import com.shizhefei.view.multitype.ItemBinderFactory; 6 | import com.shizhefei.mutitypedemo.type.Food; 7 | import com.shizhefei.mutitypedemo.type.FoodProvider; 8 | import com.shizhefei.mutitypedemo.type.Good; 9 | import com.shizhefei.mutitypedemo.type.GoodProvider; 10 | import com.shizhefei.mutitypedemo.type.ImageItem; 11 | import com.shizhefei.mutitypedemo.type.ImageItemProvider; 12 | import com.shizhefei.mutitypedemo.type.RichItem; 13 | import com.shizhefei.mutitypedemo.type.RichItemProvider; 14 | import com.shizhefei.mutitypedemo.type.StringProvider; 15 | import com.squareup.leakcanary.LeakCanary; 16 | 17 | public class ExampleApplication extends Application { 18 | 19 | @Override 20 | public void onCreate() { 21 | super.onCreate(); 22 | LeakCanary.install(this); 23 | 24 | //这些代码可以放在应用初始化的时候,静态注册Provider 25 | //每个new ItemBinderFactory()实例共享这些注册的Provider 26 | ItemBinderFactory.registerStaticProvider(ImageItem.class, new ImageItemProvider()); 27 | ItemBinderFactory.registerStaticProvider(RichItem.class, new RichItemProvider()); 28 | ItemBinderFactory.registerStaticProvider(Food.class, new FoodProvider()); 29 | ItemBinderFactory.registerStaticProvider(Good.class, new GoodProvider()); 30 | ItemBinderFactory.registerStaticProvider(String.class, new StringProvider()); 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/BoomActivity.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.view.Gravity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.LinearLayout; 9 | 10 | /** 11 | * Created by LuckyJayce on 2016/8/8. 12 | */ 13 | public class BoomActivity extends Activity { 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | LinearLayout linearLayout = new LinearLayout(this); 18 | linearLayout.setGravity(Gravity.CENTER); 19 | setContentView(linearLayout); 20 | 21 | Button button = new Button(this); 22 | linearLayout.addView(button); 23 | button.setText("点我崩溃"); 24 | button.setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View v) { 27 | "".charAt(100); 28 | } 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/ChatActivity.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity; 2 | 3 | import android.os.Bundle; 4 | import android.os.PersistableBundle; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.LinearLayoutManager; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.view.View; 10 | import android.widget.EditText; 11 | 12 | import com.shizhefei.mutitypedemo.R; 13 | import com.shizhefei.mutitypedemo.type.Message; 14 | import com.shizhefei.mutitypedemo.type.MessageProvider; 15 | import com.shizhefei.view.multitype.ItemBinderFactory; 16 | import com.shizhefei.view.multitype.ItemViewProviderSet; 17 | import com.shizhefei.view.multitype.MultiTypeAdapter; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Collections; 21 | import java.util.List; 22 | 23 | public class ChatActivity extends AppCompatActivity { 24 | 25 | private RecyclerView recyclerView; 26 | private ItemBinderFactory itemBinderFactory; 27 | private String myUserId = "1"; 28 | private MultiTypeAdapter multiTypeAdapter; 29 | private EditText messageEditText; 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_chat); 35 | recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 36 | recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true)); 37 | 38 | itemBinderFactory = new ItemBinderFactory(); 39 | //有时候需要根据TextItem里面的某个字段,生成不同的布局.比如聊天界面的message是一样的,但是有区分左右布局 40 | //ItemProviderSet可以通过数据类型区分无数种情况的Provider 41 | itemBinderFactory.registerProvider(Message.class, new ItemViewProviderSet(new MessageProvider(MessageProvider.ALIGN_LEFT), new MessageProvider(MessageProvider.ALIGN_RIGHT)) { 42 | @Override 43 | protected int selectIndex(Message message) { 44 | return myUserId.equals(message.userId) ? 1 : 0; 45 | } 46 | }); 47 | 48 | List list = load(); 49 | Collections.reverse(list); 50 | recyclerView.setAdapter(multiTypeAdapter = new MultiTypeAdapter(list, itemBinderFactory)); 51 | 52 | messageEditText = (EditText) findViewById(R.id.message_editText); 53 | findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() { 54 | @Override 55 | public void onClick(View v) { 56 | String text = messageEditText.getText().toString(); 57 | multiTypeAdapter.getData().add(0, new Message(myUserId, text)); 58 | multiTypeAdapter.notifyItemInserted(0); 59 | 60 | messageEditText.getText().clear(); 61 | recyclerView.scrollToPosition(0); 62 | } 63 | }); 64 | } 65 | 66 | @Override 67 | public void onStateNotSaved() { 68 | super.onStateNotSaved(); 69 | FragmentManager fragmentManager; 70 | } 71 | 72 | @Override 73 | public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { 74 | super.onSaveInstanceState(outState, outPersistentState); 75 | } 76 | 77 | @Override 78 | protected void onStart() { 79 | super.onStart(); 80 | } 81 | 82 | @Override 83 | protected void onSaveInstanceState(Bundle outState) { 84 | super.onSaveInstanceState(outState); 85 | } 86 | 87 | private List load() { 88 | List message = new ArrayList<>(); 89 | message.add(new Message("1", "58秒95")); 90 | message.add(new Message("2", "58秒95?")); 91 | message.add(new Message("2", "自己都没想到自己")); 92 | message.add(new Message("2", "我以为是59秒")); 93 | message.add(new Message("2", "啊~~我有这么快??")); 94 | message.add(new Message("2", "我很满意")); 95 | message.add(new Message("1", "今天这个状态有所保留么?")); 96 | message.add(new Message("2", "没有保留!我已经,我已经用了洪荒之力啦!")); 97 | message.add(new Message("1", "是不是对明天的决赛充满希望")); 98 | message.add(new Message("2", "没有,我已经很满意啦。")); 99 | message.add(new Message("1", "明天加油")); 100 | message.add(new Message("2", "啦。。。啦。。。")); 101 | return message; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/LoadActivity.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity; 2 | 3 | import android.content.Intent; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.View; 9 | 10 | import com.shizhefei.mutitypedemo.R; 11 | import com.shizhefei.mutitypedemo.activity.longpage.LoadFragment; 12 | import com.shizhefei.view.multitype.ItemBinderFactory; 13 | import com.shizhefei.view.multitype.MultiTypeAdapter; 14 | import com.shizhefei.view.multitype.MultiTypeView; 15 | import com.shizhefei.view.multitype.provider.FragmentData; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | /** 21 | * Created by LuckyJayce on 2016/8/9. 22 | */ 23 | public class LoadActivity extends AppCompatActivity { 24 | 25 | private int page; 26 | private View refreshButton; 27 | private View loadMoreButton; 28 | private View insertAndRemoveButton; 29 | private View boomButton; 30 | private MultiTypeAdapter multiTypeAdapter; 31 | 32 | @Override 33 | protected void onCreate(@Nullable Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_long_page); 36 | 37 | refreshButton = findViewById(R.id.refresh); 38 | loadMoreButton = findViewById(R.id.loadMore); 39 | insertAndRemoveButton = findViewById(R.id.insertAndRemove); 40 | boomButton = findViewById(R.id.boom); 41 | MultiTypeView multiTypeView = (MultiTypeView) findViewById(R.id.recyclerView); 42 | 43 | //带有FragmentManager的构造函数,默认帮您添加支持Fragment数据的FragmentHolderProvider 44 | //也就是说你可以把Fragment放在adapter上使用,这里的Fragment只有第一次滑动到对应位置才会onCreateView的方法 45 | ItemBinderFactory factory = new ItemBinderFactory(getSupportFragmentManager()); 46 | multiTypeView.setAdapter(multiTypeAdapter = new MultiTypeAdapter(loadData(0), factory)); 47 | 48 | refreshButton.setOnClickListener(onClickListener); 49 | loadMoreButton.setOnClickListener(onClickListener); 50 | insertAndRemoveButton.setOnClickListener(onClickListener); 51 | boomButton.setOnClickListener(onClickListener); 52 | insertAndRemoveButton.setVisibility(View.GONE); 53 | } 54 | 55 | 56 | private View.OnClickListener onClickListener = new View.OnClickListener() { 57 | @Override 58 | public void onClick(View v) { 59 | if (v == refreshButton) { 60 | List data = loadData(0); 61 | multiTypeAdapter.notifyDataChanged(data, true); 62 | } else if (v == loadMoreButton) { 63 | List data = loadData(page + 1); 64 | multiTypeAdapter.notifyDataChanged(data, false); 65 | } else if (v == boomButton) { 66 | startActivity(new Intent(getApplicationContext(), BoomActivity.class)); 67 | } 68 | } 69 | }; 70 | 71 | private List loadData(int page) { 72 | List data = new ArrayList<>(); 73 | for (int i = 0; i < colors.length; i++) { 74 | FragmentData fragmentData = new FragmentData(LoadFragment.class, "LoadFragment" + page + i); 75 | fragmentData.putInt("color", colors[i]); 76 | fragmentData.putString("1", String.valueOf(page) + "#" + i); 77 | data.add(fragmentData); 78 | } 79 | this.page = page; 80 | return data; 81 | } 82 | 83 | private int[] colors = {Color.parseColor("#F44336"), Color.parseColor("#E91E63"), Color.parseColor("#3F51B5"), 84 | Color.parseColor("#2196F3"), Color.parseColor("#4CAF50"), Color.parseColor("#CDDC39"), 85 | Color.parseColor("#FFEB3B"), Color.parseColor("#FF9800"), Color.parseColor("#FF5722"), 86 | }; 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/LongPageActivity.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity; 2 | 3 | import android.content.Intent; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.Gravity; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | 12 | import com.shizhefei.mutitypedemo.R; 13 | import com.shizhefei.mutitypedemo.activity.longpagelazy.AirlineTicketLazyFragment; 14 | import com.shizhefei.mutitypedemo.activity.longpagelazy.CultureLazyFragment; 15 | import com.shizhefei.mutitypedemo.activity.longpagelazy.EditLazyFragment; 16 | import com.shizhefei.mutitypedemo.activity.longpagelazy.FoodLazyFragment; 17 | import com.shizhefei.mutitypedemo.activity.longpagelazy.HotelLazyFragment; 18 | import com.shizhefei.mutitypedemo.activity.longpagelazy.InfoLazyFragment; 19 | import com.shizhefei.mutitypedemo.activity.longpagelazy.RecommendLazyFragment; 20 | import com.shizhefei.mutitypedemo.activity.longpagelazy.ShopLazyFragment; 21 | import com.shizhefei.mutitypedemo.type.ImageItem; 22 | import com.shizhefei.mutitypedemo.type.RichItem; 23 | import com.shizhefei.mutitypedemo.util.DisplayUtils; 24 | import com.shizhefei.view.multitype.ItemBinderFactory; 25 | import com.shizhefei.view.multitype.MultiTypeAdapter; 26 | import com.shizhefei.view.multitype.MultiTypeView; 27 | import com.shizhefei.view.multitype.provider.FragmentData; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | public class LongPageActivity extends AppCompatActivity { 33 | private MultiTypeAdapter multiTypeAdapter; 34 | private int page; 35 | private View refreshButton; 36 | private View loadMoreButton; 37 | private View insertAndRemoveButton; 38 | private View boomButton; 39 | private MultiTypeView recyclerView; 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | setContentView(R.layout.activity_long_page); 45 | 46 | refreshButton = findViewById(R.id.refresh); 47 | loadMoreButton = findViewById(R.id.loadMore); 48 | insertAndRemoveButton = findViewById(R.id.insertAndRemove); 49 | boomButton = findViewById(R.id.boom); 50 | recyclerView = (MultiTypeView) findViewById(R.id.recyclerView); 51 | 52 | //recyclerView.setLayoutManager(new LinearLayoutManager(this)); 53 | //带有FragmentManager的构造函数,默认帮您添加支持Fragment数据的FragmentHolderProvider 54 | //也就是说你可以把Fragment放在adapter上使用,这里的Fragment只有第一次滑动到对应位置才会onCreateView的方法 55 | ItemBinderFactory factory = new ItemBinderFactory(getSupportFragmentManager()); 56 | multiTypeAdapter = new MultiTypeAdapter<>(loadData(0), factory); 57 | 58 | recyclerView.setAdapter(multiTypeAdapter); 59 | 60 | 61 | refreshButton.setOnClickListener(onClickListener); 62 | loadMoreButton.setOnClickListener(onClickListener); 63 | insertAndRemoveButton.setOnClickListener(onClickListener); 64 | boomButton.setOnClickListener(onClickListener); 65 | } 66 | 67 | private View.OnClickListener onClickListener = new View.OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | if (v == refreshButton) { 71 | List data = loadData(0); 72 | multiTypeAdapter.notifyDataChanged(data, true); 73 | } else if (v == loadMoreButton) { 74 | List data = loadData(page + 1); 75 | multiTypeAdapter.notifyDataChanged(data, false); 76 | } else if (v == insertAndRemoveButton) { 77 | multiTypeAdapter.getData().remove(3); 78 | multiTypeAdapter.notifyItemRemoved(3); 79 | multiTypeAdapter.getData().add(1, new RichItem("LLLLL", R.drawable.food_1)); 80 | multiTypeAdapter.notifyItemInserted(1); 81 | } else if (v == boomButton) { 82 | startActivity(new Intent(getApplicationContext(), BoomActivity.class)); 83 | } 84 | } 85 | }; 86 | 87 | private List loadData(int page) { 88 | List data = new ArrayList<>(); 89 | TextView textView = new TextView(this); 90 | textView.setText("第" + page + "页"); 91 | textView.setGravity(Gravity.CENTER); 92 | textView.setBackgroundColor(Color.GRAY); 93 | textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dipToPix(this, 100))); 94 | data.add(textView); 95 | for (int i = 0; i < 2; i++) { 96 | data.add("HHHHH"); 97 | data.add(new RichItem("LLLLL", R.drawable.food_1)); 98 | data.add(new ImageItem(R.mipmap.ic_launcher)); 99 | } 100 | 101 | data.add(new FragmentData(InfoLazyFragment.class, "InfoLazyFragment" + page)); 102 | data.add(new FragmentData(EditLazyFragment.class, "EditLazyFragment" + page)); 103 | data.add(new FragmentData(HotelLazyFragment.class, "HotelLazyFragment" + page)); 104 | data.add(new FragmentData(AirlineTicketLazyFragment.class, "AirlineTicketLazyFragment" + page)); 105 | data.add(new FragmentData(ShopLazyFragment.class, "ShopLazyFragment" + page)); 106 | data.add(new FragmentData(RecommendLazyFragment.class, "RecommendLazyFragment" + page)); 107 | data.add(new FragmentData(FoodLazyFragment.class, "FoodLazyFragment" + page)); 108 | data.add(new FragmentData(CultureLazyFragment.class, "CultureLazyFragment" + page)); 109 | 110 | this.page = page; 111 | return data; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/LongPageStateActivity.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.os.Parcelable; 6 | import android.support.v4.app.Fragment; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.View; 9 | 10 | import com.shizhefei.mutitypedemo.R; 11 | import com.shizhefei.mutitypedemo.activity.longpage.AirlineTicketFragment; 12 | import com.shizhefei.mutitypedemo.activity.longpage.CultureFragment; 13 | import com.shizhefei.mutitypedemo.activity.longpage.EditFragment; 14 | import com.shizhefei.mutitypedemo.activity.longpage.FoodFragment; 15 | import com.shizhefei.mutitypedemo.activity.longpage.HotelFragment; 16 | import com.shizhefei.mutitypedemo.activity.longpage.InfoFragment; 17 | import com.shizhefei.mutitypedemo.activity.longpage.RecommendFragment; 18 | import com.shizhefei.mutitypedemo.activity.longpage.ShopFragment; 19 | import com.shizhefei.view.multitype.ItemBinderFactory; 20 | import com.shizhefei.view.multitype.MultiTypeAdapter; 21 | import com.shizhefei.view.multitype.MultiTypeView; 22 | import com.shizhefei.view.multitype.data.SerializableData; 23 | import com.shizhefei.view.multitype.provider.FragmentData; 24 | 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | /** 29 | * 这里演示带有恢复状态的MultiTypeStateAdapter,onSaveInstanceState保存状态,onCreate(Bundle savedInstanceState)恢复状态
30 | * Fragment的恢复机制可以查看 EditLazyFragment
31 | */ 32 | public class LongPageStateActivity extends AppCompatActivity { 33 | private MultiTypeAdapter multiTypeStateAdapter; 34 | private int page; 35 | private View refreshButton; 36 | private View loadMoreButton; 37 | private View insertAndRemoveButton; 38 | private View boomButton; 39 | 40 | @Override 41 | protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_long_page); 44 | 45 | refreshButton = findViewById(R.id.refresh); 46 | loadMoreButton = findViewById(R.id.loadMore); 47 | insertAndRemoveButton = findViewById(R.id.insertAndRemove); 48 | boomButton = findViewById(R.id.boom); 49 | MultiTypeView multiTypeView = (MultiTypeView) findViewById(R.id.recyclerView); 50 | 51 | //带有FragmentManager的构造函数,默认帮您添加支持Fragment数据的FragmentHolderProvider 52 | //也就是说你可以把Fragment放在adapter上使用,这里的Fragment只有第一次滑动到对应位置才会onCreateView的方法 53 | ItemBinderFactory factory = new ItemBinderFactory(getSupportFragmentManager()); 54 | multiTypeStateAdapter = new MultiTypeAdapter<>(factory); 55 | 56 | //从savedInstanceState中恢复列表数据(onSaveInstanceState()方法保存的数据) 57 | if (MultiTypeAdapter.restoreState("data", multiTypeStateAdapter, savedInstanceState)) { 58 | page = savedInstanceState.getInt("page"); 59 | } else { 60 | //重新加载列表数据 61 | multiTypeStateAdapter.notifyDataChanged(loadData(0), false); 62 | } 63 | multiTypeView.setAdapter(multiTypeStateAdapter); 64 | 65 | refreshButton.setOnClickListener(onClickListener); 66 | loadMoreButton.setOnClickListener(onClickListener); 67 | insertAndRemoveButton.setOnClickListener(onClickListener); 68 | boomButton.setOnClickListener(onClickListener); 69 | insertAndRemoveButton.setVisibility(View.GONE); 70 | 71 | } 72 | 73 | private View.OnClickListener onClickListener = new View.OnClickListener() { 74 | @Override 75 | public void onClick(View v) { 76 | if (v == refreshButton) { 77 | List data = loadData(0); 78 | multiTypeStateAdapter.notifyDataChanged(data, true); 79 | } else if (v == loadMoreButton) { 80 | List data = loadData(page + 1); 81 | multiTypeStateAdapter.notifyDataChanged(data, false); 82 | } else if (v == boomButton) { 83 | startActivity(new Intent(getApplicationContext(), BoomActivity.class)); 84 | } 85 | } 86 | }; 87 | 88 | private List loadData(int page) { 89 | List data = new ArrayList<>(); 90 | 91 | data.add(new FragmentData(EditFragment.class, "EditFragment" + page)); 92 | 93 | //这里演示,可以添加任何的Serializable,以及保存数据,并且自动对应到数据类型的ItemViewProvider 94 | // 基本类型都是Serializable类型 95 | data.add(SerializableData.valueOf("HHHHH_1")); 96 | //data.add(SerializableData.valueOf(1));//没有注册int的ItemViewProvider,就不添加了演示了 97 | //Serializable数据列表转化为SerializableData的列表 98 | List texts = new ArrayList<>(); 99 | for (int i = 0; i < 3; i++) { 100 | texts.add(String.valueOf(i)); 101 | } 102 | data.addAll(SerializableData.valueOfList(texts)); 103 | 104 | data.add(new FragmentData(ShopFragment.class, "ShopFragment" + page)); 105 | 106 | //FragmentData 注意tag要唯一,用于查找对应的Fragment和恢复Fragment. 107 | data.add(new FragmentData(InfoFragment.class, "InfoFragment" + page)); 108 | data.add(new FragmentData(AirlineTicketFragment.class, "AirlineTicketFragment" + page)); 109 | data.add(new FragmentData(HotelFragment.class, "HotelFragment" + page)); 110 | data.add(new FragmentData(RecommendFragment.class, "RecommendFragment" + page)); 111 | data.add(new FragmentData(FoodFragment.class, "FoodFragment" + page)); 112 | 113 | 114 | FragmentData fragmentData = new FragmentData(CultureFragment.class, "CultureFragment" + page); 115 | //可以用这种方式获取已经创建的Fragment,不过这里获取到的是null,因为还没创建 116 | //那么什么时候创建?在RecyclerView滑到对应的Fragment位置才会创建. 117 | //你也可以通过multiTypeStateAdapter.getData().get(position) 获取对应的FragmentData,在通过FragmentData获取对应的Fragment. 118 | Fragment fragment = fragmentData.getFragment(); 119 | //为什么要用FragmentData的方式添加,因为它实现了Parcelable可以作为数据保存下来 120 | data.add(fragmentData); 121 | 122 | this.page = page; 123 | return data; 124 | } 125 | 126 | @Override 127 | protected void onSaveInstanceState(Bundle outState) { 128 | super.onSaveInstanceState(outState); 129 | outState.putInt("page", page); 130 | MultiTypeAdapter.saveState("data", multiTypeStateAdapter, outState); 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | 9 | import com.shizhefei.mutitypedemo.R; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | @Override 13 | protected void onCreate(@Nullable Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | findViewById(R.id.longPageState).setOnClickListener(new View.OnClickListener() { 17 | @Override 18 | public void onClick(View v) { 19 | startActivity(new Intent(getApplicationContext(), LongPageStateActivity.class)); 20 | } 21 | }); 22 | findViewById(R.id.longPage).setOnClickListener(new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | startActivity(new Intent(getApplicationContext(), LongPageActivity.class)); 26 | } 27 | }); 28 | findViewById(R.id.loading).setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | startActivity(new Intent(getApplicationContext(), LoadActivity.class)); 32 | } 33 | }); 34 | findViewById(R.id.chat).setOnClickListener(new View.OnClickListener() { 35 | @Override 36 | public void onClick(View v) { 37 | startActivity(new Intent(getApplicationContext(), ChatActivity.class)); 38 | } 39 | }); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/AirlineTicketFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | 11 | import com.shizhefei.mutitypedemo.R; 12 | 13 | /** 14 | * Created by LuckyJayce on 2016/8/8. 15 | */ 16 | public class AirlineTicketFragment extends Fragment { 17 | @Nullable 18 | @Override 19 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 20 | View view = inflater.inflate(R.layout.fragment_airlineticket, container, false); 21 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 22 | return view; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/CultureFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | 11 | import com.shizhefei.mutitypedemo.R; 12 | 13 | /** 14 | * Created by LuckyJayce on 2016/8/8. 15 | */ 16 | public class CultureFragment extends Fragment { 17 | @Nullable 18 | @Override 19 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 20 | View view = inflater.inflate(R.layout.fragment_culture, container, false); 21 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 22 | return view; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/EditFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v7.widget.GridLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.text.Editable; 9 | import android.text.TextWatcher; 10 | import android.util.Log; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.EditText; 15 | import android.widget.TextView; 16 | 17 | import com.shizhefei.mutitypedemo.R; 18 | import com.shizhefei.recyclerview.HFAdapter; 19 | import com.shizhefei.recyclerview.HFRecyclerAdapter; 20 | import com.shizhefei.view.multitype.ItemBinderFactory; 21 | import com.shizhefei.view.multitype.MultiTypeAdapter; 22 | 23 | import java.io.Serializable; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | /** 28 | * 这里演示Fragment的状态恢复机制,TextView和EditTextView都是实现了View的onSaveInstanceState和onRestoreInstanceState的方法 29 | * 所以它会自动帮助你恢复,不用你手动恢复界面,其他的View实现这两个方法也同理 30 | * 可以参考:http://ju.outofmemory.cn/entry/169700 31 | * Fragment主要保存逻辑上的数据,onSaveInstanceState去保存,onActivityCreated做恢复动作 32 | * Created by LuckyJayce on 2016/8/9. 33 | */ 34 | public class EditFragment extends Fragment { 35 | private EditText userNameEditText; 36 | private EditText passwordEditText; 37 | private TextView textView; 38 | private RecyclerView recyclerView; 39 | private MultiTypeAdapter multiTypeAdapter; 40 | private List datas; 41 | 42 | @Override 43 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 44 | super.onActivityCreated(savedInstanceState); 45 | } 46 | 47 | @Nullable 48 | @Override 49 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 50 | if (savedInstanceState != null) { 51 | datas = (List) savedInstanceState.getSerializable("data"); 52 | Log.d("cccc", getClass().getSimpleName()+" 读取:" + savedInstanceState); 53 | } 54 | 55 | View view = inflater.inflate(R.layout.fragment_edit, container, false); 56 | userNameEditText = (EditText) view.findViewById(R.id.editText); 57 | passwordEditText = (EditText) view.findViewById(R.id.editText2); 58 | textView = (TextView) view.findViewById(R.id.textView4); 59 | recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); 60 | 61 | //特别注意,recyclerView嵌套recyclerView,里面一层的recyclerView的view是不能被回收的,而且是一次性创建。 62 | //所以里面的recyclerView 适用于少量的item,大量的item还是建议放在外面的recyclerView去实现 63 | recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4)); 64 | 65 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 66 | 67 | userNameEditText.addTextChangedListener(textWatcher); 68 | if (datas == null) { 69 | datas = load(); 70 | } 71 | multiTypeAdapter = new MultiTypeAdapter(datas, new ItemBinderFactory()); 72 | HFRecyclerAdapter adapter = new HFRecyclerAdapter(multiTypeAdapter); 73 | adapter.setOnItemClickListener(onItemClickListener); 74 | recyclerView.setAdapter(adapter); 75 | return view; 76 | } 77 | 78 | @Override 79 | public void onSaveInstanceState(Bundle outState) { 80 | super.onSaveInstanceState(outState); 81 | Log.d("cccc", "EditLazyFragment 保存:" + this); 82 | List datas = multiTypeAdapter.getData(); 83 | outState.putSerializable("data", (Serializable) datas); 84 | } 85 | 86 | private HFAdapter.OnItemClickListener onItemClickListener = new HFAdapter.OnItemClickListener() { 87 | @Override 88 | public void onItemClick(HFAdapter adapter, RecyclerView.ViewHolder vh, int position) { 89 | if (position >= 0) { 90 | multiTypeAdapter.getData().remove(position); 91 | multiTypeAdapter.notifyItemRemoved(position); 92 | } 93 | } 94 | }; 95 | 96 | private TextWatcher textWatcher = new TextWatcher() { 97 | @Override 98 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 99 | 100 | } 101 | 102 | @Override 103 | public void onTextChanged(CharSequence s, int start, int before, int count) { 104 | 105 | } 106 | 107 | @Override 108 | public void afterTextChanged(Editable s) { 109 | textView.setText(s.toString()); 110 | } 111 | }; 112 | 113 | private List load() { 114 | List list = new ArrayList<>(); 115 | for (int i = 0; i < 12; i++) { 116 | list.add(String.valueOf(i)); 117 | } 118 | return list; 119 | } 120 | 121 | 122 | } 123 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/FoodFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v7.widget.GridLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.util.Log; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | 13 | import com.shizhefei.mutitypedemo.R; 14 | import com.shizhefei.mutitypedemo.type.Food; 15 | import com.shizhefei.view.multitype.ItemBinderFactory; 16 | import com.shizhefei.view.multitype.MultiTypeAdapter; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * Created by LuckyJayce on 2016/8/8. 23 | */ 24 | public class FoodFragment extends Fragment { 25 | private RecyclerView recyclerView; 26 | 27 | @Nullable 28 | @Override 29 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 30 | View view = inflater.inflate(R.layout.fragment_food, container, false); 31 | recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); 32 | recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3)); 33 | 34 | recyclerView.setAdapter(new MultiTypeAdapter(load(), new ItemBinderFactory())); 35 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 36 | return view; 37 | } 38 | 39 | private List load() { 40 | List foods = new ArrayList<>(); 41 | for (int i = 0; i < 10; i++) { 42 | foods.add(new Food()); 43 | } 44 | return foods; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/HotelFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | 12 | import com.shizhefei.mutitypedemo.R; 13 | 14 | /** 15 | * Created by LuckyJayce on 2016/8/8. 16 | */ 17 | public class HotelFragment extends Fragment { 18 | private View addButton; 19 | private View minusButton; 20 | private TextView numberTextView; 21 | private int number = 1; 22 | 23 | @Nullable 24 | @Override 25 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 26 | View view = inflater.inflate(R.layout.fragment_hotel, container, false); 27 | addButton = view.findViewById(R.id.add_button); 28 | minusButton = view.findViewById(R.id.minus_button); 29 | numberTextView = (TextView) view.findViewById(R.id.number_textView); 30 | 31 | addButton.setOnClickListener(onClickListener); 32 | minusButton.setOnClickListener(onClickListener); 33 | 34 | numberTextView.setText(String.valueOf(number)); 35 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 36 | return view; 37 | } 38 | 39 | private View.OnClickListener onClickListener = new View.OnClickListener() { 40 | @Override 41 | public void onClick(View v) { 42 | if (v == addButton) { 43 | number++; 44 | numberTextView.setText(String.valueOf(number)); 45 | } else if (v == minusButton) { 46 | if (number > 0) { 47 | number--; 48 | numberTextView.setText(String.valueOf(number)); 49 | } 50 | } 51 | } 52 | }; 53 | } 54 | 55 | 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/InfoFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | 11 | import com.shizhefei.mutitypedemo.R; 12 | 13 | /** 14 | * Created by LuckyJayce on 2016/8/8. 15 | */ 16 | public class InfoFragment extends Fragment { 17 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 18 | View view = inflater.inflate(R.layout.fragment_info, container, false); 19 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 20 | return view; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/LoadFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | 12 | import com.shizhefei.mutitypedemo.R; 13 | 14 | 15 | public class LoadFragment extends Fragment { 16 | private View progressBar; 17 | private String i; 18 | private TextView textView; 19 | 20 | @Nullable 21 | @Override 22 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 23 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 24 | i = getArguments().getString("1"); 25 | int color = getArguments().getInt("color"); 26 | View view = inflater.inflate(R.layout.test, container, false); 27 | view.setBackgroundColor(color); 28 | progressBar = view.findViewById(R.id.progressBar); 29 | textView = (TextView) view.findViewById(R.id.textView); 30 | textView.setText(i); 31 | textView.append(" 3秒后加载完毕"); 32 | textView.postDelayed(new Runnable() { 33 | @Override 34 | public void run() { 35 | textView.setText(i); 36 | textView.append(" 2秒后加载完毕"); 37 | } 38 | }, 1000); 39 | textView.postDelayed(new Runnable() { 40 | @Override 41 | public void run() { 42 | textView.setText(i); 43 | textView.append(" 1秒后加载完毕"); 44 | } 45 | }, 2000); 46 | textView.postDelayed(new Runnable() { 47 | @Override 48 | public void run() { 49 | progressBar.setVisibility(View.GONE); 50 | textView.setText(i); 51 | textView.append(" 加载完毕"); 52 | } 53 | }, 3000); 54 | return view; 55 | } 56 | 57 | @Override 58 | public void onDestroyView() { 59 | super.onDestroyView(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/RecommendFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | 11 | import com.shizhefei.mutitypedemo.R; 12 | 13 | /** 14 | * Created by LuckyJayce on 2016/8/8. 15 | */ 16 | public class RecommendFragment extends Fragment { 17 | 18 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 19 | View view = inflater.inflate(R.layout.fragment_recommend, container, false); 20 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 21 | return view; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpage/ShopFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpage; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.util.Log; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | 13 | import com.shizhefei.mutitypedemo.R; 14 | import com.shizhefei.mutitypedemo.type.Good; 15 | import com.shizhefei.view.multitype.ItemBinderFactory; 16 | import com.shizhefei.view.multitype.MultiTypeAdapter; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | /** 22 | * Created by LuckyJayce on 2016/8/8. 23 | */ 24 | public class ShopFragment extends Fragment { 25 | private RecyclerView recyclerView; 26 | 27 | @Nullable 28 | @Override 29 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 30 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 31 | View view = inflater.inflate(R.layout.fragment_shop, container, false); 32 | recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); 33 | 34 | //特别注意,recyclerView嵌套recyclerView,里面一层的recyclerView的view是不能被回收的,而且是一次性创建。 35 | //所以里面的recyclerView 适用于少量的item,大量的item还是建议放在外面的recyclerView去实现 36 | recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 37 | 38 | recyclerView.setAdapter(new MultiTypeAdapter(load(), new ItemBinderFactory())); 39 | 40 | // FragmentStatePagerAdapter 41 | 42 | return view; 43 | } 44 | 45 | private List load() { 46 | List foods = new ArrayList<>(); 47 | for (int i = 0; i < 6; i++) { 48 | foods.add(new Good()); 49 | } 50 | return foods; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/AirlineTicketLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.shizhefei.fragment.LazyFragment; 7 | import com.shizhefei.mutitypedemo.R; 8 | 9 | /** 10 | * Created by LuckyJayce on 2016/8/8. 11 | */ 12 | public class AirlineTicketLazyFragment extends LazyFragment { 13 | 14 | @Override 15 | protected void onCreateViewLazy(Bundle savedInstanceState) { 16 | super.onCreateViewLazy(savedInstanceState); 17 | setContentView(R.layout.fragment_airlineticket); 18 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/CultureLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.shizhefei.fragment.LazyFragment; 7 | import com.shizhefei.mutitypedemo.R; 8 | 9 | /** 10 | * Created by LuckyJayce on 2016/8/8. 11 | */ 12 | public class CultureLazyFragment extends LazyFragment { 13 | @Override 14 | protected void onCreateViewLazy(Bundle savedInstanceState) { 15 | super.onCreateViewLazy(savedInstanceState); 16 | setContentView(R.layout.fragment_culture); 17 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/EditLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.text.Editable; 8 | import android.text.TextWatcher; 9 | import android.util.Log; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.widget.EditText; 14 | import android.widget.TextView; 15 | 16 | import com.shizhefei.fragment.LazyFragment; 17 | import com.shizhefei.mutitypedemo.R; 18 | import com.shizhefei.recyclerview.HFAdapter; 19 | import com.shizhefei.recyclerview.HFRecyclerAdapter; 20 | import com.shizhefei.view.multitype.ItemBinderFactory; 21 | import com.shizhefei.view.multitype.MultiTypeAdapter; 22 | 23 | import java.io.Serializable; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | /** 28 | * 这里演示Fragment的状态恢复机制,TextView和EditTextView都是实现了View的onSaveInstanceState和onRestoreInstanceState的方法 29 | * 所以它会自动帮助你恢复,不用你手动恢复界面,其他的View实现这两个方法也同理 30 | * 可以参考:http://ju.outofmemory.cn/entry/169700 31 | * Fragment主要保存逻辑上的数据,onSaveInstanceState去保存,onActivityCreated做恢复动作 32 | * Created by LuckyJayce on 2016/8/9. 33 | */ 34 | public class EditLazyFragment extends LazyFragment { 35 | private EditText userNameEditText; 36 | private EditText passwordEditText; 37 | private TextView textView; 38 | private RecyclerView recyclerView; 39 | private MultiTypeAdapter multiTypeAdapter; 40 | private List datas; 41 | 42 | @Override 43 | protected View getPreviewLayout(LayoutInflater inflater, ViewGroup container) { 44 | return inflater.inflate(R.layout.fragment_edit, container, false); 45 | } 46 | 47 | @Override 48 | protected void onCreateViewLazy(Bundle savedInstanceState) { 49 | super.onCreateViewLazy(savedInstanceState); 50 | // setContentView(R.layout.fragment_edit); 51 | userNameEditText = (EditText) findViewById(R.id.editText); 52 | passwordEditText = (EditText) findViewById(R.id.editText2); 53 | textView = (TextView) findViewById(R.id.textView4); 54 | recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 55 | 56 | //特别注意,recyclerView嵌套recyclerView,里面一层的recyclerView的view是不能被回收的,而且是一次性创建。 57 | //所以里面的recyclerView 适用于少量的item,大量的item还是建议放在外面的recyclerView去实现 58 | recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4)); 59 | 60 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 61 | 62 | userNameEditText.addTextChangedListener(textWatcher); 63 | if (datas == null) { 64 | datas = load(); 65 | } 66 | multiTypeAdapter = new MultiTypeAdapter(datas, new ItemBinderFactory()); 67 | HFRecyclerAdapter adapter = new HFRecyclerAdapter(multiTypeAdapter); 68 | adapter.setOnItemClickListener(onItemClickListener); 69 | recyclerView.setAdapter(adapter); 70 | } 71 | 72 | @Override 73 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 74 | super.onActivityCreated(savedInstanceState); 75 | if (savedInstanceState != null) { 76 | datas = (List) savedInstanceState.getSerializable("data"); 77 | Log.d("cccc", "EditLazyFragment 读取:" + savedInstanceState); 78 | } 79 | } 80 | 81 | @Override 82 | public void onSaveInstanceState(Bundle outState) { 83 | super.onSaveInstanceState(outState); 84 | if (multiTypeAdapter != null) { 85 | Log.d("cccc", "EditLazyFragment 保存:" + this); 86 | List datas = multiTypeAdapter.getData(); 87 | outState.putSerializable("data", (Serializable) datas); 88 | } 89 | } 90 | 91 | private HFAdapter.OnItemClickListener onItemClickListener = new HFAdapter.OnItemClickListener() { 92 | @Override 93 | public void onItemClick(HFAdapter adapter, RecyclerView.ViewHolder vh, int position) { 94 | if (position >= 0) { 95 | multiTypeAdapter.getData().remove(position); 96 | multiTypeAdapter.notifyItemRemoved(position); 97 | } 98 | } 99 | }; 100 | 101 | private TextWatcher textWatcher = new TextWatcher() { 102 | @Override 103 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 104 | 105 | } 106 | 107 | @Override 108 | public void onTextChanged(CharSequence s, int start, int before, int count) { 109 | 110 | } 111 | 112 | @Override 113 | public void afterTextChanged(Editable s) { 114 | textView.setText(s.toString()); 115 | } 116 | }; 117 | 118 | private List load() { 119 | List list = new ArrayList<>(); 120 | for (int i = 0; i < 12; i++) { 121 | list.add(String.valueOf(i)); 122 | } 123 | return list; 124 | } 125 | 126 | 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/FoodLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.widget.GridLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.util.Log; 7 | 8 | import com.shizhefei.fragment.LazyFragment; 9 | import com.shizhefei.mutitypedemo.R; 10 | import com.shizhefei.mutitypedemo.type.Food; 11 | import com.shizhefei.view.multitype.ItemBinderFactory; 12 | import com.shizhefei.view.multitype.MultiTypeAdapter; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * Created by LuckyJayce on 2016/8/8. 19 | */ 20 | public class FoodLazyFragment extends LazyFragment { 21 | private RecyclerView recyclerView; 22 | 23 | @Override 24 | protected void onCreateViewLazy(Bundle savedInstanceState) { 25 | super.onCreateViewLazy(savedInstanceState); 26 | setContentView(R.layout.fragment_food); 27 | recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 28 | recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3)); 29 | 30 | recyclerView.setAdapter(new MultiTypeAdapter(load(), new ItemBinderFactory())); 31 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 32 | } 33 | 34 | private List load() { 35 | List foods = new ArrayList<>(); 36 | for (int i = 0; i < 10; i++) { 37 | foods.add(new Food()); 38 | } 39 | return foods; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/HotelLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | 8 | import com.shizhefei.fragment.LazyFragment; 9 | import com.shizhefei.mutitypedemo.R; 10 | 11 | /** 12 | * Created by LuckyJayce on 2016/8/8. 13 | */ 14 | public class HotelLazyFragment extends LazyFragment { 15 | private View addButton; 16 | private View minusButton; 17 | private TextView numberTextView; 18 | private int number = 1; 19 | 20 | @Override 21 | protected void onCreateViewLazy(Bundle savedInstanceState) { 22 | super.onCreateViewLazy(savedInstanceState); 23 | setContentView(R.layout.fragment_hotel); 24 | addButton = findViewById(R.id.add_button); 25 | minusButton = findViewById(R.id.minus_button); 26 | numberTextView = (TextView) findViewById(R.id.number_textView); 27 | 28 | addButton.setOnClickListener(onClickListener); 29 | minusButton.setOnClickListener(onClickListener); 30 | 31 | numberTextView.setText(String.valueOf(number)); 32 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 33 | } 34 | 35 | private View.OnClickListener onClickListener = new View.OnClickListener() { 36 | @Override 37 | public void onClick(View v) { 38 | if (v == addButton) { 39 | number++; 40 | numberTextView.setText(String.valueOf(number)); 41 | } else if (v == minusButton) { 42 | if (number > 0) { 43 | number--; 44 | numberTextView.setText(String.valueOf(number)); 45 | } 46 | } 47 | } 48 | }; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/InfoLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.shizhefei.fragment.LazyFragment; 7 | import com.shizhefei.mutitypedemo.R; 8 | 9 | /** 10 | * Created by LuckyJayce on 2016/8/8. 11 | */ 12 | public class InfoLazyFragment extends LazyFragment { 13 | @Override 14 | protected void onCreateViewLazy(Bundle savedInstanceState) { 15 | super.onCreateViewLazy(savedInstanceState); 16 | setContentView(R.layout.fragment_info); 17 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/LoadLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | 8 | import com.shizhefei.fragment.LazyFragment; 9 | import com.shizhefei.mutitypedemo.R; 10 | 11 | 12 | public class LoadLazyFragment extends LazyFragment { 13 | private View progressBar; 14 | private String i; 15 | private TextView textView; 16 | 17 | @Override 18 | protected void onCreateViewLazy(Bundle savedInstanceState) { 19 | super.onCreateViewLazy(savedInstanceState); 20 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 21 | setContentView(R.layout.test); 22 | i = getArguments().getString("1"); 23 | int color = getArguments().getInt("color"); 24 | getContentView().setBackgroundColor(color); 25 | progressBar = findViewById(R.id.progressBar); 26 | textView = (TextView) findViewById(R.id.textView); 27 | textView.setText(i); 28 | textView.append(" 3秒后加载完毕"); 29 | textView.postDelayed(new Runnable() { 30 | @Override 31 | public void run() { 32 | textView.setText(i); 33 | textView.append(" 2秒后加载完毕"); 34 | } 35 | }, 1000); 36 | textView.postDelayed(new Runnable() { 37 | @Override 38 | public void run() { 39 | textView.setText(i); 40 | textView.append(" 1秒后加载完毕"); 41 | } 42 | }, 2000); 43 | textView.postDelayed(new Runnable() { 44 | @Override 45 | public void run() { 46 | progressBar.setVisibility(View.GONE); 47 | textView.setText(i); 48 | textView.append(" 加载完毕"); 49 | } 50 | }, 3000); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/RecommendLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.shizhefei.fragment.LazyFragment; 7 | import com.shizhefei.mutitypedemo.R; 8 | 9 | /** 10 | * Created by LuckyJayce on 2016/8/8. 11 | */ 12 | public class RecommendLazyFragment extends LazyFragment { 13 | 14 | @Override 15 | protected void onCreateViewLazy(Bundle savedInstanceState) { 16 | super.onCreateViewLazy(savedInstanceState); 17 | setContentView(R.layout.fragment_recommend); 18 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/activity/longpagelazy/ShopLazyFragment.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.activity.longpagelazy; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.widget.LinearLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.util.Log; 7 | 8 | import com.shizhefei.fragment.LazyFragment; 9 | import com.shizhefei.mutitypedemo.R; 10 | import com.shizhefei.mutitypedemo.type.Good; 11 | import com.shizhefei.view.multitype.ItemBinderFactory; 12 | import com.shizhefei.view.multitype.MultiTypeAdapter; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * Created by LuckyJayce on 2016/8/8. 19 | */ 20 | public class ShopLazyFragment extends LazyFragment { 21 | private RecyclerView recyclerView; 22 | 23 | @Override 24 | protected void onCreateViewLazy(Bundle savedInstanceState) { 25 | super.onCreateViewLazy(savedInstanceState); 26 | 27 | Log.d("dddd", "onCreateView :" + getClass().getSimpleName() + " " + savedInstanceState); 28 | 29 | setContentView(R.layout.fragment_shop); 30 | recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 31 | //特别注意,recyclerView嵌套recyclerView,里面一层的recyclerView的view是不能被回收的,而且是一次性创建。 32 | //所以里面的recyclerView 适用于少量的item,大量的item还是建议放在外面的recyclerView去实现 33 | recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 34 | 35 | recyclerView.setAdapter(new MultiTypeAdapter(load(), new ItemBinderFactory())); 36 | } 37 | 38 | private List load() { 39 | List foods = new ArrayList<>(); 40 | for (int i = 0; i < 6; i++) { 41 | foods.add(new Good()); 42 | } 43 | return foods; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/Food.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | /** 4 | * Created by LuckyJayce on 2016/8/8. 5 | */ 6 | public class Food { 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/FoodProvider.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.ViewGroup; 6 | 7 | import com.shizhefei.view.multitype.ItemViewProvider; 8 | import com.shizhefei.mutitypedemo.R; 9 | 10 | /** 11 | * Created by LuckyJayce on 2016/8/8. 12 | */ 13 | public class FoodProvider extends ItemViewProvider { 14 | 15 | @Override 16 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int providerType) { 17 | return new RecyclerView.ViewHolder(inflater.inflate(R.layout.item_food, parent, false)) { 18 | }; 19 | } 20 | 21 | @Override 22 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Food food) { 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/Good.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | /** 4 | * Created by LuckyJayce on 2016/8/8. 5 | */ 6 | public class Good { 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/GoodProvider.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.ViewGroup; 6 | 7 | import com.shizhefei.view.multitype.ItemViewProvider; 8 | import com.shizhefei.mutitypedemo.R; 9 | 10 | /** 11 | * Created by LuckyJayce on 2016/8/8. 12 | */ 13 | public class GoodProvider extends ItemViewProvider { 14 | 15 | @Override 16 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int providerType) { 17 | return new RecyclerView.ViewHolder(inflater.inflate(R.layout.item_good, parent, false)) { 18 | }; 19 | } 20 | 21 | @Override 22 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Good good) { 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/ImageItem.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | public class ImageItem { 4 | 5 | public final int resId; 6 | 7 | public ImageItem(int resId) { 8 | this.resId = resId; 9 | } 10 | } -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/ImageItemProvider.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.ImageView; 8 | 9 | import com.shizhefei.view.multitype.ItemViewProvider; 10 | import com.shizhefei.mutitypedemo.R; 11 | 12 | public class ImageItemProvider extends ItemViewProvider { 13 | 14 | @Override 15 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent,int providerType) { 16 | View root = inflater.inflate(R.layout.item_image, parent, false); 17 | return new ItemViewHolder(root); 18 | } 19 | 20 | @Override 21 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, ImageItem data) { 22 | ItemViewHolder holder = (ItemViewHolder) viewHolder; 23 | holder.imageView.setImageResource(data.resId); 24 | } 25 | 26 | private class ItemViewHolder extends RecyclerView.ViewHolder { 27 | private final ImageView imageView; 28 | 29 | public ItemViewHolder(View itemView) { 30 | super(itemView); 31 | imageView = (ImageView) itemView.findViewById(R.id.image); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/Message.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | /** 4 | * Created by LuckyJayce on 2016/8/8. 5 | */ 6 | public class Message { 7 | public String userId; 8 | public String text; 9 | 10 | public Message(String userId, String text) { 11 | this.userId = userId; 12 | this.text = text; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/MessageProvider.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.TextView; 8 | 9 | import com.shizhefei.mutitypedemo.R; 10 | import com.shizhefei.view.multitype.ItemViewProvider; 11 | 12 | /** 13 | * Created by LuckyJayce on 2016/8/8. 14 | */ 15 | public class MessageProvider extends ItemViewProvider { 16 | public static final int ALIGN_LEFT = 0; 17 | public static final int ALIGN_RIGHT = 1; 18 | private int align; 19 | 20 | public MessageProvider(int align) { 21 | this.align = align; 22 | } 23 | 24 | @Override 25 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int providerType) { 26 | if (align == ALIGN_LEFT) { 27 | return new ItemViewHolder(inflater.inflate(R.layout.item_message_left, parent, false)); 28 | } 29 | return new ItemViewHolder(inflater.inflate(R.layout.item_message_right, parent, false)); 30 | } 31 | 32 | @Override 33 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Message message) { 34 | ItemViewHolder vh = (ItemViewHolder) viewHolder; 35 | vh.textView.setText(message.text); 36 | } 37 | 38 | private class ItemViewHolder extends RecyclerView.ViewHolder { 39 | 40 | private final TextView textView; 41 | 42 | public ItemViewHolder(View itemView) { 43 | super(itemView); 44 | textView = (TextView) itemView.findViewById(R.id.item_message_textView); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/RichItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 drakeet. https://github.com/drakeet 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.shizhefei.mutitypedemo.type; 18 | 19 | import android.support.annotation.NonNull; 20 | 21 | /** 22 | * @author drakeet 23 | */ 24 | public class RichItem { 25 | 26 | @NonNull 27 | public String text; 28 | public int imageResId; 29 | 30 | 31 | public RichItem(@NonNull String text, int imageResId) { 32 | this.text = text; 33 | this.imageResId = imageResId; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/RichItemProvider.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.ImageView; 8 | import android.widget.TextView; 9 | 10 | import com.shizhefei.view.multitype.ItemViewProvider; 11 | import com.shizhefei.mutitypedemo.R; 12 | 13 | public class RichItemProvider extends ItemViewProvider { 14 | 15 | @Override 16 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int providerType) { 17 | View root = inflater.inflate(R.layout.item_rich, parent, false); 18 | return new ItemViewHolder(root); 19 | } 20 | 21 | @Override 22 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, RichItem data) { 23 | ItemViewHolder holder = (ItemViewHolder) viewHolder; 24 | holder.text.setText(data.text); 25 | holder.image.setImageResource(data.imageResId); 26 | } 27 | 28 | private class ItemViewHolder extends RecyclerView.ViewHolder { 29 | private final TextView text; 30 | private final ImageView image; 31 | 32 | public ItemViewHolder(View itemView) { 33 | super(itemView); 34 | this.text = (TextView) itemView.findViewById(R.id.text); 35 | this.image = (ImageView) itemView.findViewById(R.id.image); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/type/StringProvider.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.type; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.ViewGroup; 6 | import android.widget.TextView; 7 | 8 | import com.shizhefei.view.multitype.ItemViewProvider; 9 | import com.shizhefei.mutitypedemo.R; 10 | 11 | /** 12 | * Created by LuckyJayce on 2016/8/9. 13 | */ 14 | public class StringProvider extends ItemViewProvider { 15 | 16 | @Override 17 | public RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int providerType) { 18 | return new RecyclerView.ViewHolder(inflater.inflate(R.layout.item_string, parent, false)) { 19 | }; 20 | } 21 | 22 | @Override 23 | public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, String s) { 24 | TextView textView = (TextView) viewHolder.itemView; 25 | textView.setText(s); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/shizhefei/mutitypedemo/util/DisplayUtils.java: -------------------------------------------------------------------------------- 1 | package com.shizhefei.mutitypedemo.util; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.ClipData; 5 | import android.content.Context; 6 | import android.content.res.Configuration; 7 | import android.graphics.Bitmap; 8 | import android.graphics.Bitmap.Config; 9 | import android.graphics.Canvas; 10 | import android.graphics.Paint; 11 | import android.graphics.Paint.FontMetricsInt; 12 | import android.graphics.RectF; 13 | import android.os.Build; 14 | import android.provider.Settings.Secure; 15 | import android.util.DisplayMetrics; 16 | import android.util.TypedValue; 17 | 18 | import java.util.Locale; 19 | 20 | public class DisplayUtils { 21 | 22 | public static boolean isTablet(Context context) { 23 | return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; 24 | } 25 | 26 | private static int deviceType = -1; 27 | 28 | 29 | /** 30 | * 根据dip值转化成px值 31 | * 32 | * @param context 33 | * @param dip 34 | * @return 35 | */ 36 | public static int dipToPix(Context context, float dip) { 37 | int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics()); 38 | return size; 39 | } 40 | 41 | public static float spToPx(Context context, float sp) { 42 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); 43 | } 44 | 45 | public static int getDimenValue(Context context, int dimenId) { 46 | return (int) context.getResources().getDimension(dimenId); 47 | } 48 | 49 | public static DisplayMetrics getDisplayMetrics(Context context) { 50 | // DisplayMetrics dm = new DisplayMetrics(); 51 | // activity.getResources().getDisplayMetrics(); 52 | // activity.getWindowManager().getDefaultDisplay().getMetrics(dm); 53 | return context.getResources().getDisplayMetrics(); 54 | } 55 | 56 | /** 57 | * 判断设备是否是模拟器 58 | * 59 | * @return 60 | */ 61 | public static boolean isEmulator() { 62 | return "sdk".equals(Build.PRODUCT) || "google_sdk".equals(Build.PRODUCT) || "generic".equals(Build.BRAND.toLowerCase(Locale.getDefault())); 63 | } 64 | 65 | /** 66 | * 得到设备id 67 | * 68 | * @param context 69 | * @return 70 | */ 71 | public static String getAndroidId(Context context) { 72 | String android_id = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID); 73 | return android_id; 74 | } 75 | 76 | public static int getStatusBarHeight(Context context) { 77 | int result = 0; 78 | int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); 79 | if (resourceId > 0) { 80 | result = context.getResources().getDimensionPixelSize(resourceId); 81 | } 82 | return result; 83 | } 84 | 85 | public static Bitmap createTextBitmap(Context context, String str, int bgColor, int fontColot, float fontSizePx) { 86 | Paint paint = new Paint(); 87 | paint.setTextSize(fontSizePx); 88 | int w = (int) paint.measureText(str);// 计算文字实际占用的宽度 89 | int margin = (int) (fontSizePx * 0.2f); 90 | int bgHeight = (int) (fontSizePx + margin * 2);// 将高度+10防止绘制椭圆时左右的文字超出椭圆范围 91 | int bgWitdh = w + margin * 2; 92 | Bitmap bm = Bitmap.createBitmap(bgWitdh, bgHeight, Config.ARGB_4444); 93 | Canvas c = new Canvas(bm); 94 | 95 | Paint p2 = new Paint(); 96 | RectF re = new RectF(0, 0, bgWitdh, bgHeight);// 矩形 97 | float roundPx = margin * 2; 98 | p2.setAntiAlias(true);// 设置Paint为无锯齿 99 | c.drawARGB(0, 0, 0, 0);// 透明色 100 | p2.setColor(bgColor); 101 | c.drawRoundRect(re, roundPx, roundPx, p2);// 绘制圆角矩形 102 | Paint p1 = new Paint(); 103 | p1.setColor(fontColot); 104 | p1.setTextSize(fontSizePx); 105 | p1.setTextAlign(Paint.Align.CENTER); 106 | FontMetricsInt fontMetrics = paint.getFontMetricsInt(); 107 | int baseline = (int) (re.top + (re.bottom - re.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top); 108 | c.drawText(str, re.centerX(), baseline, p1); 109 | c.save(Canvas.ALL_SAVE_FLAG); 110 | c.restore(); 111 | return bm; 112 | } 113 | 114 | @SuppressWarnings("deprecation") 115 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 116 | public static void copyToClipboard(Context context, String text) { 117 | // if() 118 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 119 | android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 120 | clipboard.setText(text); 121 | } else { 122 | android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 123 | clipboard.setPrimaryClip(ClipData.newPlainText(null, text)); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/airlinetiket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/airlinetiket.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/back.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/food_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/food_1.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/go.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/go.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/good_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/good_1.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/hotel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/hotel.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/hotel_pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/hotel_pic.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/message_left_bg.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/message_left_bg.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/message_right_bg.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/message_right_bg.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/sanya.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/sanya.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/to.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/to.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/user_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/user_a.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/user_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LuckyJayce/MultiTypeView/a2728058bc63a9575d4da12c345e5fcee2fab0cc/app/src/main/res/drawable-xhdpi/user_b.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/square_border.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_chat.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 26 | 27 | 33 | 34 | 35 | 36 | 41 | 42 | 47 | 48 | 59 | 60 |