diff --git a/.gitignore b/.gitignore index a43ba82..08daad3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,9 @@ -.gradle -/local.properties -/.idea/workspace.xml -/.idea/libraries -.DS_Store -/build -/captures -*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +*.iml .idea \ No newline at end of file diff --git a/LICENSE b/LICENSE index 8f71f43..113e843 100644 --- a/LICENSE +++ b/LICENSE @@ -1,202 +1,202 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "{}" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright {yyyy} {name of copyright owner} - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/README.md b/README.md index 31f99a2..3d39564 100644 --- a/README.md +++ b/README.md @@ -1,168 +1,190 @@ -# base-adapter -Android 万能的Adapter for ListView,RecyclerView,GridView等,支持多种Item类型的情况。 - - -## 引入 - -### ForRecyclerView - -``` -compile 'com.zhy:base-rvadapter:3.0.2' -``` - -### ForListView - -``` -compile 'com.zhy:base-adapter:3.0.2' -``` - - -## 使用 - -##(1)简单的数据绑定(ListView与其使用方式一致) - -首先看我们最常用的单种Item的书写方式: - -``` -mRecyclerView.setAdapter(new CommonAdapter(this, R.layout.item_list, mDatas) -{ - @Override - public void convert(ViewHolder holder, String s) - { - holder.setText(R.id.id_item_list_title, s); - } -}); -``` -是不是相当方便,在convert方法中完成数据、事件绑定即可。 - - -只需要简单的将Adapter继承CommonAdapter,复写convert方法即可。省去了自己编写ViewHolder等大量的重复的代码。 - -* 可以通过holder.getView(id)拿到任何控件。 -* ViewHolder中封装了大量的常用的方法,比如holder.setText(id,text),holder.setOnClickListener(id,listener)等,可以支持使用。 - -效果图: - - - -##(2)多种ItemViewType(ListView与其使用方式一致) - -对于多中itemviewtype的处理参考:https://github.com/sockeqwe/AdapterDelegates ,具有极高的扩展性。 - - -``` -MultiItemTypeAdapter adapter = new MultiItemTypeAdapter(this,mDatas); -adapter.addItemViewDelegate(new MsgSendItemDelagate()); -adapter.addItemViewDelegate(new MsgComingItemDelagate()); -``` - -每种Item类型对应一个ItemViewDelegete,例如: - -``` -public class MsgComingItemDelagate implements ItemViewDelegate -{ - - @Override - public int getItemViewLayoutId() - { - return R.layout.main_chat_from_msg; - } - - @Override - public boolean isForViewType(ChatMessage item, int position) - { - return item.isComMeg(); - } - - @Override - public void convert(ViewHolder holder, ChatMessage chatMessage, int position) - { - holder.setText(R.id.chat_from_content, chatMessage.getContent()); - holder.setText(R.id.chat_from_name, chatMessage.getName()); - holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); - } -} -``` - -贴个效果图: - - - - - - -##(3) 添加HeaderView、FooterView - -``` -mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter); - -TextView t1 = new TextView(this); -t1.setText("Header 1"); -TextView t2 = new TextView(this); -t2.setText("Header 2"); -mHeaderAndFooterWrapper.addHeaderView(t1); -mHeaderAndFooterWrapper.addHeaderView(t2); - -mRecyclerView.setAdapter(mHeaderAndFooterWrapper); -mHeaderAndFooterWrapper.notifyDataSetChanged(); -``` - -类似装饰者模式,直接将原本的adapter传入,初始化一个HeaderAndFooterWrapper对象,然后调用相关API添加。 - -##(4) 添加LoadMore - -``` -mLoadMoreWrapper = new LoadMoreWrapper(mOriginAdapter); -mLoadMoreWrapper.setLoadMoreView(R.layout.default_loading); -mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() -{ - @Override - public void onLoadMoreRequested() - { - } -}); - -mRecyclerView.setAdapter(mLoadMoreWrapper); - -``` -直接将原本的adapter传入,初始化一个LoadMoreWrapper对象,然后调用相关API即可。 - -##(5)添加EmptyView - -``` -mEmptyWrapper = new EmptyWrapper(mAdapter); -mEmptyWrapper.setEmptyView(R.layout.empty_view); - -mRecyclerView.setAdapter(mEmptyWrapper ); - -``` - -直接将原本的adapter传入,初始化一个EmptyWrapper对象,然后调用相关API即可。 - - -支持链式添加多种功能,示例代码: - -``` -mAdapter = new EmptyViewWrapper( - new LoadMoreWrapper( - new HeaderAndFooterWrapper(mOriginAdapter))); -``` - - - -## 感谢 - - -* [https://github.com/JoanZapata/base-adapter-helper](https://github.com/JoanZapata/base-adapter-helper) - - 应该是我所知道的最早的对listview,gridview封装的adapter了,参考了其中很多,比如在`ViewHolder.setXXX`类的辅助方法。 - -* [https://github.com/sockeqwe/AdapterDelegates](https://github.com/sockeqwe/AdapterDelegates) - - 参考该库多种ItemType绑定数据方式 - -* [https://github.com/ragunathjawahar/simple-section-adapter](https://github.com/ragunathjawahar/simple-section-adapter) - - `SectionAdapter`对其进行了参考。 - - +# base-adapter +Android 万能的Adapter for ListView,RecyclerView,GridView等,支持多种Item类型的情况。 + + +## 引入 + +### ForRecyclerView + +``` +compile 'com.zhy:base-rvadapter:3.0.3' +``` + +### ForListView + +``` +compile 'com.zhy:base-adapter:3.0.3' +``` + + +## 使用 + +##(1)简单的数据绑定(ListView与其使用方式一致) + +首先看我们最常用的单种Item的书写方式: + +``` +mRecyclerView.setAdapter(new CommonAdapter(this, R.layout.item_list, mDatas) +{ + @Override + public void convert(ViewHolder holder, String s) + { + holder.setText(R.id.id_item_list_title, s); + } +}); +``` +是不是相当方便,在convert方法中完成数据、事件绑定即可。 + + +只需要简单的将Adapter继承CommonAdapter,复写convert方法即可。省去了自己编写ViewHolder等大量的重复的代码。 + +* 可以通过holder.getView(id)拿到任何控件。 +* ViewHolder中封装了大量的常用的方法,比如holder.setText(id,text),holder.setOnClickListener(id,listener)等,可以支持使用。 + +效果图: + + + +##(2)多种ItemViewType(ListView与其使用方式一致) + +对于多中itemviewtype的处理参考:https://github.com/sockeqwe/AdapterDelegates ,具有极高的扩展性。 + + +``` +MultiItemTypeAdapter adapter = new MultiItemTypeAdapter(this,mDatas); +adapter.addItemViewDelegate(new MsgSendItemDelagate()); +adapter.addItemViewDelegate(new MsgComingItemDelagate()); +``` + +每种Item类型对应一个ItemViewDelegete,例如: + +``` +public class MsgComingItemDelagate implements ItemViewDelegate +{ + + @Override + public int getItemViewLayoutId() + { + return R.layout.main_chat_from_msg; + } + + @Override + public boolean isForViewType(ChatMessage item, int position) + { + return item.isComMeg(); + } + + @Override + public void convert(ViewHolder holder, ChatMessage chatMessage, int position) + { + holder.setText(R.id.chat_from_content, chatMessage.getContent()); + holder.setText(R.id.chat_from_name, chatMessage.getName()); + holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); + } +} +``` + +贴个效果图: + + + + + + +##(3) 添加HeaderView、FooterView + +``` +mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter); + +TextView t1 = new TextView(this); +t1.setText("Header 1"); +TextView t2 = new TextView(this); +t2.setText("Header 2"); +mHeaderAndFooterWrapper.addHeaderView(t1); +mHeaderAndFooterWrapper.addHeaderView(t2); + +mRecyclerView.setAdapter(mHeaderAndFooterWrapper); +mHeaderAndFooterWrapper.notifyDataSetChanged(); +``` + +类似装饰者模式,直接将原本的adapter传入,初始化一个HeaderAndFooterWrapper对象,然后调用相关API添加。 + +##(4) 添加LoadMore + +``` +mLoadMoreWrapper = new LoadMoreWrapper(mOriginAdapter); +mLoadMoreWrapper.setLoadMoreView(R.layout.default_loading); +mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() +{ + @Override + public void onLoadMoreRequested() + { + } +}); + +mRecyclerView.setAdapter(mLoadMoreWrapper); + +``` +直接将原本的adapter传入,初始化一个LoadMoreWrapper对象,然后调用相关API即可。 + +##(5)添加EmptyView + +``` +mEmptyWrapper = new EmptyWrapper(mAdapter); +mEmptyWrapper.setEmptyView(R.layout.empty_view); + +mRecyclerView.setAdapter(mEmptyWrapper ); + +``` + +直接将原本的adapter传入,初始化一个EmptyWrapper对象,然后调用相关API即可。 + + +支持链式添加多种功能,示例代码: + +``` +mAdapter = new EmptyViewWrapper( + new LoadMoreWrapper( + new HeaderAndFooterWrapper(mOriginAdapter))); +``` + + +## 一些回调 + +### onViewHolderCreated + +``` +mListView.setAdapter(new CommonAdapter(this, R.layout.item_list, mDatas) +{ + @Override + public void convert(ViewHolder holder, String o, int pos) + { + holder.setText(R.id.id_item_list_title, o); + } + + @Override + public void onViewHolderCreated(ViewHolder holder, View itemView) + { + super.onViewHolderCreated(holder, itemView); + //AutoUtil.autoSize(itemView) + } +}); +``` + + +## 感谢 + + +* [https://github.com/JoanZapata/base-adapter-helper](https://github.com/JoanZapata/base-adapter-helper) + + 应该是我所知道的最早的对listview,gridview封装的adapter了,参考了其中很多,比如在`ViewHolder.setXXX`类的辅助方法。 + +* [https://github.com/sockeqwe/AdapterDelegates](https://github.com/sockeqwe/AdapterDelegates) + + 参考该库多种ItemType绑定数据方式 + +* [https://github.com/ragunathjawahar/simple-section-adapter](https://github.com/ragunathjawahar/simple-section-adapter) + + `SectionAdapter`对其进行了参考。 + + diff --git a/base-adapter-library/.gitignore b/base-adapter-library/.gitignore index ccf2efe..1100ebe 100644 --- a/base-adapter-library/.gitignore +++ b/base-adapter-library/.gitignore @@ -1,27 +1,27 @@ -# Built application files -*.apk -*.ap_ - -# Files for the Dalvik VM -*.dex - -# Java class files -*.class - -# Generated files -bin/ -gen/ - -# Gradle files -.gradle/ -build/ -/*/build/ - -# Local configuration file (sdk path, etc) -local.properties - -# Proguard folder generated by Eclipse -proguard/ - -# Log Files -*.log +# Built application files +*.apk +*.ap_ + +# Files for the Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +build/ +/*/build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log diff --git a/base-adapter-library/base-adapter-library.iml b/base-adapter-library/base-adapter-library.iml index 487f0f7..60006e3 100644 --- a/base-adapter-library/base-adapter-library.iml +++ b/base-adapter-library/base-adapter-library.iml @@ -1,115 +1,110 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/base-adapter-library/build.gradle b/base-adapter-library/build.gradle index c5010e0..275ee00 100644 --- a/base-adapter-library/build.gradle +++ b/base-adapter-library/build.gradle @@ -1,36 +1,36 @@ -apply plugin: 'com.android.library' -apply plugin: 'com.novoda.bintray-release' - -android { - compileSdkVersion 23 - buildToolsVersion "23.0.0 rc3" - - defaultConfig { - minSdkVersion 10 - targetSdkVersion 23 - versionCode 1 - versionName "1.0" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:recyclerview-v7:23.2.0' -} - - -publish { - userOrg = 'hongyangandroid' - groupId = 'com.zhy' - artifactId = 'base-adapter' - publishVersion = '3.0.2' - desc = 'a easy adapter for listview , gridview . ' - website = 'https://github.com/hongyangAndroid/baseAdapter' - licences = ['Apache-2.0'] -} +apply plugin: 'com.android.library' +//apply plugin: 'com.novoda.bintray-release' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + minSdkVersion 10 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:recyclerview-v7:23.2.0' +} + + +//publish { +// userOrg = 'hongyangandroid' +// groupId = 'com.zhy' +// artifactId = 'base-adapter' +// publishVersion = '3.0.3' +// desc = 'a easy adapter for listview , gridview . ' +// website = 'https://github.com/hongyangAndroid/baseAdapter' +// licences = ['Apache-2.0'] +//} diff --git a/base-adapter-library/proguard-rules.pro b/base-adapter-library/proguard-rules.pro index 1c6c033..9224660 100644 --- a/base-adapter-library/proguard-rules.pro +++ b/base-adapter-library/proguard-rules.pro @@ -1,17 +1,17 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/base-adapter-library/src/androidTest/java/com/zhy/quickdev/adapter/ApplicationTest.java b/base-adapter-library/src/androidTest/java/com/zhy/quickdev/adapter/ApplicationTest.java index c361f76..5159c0d 100644 --- a/base-adapter-library/src/androidTest/java/com/zhy/quickdev/adapter/ApplicationTest.java +++ b/base-adapter-library/src/androidTest/java/com/zhy/quickdev/adapter/ApplicationTest.java @@ -1,15 +1,15 @@ -package com.zhy.quickdev.adapter; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase -{ - public ApplicationTest() - { - super(Application.class); - } +package com.zhy.quickdev.adapter; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase +{ + public ApplicationTest() + { + super(Application.class); + } } \ No newline at end of file diff --git a/base-adapter-library/src/main/AndroidManifest.xml b/base-adapter-library/src/main/AndroidManifest.xml index 3eb04bf..4030cfe 100644 --- a/base-adapter-library/src/main/AndroidManifest.xml +++ b/base-adapter-library/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ - - - - - + + + + + diff --git a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/CommonAdapter.java b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/CommonAdapter.java index 0133797..2608948 100644 --- a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/CommonAdapter.java +++ b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/CommonAdapter.java @@ -1,6 +1,7 @@ package com.zhy.adapter.abslistview; import android.content.Context; +import android.view.View; import com.zhy.adapter.abslistview.base.ItemViewDelegate; @@ -35,7 +36,6 @@ public void convert(ViewHolder holder, T t, int position) }); } - protected abstract void convert(ViewHolder viewHolder, T item, int position); } diff --git a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/MultiItemTypeAdapter.java b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/MultiItemTypeAdapter.java index be59577..1cba072 100755 --- a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/MultiItemTypeAdapter.java +++ b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/MultiItemTypeAdapter.java @@ -1,6 +1,7 @@ package com.zhy.adapter.abslistview; import android.content.Context; +import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; @@ -10,45 +11,38 @@ import java.util.List; -public class MultiItemTypeAdapter extends BaseAdapter -{ +public class MultiItemTypeAdapter extends BaseAdapter { protected Context mContext; protected List mDatas; private ItemViewDelegateManager mItemViewDelegateManager; - public MultiItemTypeAdapter(Context context, List datas) - { + public MultiItemTypeAdapter(Context context, List datas) { this.mContext = context; this.mDatas = datas; mItemViewDelegateManager = new ItemViewDelegateManager(); } - public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate itemViewDelegate) - { + public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate itemViewDelegate) { mItemViewDelegateManager.addDelegate(itemViewDelegate); return this; } - private boolean useItemViewDelegateManager() - { + private boolean useItemViewDelegateManager() { return mItemViewDelegateManager.getItemViewDelegateCount() > 0; } @Override - public int getViewTypeCount() - { + public int getViewTypeCount() { if (useItemViewDelegateManager()) return mItemViewDelegateManager.getItemViewDelegateCount(); return super.getViewTypeCount(); } @Override - public int getItemViewType(int position) - { - if (useItemViewDelegateManager()) - { + public int getItemViewType(int position) { + if (useItemViewDelegateManager()) { int viewType = mItemViewDelegateManager.getItemViewType(mDatas.get(position), position); return viewType; } @@ -56,36 +50,47 @@ public int getItemViewType(int position) } @Override - public View getView(int position, View convertView, ViewGroup parent) - { - int layoutId = mItemViewDelegateManager.getItemViewLayoutId(mDatas.get(position), position); + public View getView(int position, View convertView, ViewGroup parent) { + ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(mDatas.get(position), position); + int layoutId = itemViewDelegate.getItemViewLayoutId(); + ViewHolder viewHolder = null ; + if (convertView == null) + { + View itemView = LayoutInflater.from(mContext).inflate(layoutId, parent, + false); + viewHolder = new ViewHolder(mContext, itemView, parent, position); + viewHolder.mLayoutId = layoutId; + onViewHolderCreated(viewHolder,viewHolder.getConvertView()); + } else + { + viewHolder = (ViewHolder) convertView.getTag(); + viewHolder.mPosition = position; + } + - ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, - layoutId, position); convert(viewHolder, getItem(position), position); return viewHolder.getConvertView(); } - protected void convert(ViewHolder viewHolder, T item, int position) - { + protected void convert(ViewHolder viewHolder, T item, int position) { mItemViewDelegateManager.convert(viewHolder, item, position); } + public void onViewHolderCreated(ViewHolder holder , View itemView ) + {} + @Override - public int getCount() - { + public int getCount() { return mDatas.size(); } @Override - public T getItem(int position) - { + public T getItem(int position) { return mDatas.get(position); } @Override - public long getItemId(int position) - { + public long getItemId(int position) { return position; } diff --git a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/ViewHolder.java b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/ViewHolder.java index df751cd..d8a86b0 100755 --- a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/ViewHolder.java +++ b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/ViewHolder.java @@ -22,10 +22,10 @@ public class ViewHolder { private SparseArray mViews; - private int mPosition; + protected int mPosition; private View mConvertView; private Context mContext; - private int mLayoutId; + protected int mLayoutId; public ViewHolder(Context context, View itemView, ViewGroup parent, int position) { diff --git a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegate.java b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegate.java index 26414ef..4f5ca9c 100644 --- a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegate.java +++ b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegate.java @@ -1,19 +1,22 @@ -package com.zhy.adapter.abslistview.base; - - -import com.zhy.adapter.abslistview.ViewHolder; - -/** - * Created by zhy on 16/6/22. - */ -public interface ItemViewDelegate -{ - - public abstract int getItemViewLayoutId(); - - public abstract boolean isForViewType(T item, int position); - - public abstract void convert(ViewHolder holder, T t, int position); - - -} +package com.zhy.adapter.abslistview.base; + + +import android.view.View; + +import com.zhy.adapter.abslistview.ViewHolder; + +/** + * Created by zhy on 16/6/22. + */ +public interface ItemViewDelegate +{ + + public abstract int getItemViewLayoutId(); + + public abstract boolean isForViewType(T item, int position); + + public abstract void convert(ViewHolder holder, T t, int position); + + + +} diff --git a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegateManager.java b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegateManager.java index 61ecee0..298cccf 100644 --- a/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegateManager.java +++ b/base-adapter-library/src/main/java/com/zhy/adapter/abslistview/base/ItemViewDelegateManager.java @@ -1,128 +1,133 @@ -package com.zhy.adapter.abslistview.base; - -import android.support.v4.util.SparseArrayCompat; - -import com.zhy.adapter.abslistview.ViewHolder; - - -/** - * Created by zhy on 16/6/22. - */ -public class ItemViewDelegateManager -{ - SparseArrayCompat> delegates = new SparseArrayCompat(); - - public int getItemViewDelegateCount() - { - return delegates.size(); - } - - public ItemViewDelegateManager addDelegate(ItemViewDelegate delegate) - { - int viewType = delegates.size(); - if (delegate != null) - { - delegates.put(viewType, delegate); - viewType++; - } - return this; - } - - public ItemViewDelegateManager addDelegate(int viewType, ItemViewDelegate delegate) - { - if (delegates.get(viewType) != null) - { - throw new IllegalArgumentException( - "An ItemViewDelegate is already registered for the viewType = " - + viewType - + ". Already registered ItemViewDelegate is " - + delegates.get(viewType)); - } - delegates.put(viewType, delegate); - return this; - } - - public ItemViewDelegateManager removeDelegate(ItemViewDelegate delegate) - { - if (delegate == null) - { - throw new NullPointerException("ItemViewDelegate is null"); - } - int indexToRemove = delegates.indexOfValue(delegate); - - if (indexToRemove >= 0) - { - delegates.removeAt(indexToRemove); - } - return this; - } - - public ItemViewDelegateManager removeDelegate(int itemType) - { - int indexToRemove = delegates.indexOfKey(itemType); - - if (indexToRemove >= 0) - { - delegates.removeAt(indexToRemove); - } - return this; - } - - public int getItemViewType(T item, int position) - { - int delegatesCount = delegates.size(); - for (int i = delegatesCount - 1; i >= 0; i--) - { - ItemViewDelegate delegate = delegates.valueAt(i); - if (delegate.isForViewType(item, position)) - { - return delegates.keyAt(i); - } - } - throw new IllegalArgumentException( - "No ItemViewDelegate added that matches position=" + position + " in data source"); - } - - public void convert(ViewHolder holder, T item, int position) - { - int delegatesCount = delegates.size(); - for (int i = 0; i < delegatesCount; i++) - { - ItemViewDelegate delegate = delegates.valueAt(i); - - if (delegate.isForViewType(item, position)) - { - delegate.convert(holder, item, position); - return; - } - } - throw new IllegalArgumentException( - "No ItemViewDelegateManager added that matches position=" + position + " in data source"); - } - - - public int getItemViewLayoutId(int viewType) - { - return delegates.get(viewType).getItemViewLayoutId(); - } - - public int getItemViewType(ItemViewDelegate itemViewDelegate) - { - return delegates.indexOfValue(itemViewDelegate); - } - - public int getItemViewLayoutId(T item, int position) - { - int delegatesCount = delegates.size(); - for (int i = delegatesCount - 1; i >= 0; i--) - { - ItemViewDelegate delegate = delegates.valueAt(i); - if (delegate.isForViewType(item, position)) - { - return delegate.getItemViewLayoutId(); - } - } - throw new IllegalArgumentException( - "No ItemViewDelegate added that matches position=" + position + " in data source"); - } -} +package com.zhy.adapter.abslistview.base; + +import android.support.v4.util.SparseArrayCompat; + +import com.zhy.adapter.abslistview.ViewHolder; + + +/** + * Created by zhy on 16/6/22. + */ +public class ItemViewDelegateManager +{ + SparseArrayCompat> delegates = new SparseArrayCompat(); + + public int getItemViewDelegateCount() + { + return delegates.size(); + } + + public ItemViewDelegateManager addDelegate(ItemViewDelegate delegate) + { + int viewType = delegates.size(); + if (delegate != null) + { + delegates.put(viewType, delegate); + viewType++; + } + return this; + } + + public ItemViewDelegateManager addDelegate(int viewType, ItemViewDelegate delegate) + { + if (delegates.get(viewType) != null) + { + throw new IllegalArgumentException( + "An ItemViewDelegate is already registered for the viewType = " + + viewType + + ". Already registered ItemViewDelegate is " + + delegates.get(viewType)); + } + delegates.put(viewType, delegate); + return this; + } + + public ItemViewDelegateManager removeDelegate(ItemViewDelegate delegate) + { + if (delegate == null) + { + throw new NullPointerException("ItemViewDelegate is null"); + } + int indexToRemove = delegates.indexOfValue(delegate); + + if (indexToRemove >= 0) + { + delegates.removeAt(indexToRemove); + } + return this; + } + + public ItemViewDelegateManager removeDelegate(int itemType) + { + int indexToRemove = delegates.indexOfKey(itemType); + + if (indexToRemove >= 0) + { + delegates.removeAt(indexToRemove); + } + return this; + } + + public int getItemViewType(T item, int position) + { + int delegatesCount = delegates.size(); + for (int i = delegatesCount - 1; i >= 0; i--) + { + ItemViewDelegate delegate = delegates.valueAt(i); + if (delegate.isForViewType(item, position)) + { + return delegates.keyAt(i); + } + } + throw new IllegalArgumentException( + "No ItemViewDelegate added that matches position=" + position + " in data source"); + } + + public void convert(ViewHolder holder, T item, int position) + { + int delegatesCount = delegates.size(); + for (int i = 0; i < delegatesCount; i++) + { + ItemViewDelegate delegate = delegates.valueAt(i); + + if (delegate.isForViewType(item, position)) + { + delegate.convert(holder, item, position); + return; + } + } + throw new IllegalArgumentException( + "No ItemViewDelegateManager added that matches position=" + position + " in data source"); + } + + + public int getItemViewLayoutId(int viewType) + { + return delegates.get(viewType).getItemViewLayoutId(); + } + + public int getItemViewType(ItemViewDelegate itemViewDelegate) + { + return delegates.indexOfValue(itemViewDelegate); + } + + public ItemViewDelegate getItemViewDelegate(T item, int position) + { + int delegatesCount = delegates.size(); + for (int i = delegatesCount - 1; i >= 0; i--) + { + ItemViewDelegate delegate = delegates.valueAt(i); + if (delegate.isForViewType(item, position)) + { + return delegate; + } + } + throw new IllegalArgumentException( + "No ItemViewDelegate added that matches position=" + position + " in data source"); + } + + public int getItemViewLayoutId(T item, int position) + { + return getItemViewDelegate(item,position).getItemViewLayoutId(); + } +} diff --git a/baseadapter-recyclerview/.gitignore b/baseadapter-recyclerview/.gitignore index 796b96d..3543521 100644 --- a/baseadapter-recyclerview/.gitignore +++ b/baseadapter-recyclerview/.gitignore @@ -1 +1 @@ -/build +/build diff --git a/baseadapter-recyclerview/build.gradle b/baseadapter-recyclerview/build.gradle index 22b4217..a557009 100644 --- a/baseadapter-recyclerview/build.gradle +++ b/baseadapter-recyclerview/build.gradle @@ -1,35 +1,35 @@ -apply plugin: 'com.android.library' -//apply plugin: 'com.novoda.bintray-release' - -android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" - - defaultConfig { - minSdkVersion 10 - targetSdkVersion 23 - versionCode 1 - versionName "1.0" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:recyclerview-v7:23.4.0' -} - -//publish { -// userOrg = 'hongyangandroid' -// groupId = 'com.zhy' -// artifactId = 'base-rvadapter' -// publishVersion = '3.0.2' -// desc = 'a easy adapter for recyclerview' -// website = 'https://github.com/hongyangAndroid/baseAdapter' -// licences = ['Apache-2.0'] -//} +apply plugin: 'com.android.library' +apply plugin: 'com.novoda.bintray-release' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + minSdkVersion 10 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:recyclerview-v7:23.4.0' +} + +publish { + userOrg = 'hongyangandroid' + groupId = 'com.zhy' + artifactId = 'base-rvadapter' + publishVersion = '3.0.3' + desc = 'a easy adapter for recyclerview' + website = 'https://github.com/hongyangAndroid/baseAdapter' + licences = ['Apache-2.0'] +} diff --git a/baseadapter-recyclerview/proguard-rules.pro b/baseadapter-recyclerview/proguard-rules.pro index 1c6c033..9224660 100644 --- a/baseadapter-recyclerview/proguard-rules.pro +++ b/baseadapter-recyclerview/proguard-rules.pro @@ -1,17 +1,17 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/baseadapter-recyclerview/src/androidTest/java/com/zhy/baseadapter_recyclerview/ApplicationTest.java b/baseadapter-recyclerview/src/androidTest/java/com/zhy/baseadapter_recyclerview/ApplicationTest.java index 069a409..81b7af0 100644 --- a/baseadapter-recyclerview/src/androidTest/java/com/zhy/baseadapter_recyclerview/ApplicationTest.java +++ b/baseadapter-recyclerview/src/androidTest/java/com/zhy/baseadapter_recyclerview/ApplicationTest.java @@ -1,15 +1,15 @@ -package com.zhy.baseadapter_recyclerview; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase -{ - public ApplicationTest() - { - super(Application.class); - } +package com.zhy.baseadapter_recyclerview; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase +{ + public ApplicationTest() + { + super(Application.class); + } } \ No newline at end of file diff --git a/baseadapter-recyclerview/src/main/AndroidManifest.xml b/baseadapter-recyclerview/src/main/AndroidManifest.xml index af3ea69..be0eefb 100644 --- a/baseadapter-recyclerview/src/main/AndroidManifest.xml +++ b/baseadapter-recyclerview/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ - - - - + + + + diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/CommonAdapter.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/CommonAdapter.java index 99a8f94..f5c36f1 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/CommonAdapter.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/CommonAdapter.java @@ -1,54 +1,55 @@ -package com.zhy.adapter.recyclerview; - -import android.content.Context; -import android.view.LayoutInflater; - -import com.zhy.adapter.recyclerview.base.ItemViewDelegate; -import com.zhy.adapter.recyclerview.base.ViewHolder; - -import java.util.List; - -/** - * Created by zhy on 16/4/9. - */ -public abstract class CommonAdapter extends MultiItemTypeAdapter -{ - protected Context mContext; - protected int mLayoutId; - protected List mDatas; - protected LayoutInflater mInflater; - - public CommonAdapter(final Context context, final int layoutId, List datas) - { - super(context, datas); - mContext = context; - mInflater = LayoutInflater.from(context); - mLayoutId = layoutId; - mDatas = datas; - - addItemViewDelegate(new ItemViewDelegate() - { - @Override - public int getItemViewLayoutId() - { - return layoutId; - } - - @Override - public boolean isForViewType( T item, int position) - { - return true; - } - - @Override - public void convert(ViewHolder holder, T t, int position) - { - CommonAdapter.this.convert(holder, t, position); - } - }); - } - - protected abstract void convert(ViewHolder holder, T t, int position); - - -} +package com.zhy.adapter.recyclerview; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; + +import com.zhy.adapter.recyclerview.base.ItemViewDelegate; +import com.zhy.adapter.recyclerview.base.ViewHolder; + +import java.util.List; + +/** + * Created by zhy on 16/4/9. + */ +public abstract class CommonAdapter extends MultiItemTypeAdapter +{ + protected Context mContext; + protected int mLayoutId; + protected List mDatas; + protected LayoutInflater mInflater; + + public CommonAdapter(final Context context, final int layoutId, List datas) + { + super(context, datas); + mContext = context; + mInflater = LayoutInflater.from(context); + mLayoutId = layoutId; + mDatas = datas; + + addItemViewDelegate(new ItemViewDelegate() + { + @Override + public int getItemViewLayoutId() + { + return layoutId; + } + + @Override + public boolean isForViewType( T item, int position) + { + return true; + } + + @Override + public void convert(ViewHolder holder, T t, int position) + { + CommonAdapter.this.convert(holder, t, position); + } + }); + } + + protected abstract void convert(ViewHolder holder, T t, int position); + + +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/MultiItemTypeAdapter.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/MultiItemTypeAdapter.java index 6828065..e4318a6 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/MultiItemTypeAdapter.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/MultiItemTypeAdapter.java @@ -1,139 +1,124 @@ -package com.zhy.adapter.recyclerview; - -import android.content.Context; -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.view.ViewGroup; - -import com.zhy.adapter.recyclerview.base.ItemViewDelegate; -import com.zhy.adapter.recyclerview.base.ItemViewDelegateManager; -import com.zhy.adapter.recyclerview.base.ViewHolder; - -import java.util.List; - -/** - * Created by zhy on 16/4/9. - */ -public class MultiItemTypeAdapter extends RecyclerView.Adapter -{ - protected Context mContext; - protected List mDatas; - - protected ItemViewDelegateManager mItemViewDelegateManager; - protected OnItemClickListener mOnItemClickListener; - - - public MultiItemTypeAdapter(Context context, List datas) - { - mContext = context; - mDatas = datas; - mItemViewDelegateManager = new ItemViewDelegateManager(); - } - - @Override - public int getItemViewType(int position) - { - if (!useItemViewDelegateManager()) return super.getItemViewType(position); - return mItemViewDelegateManager.getItemViewType(mDatas.get(position), position); - } - - - @Override - public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) - { - int layoutId = mItemViewDelegateManager.getItemViewLayoutId(viewType); - ViewHolder holder = ViewHolder.createViewHolder(mContext, parent, layoutId); - setListener(parent, holder, viewType); - return holder; - } - - public void convert(ViewHolder holder, T t) - { - mItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition()); - } - - protected boolean isEnabled(int viewType) - { - return true; - } - - - protected void setListener(final ViewGroup parent, final ViewHolder viewHolder, int viewType) - { - if (!isEnabled(viewType)) return; - viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - if (mOnItemClickListener != null) - { - int position = viewHolder.getAdapterPosition(); - mOnItemClickListener.onItemClick(v, viewHolder, mDatas.get(position), position); - } - } - }); - - viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() - { - @Override - public boolean onLongClick(View v) - { - if (mOnItemClickListener != null) - { - int position = viewHolder.getAdapterPosition(); - return mOnItemClickListener.onItemLongClick(v, viewHolder, mDatas.get(position), position); - } - return false; - } - }); - } - - @Override - public void onBindViewHolder(ViewHolder holder, int position) - { - convert(holder, mDatas.get(position)); - } - - @Override - public int getItemCount() - { - int itemCount = mDatas.size(); - return itemCount; - } - - - public List getDatas() - { - return mDatas; - } - - public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate itemViewDelegate) - { - mItemViewDelegateManager.addDelegate(itemViewDelegate); - return this; - } - - public MultiItemTypeAdapter addItemViewDelegate(int viewType, ItemViewDelegate itemViewDelegate) - { - mItemViewDelegateManager.addDelegate(viewType, itemViewDelegate); - return this; - } - - protected boolean useItemViewDelegateManager() - { - return mItemViewDelegateManager.getItemViewDelegateCount() > 0; - } - - public interface OnItemClickListener - { - void onItemClick(View view, RecyclerView.ViewHolder holder, T o, int position); - - boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, T o, int position); - } - - public void setOnItemClickListener(OnItemClickListener onItemClickListener) - { - this.mOnItemClickListener = onItemClickListener; - } -} +package com.zhy.adapter.recyclerview; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.ViewGroup; + +import com.zhy.adapter.recyclerview.base.ItemViewDelegate; +import com.zhy.adapter.recyclerview.base.ItemViewDelegateManager; +import com.zhy.adapter.recyclerview.base.ViewHolder; + +import java.util.List; + +/** + * Created by zhy on 16/4/9. + */ +public class MultiItemTypeAdapter extends RecyclerView.Adapter { + protected Context mContext; + protected List mDatas; + + protected ItemViewDelegateManager mItemViewDelegateManager; + protected OnItemClickListener mOnItemClickListener; + + + public MultiItemTypeAdapter(Context context, List datas) { + mContext = context; + mDatas = datas; + mItemViewDelegateManager = new ItemViewDelegateManager(); + } + + @Override + public int getItemViewType(int position) { + if (!useItemViewDelegateManager()) return super.getItemViewType(position); + return mItemViewDelegateManager.getItemViewType(mDatas.get(position), position); + } + + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType); + int layoutId = itemViewDelegate.getItemViewLayoutId(); + ViewHolder holder = ViewHolder.createViewHolder(mContext, parent, layoutId); + onViewHolderCreated(holder,holder.getConvertView()); + setListener(parent, holder, viewType); + return holder; + } + + public void onViewHolderCreated(ViewHolder holder,View itemView){ + + } + + public void convert(ViewHolder holder, T t) { + mItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition()); + } + + protected boolean isEnabled(int viewType) { + return true; + } + + + protected void setListener(final ViewGroup parent, final ViewHolder viewHolder, int viewType) { + if (!isEnabled(viewType)) return; + viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mOnItemClickListener != null) { + int position = viewHolder.getAdapterPosition(); + mOnItemClickListener.onItemClick(v, viewHolder , position); + } + } + }); + + viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + if (mOnItemClickListener != null) { + int position = viewHolder.getAdapterPosition(); + return mOnItemClickListener.onItemLongClick(v, viewHolder, position); + } + return false; + } + }); + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + convert(holder, mDatas.get(position)); + } + + @Override + public int getItemCount() { + int itemCount = mDatas.size(); + return itemCount; + } + + + public List getDatas() { + return mDatas; + } + + public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate itemViewDelegate) { + mItemViewDelegateManager.addDelegate(itemViewDelegate); + return this; + } + + public MultiItemTypeAdapter addItemViewDelegate(int viewType, ItemViewDelegate itemViewDelegate) { + mItemViewDelegateManager.addDelegate(viewType, itemViewDelegate); + return this; + } + + protected boolean useItemViewDelegateManager() { + return mItemViewDelegateManager.getItemViewDelegateCount() > 0; + } + + public interface OnItemClickListener { + void onItemClick(View view, RecyclerView.ViewHolder holder, int position); + + boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position); + } + + public void setOnItemClickListener(OnItemClickListener onItemClickListener) { + this.mOnItemClickListener = onItemClickListener; + } +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegate.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegate.java index eefa9fb..9ba9863 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegate.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegate.java @@ -1,17 +1,18 @@ -package com.zhy.adapter.recyclerview.base; - - -/** - * Created by zhy on 16/6/22. - */ -public interface ItemViewDelegate -{ - - int getItemViewLayoutId(); - - boolean isForViewType(T item, int position); - - void convert(ViewHolder holder, T t, int position); - - -} +package com.zhy.adapter.recyclerview.base; + + +import android.view.View; + +/** + * Created by zhy on 16/6/22. + */ +public interface ItemViewDelegate +{ + + int getItemViewLayoutId(); + + boolean isForViewType(T item, int position); + + void convert(ViewHolder holder, T t, int position); + +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegateManager.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegateManager.java index c6a8472..3c8dba8 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegateManager.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/base/ItemViewDelegateManager.java @@ -1,111 +1,116 @@ -package com.zhy.adapter.recyclerview.base; - -import android.support.v4.util.SparseArrayCompat; - - -/** - * Created by zhy on 16/6/22. - */ -public class ItemViewDelegateManager -{ - SparseArrayCompat> delegates = new SparseArrayCompat(); - - public int getItemViewDelegateCount() - { - return delegates.size(); - } - - public ItemViewDelegateManager addDelegate(ItemViewDelegate delegate) - { - int viewType = delegates.size(); - if (delegate != null) - { - delegates.put(viewType, delegate); - viewType++; - } - return this; - } - - public ItemViewDelegateManager addDelegate(int viewType, ItemViewDelegate delegate) - { - if (delegates.get(viewType) != null) - { - throw new IllegalArgumentException( - "An ItemViewDelegate is already registered for the viewType = " - + viewType - + ". Already registered ItemViewDelegate is " - + delegates.get(viewType)); - } - delegates.put(viewType, delegate); - return this; - } - - public ItemViewDelegateManager removeDelegate(ItemViewDelegate delegate) - { - if (delegate == null) - { - throw new NullPointerException("ItemViewDelegate is null"); - } - int indexToRemove = delegates.indexOfValue(delegate); - - if (indexToRemove >= 0) - { - delegates.removeAt(indexToRemove); - } - return this; - } - - public ItemViewDelegateManager removeDelegate(int itemType) - { - int indexToRemove = delegates.indexOfKey(itemType); - - if (indexToRemove >= 0) - { - delegates.removeAt(indexToRemove); - } - return this; - } - - public int getItemViewType(T item, int position) - { - int delegatesCount = delegates.size(); - for (int i = delegatesCount - 1; i >= 0; i--) - { - ItemViewDelegate delegate = delegates.valueAt(i); - if (delegate.isForViewType( item, position)) - { - return delegates.keyAt(i); - } - } - throw new IllegalArgumentException( - "No ItemViewDelegate added that matches position=" + position + " in data source"); - } - - public void convert(ViewHolder holder, T item, int position) - { - int delegatesCount = delegates.size(); - for (int i = 0; i < delegatesCount; i++) - { - ItemViewDelegate delegate = delegates.valueAt(i); - - if (delegate.isForViewType( item, position)) - { - delegate.convert(holder, item, position); - return; - } - } - throw new IllegalArgumentException( - "No ItemViewDelegateManager added that matches position=" + position + " in data source"); - } - - - public int getItemViewLayoutId(int viewType) - { - return delegates.get(viewType).getItemViewLayoutId(); - } - - public int getItemViewType(ItemViewDelegate itemViewDelegate) - { - return delegates.indexOfValue(itemViewDelegate); - } -} +package com.zhy.adapter.recyclerview.base; + +import android.support.v4.util.SparseArrayCompat; + + +/** + * Created by zhy on 16/6/22. + */ +public class ItemViewDelegateManager +{ + SparseArrayCompat> delegates = new SparseArrayCompat(); + + public int getItemViewDelegateCount() + { + return delegates.size(); + } + + public ItemViewDelegateManager addDelegate(ItemViewDelegate delegate) + { + int viewType = delegates.size(); + if (delegate != null) + { + delegates.put(viewType, delegate); + viewType++; + } + return this; + } + + public ItemViewDelegateManager addDelegate(int viewType, ItemViewDelegate delegate) + { + if (delegates.get(viewType) != null) + { + throw new IllegalArgumentException( + "An ItemViewDelegate is already registered for the viewType = " + + viewType + + ". Already registered ItemViewDelegate is " + + delegates.get(viewType)); + } + delegates.put(viewType, delegate); + return this; + } + + public ItemViewDelegateManager removeDelegate(ItemViewDelegate delegate) + { + if (delegate == null) + { + throw new NullPointerException("ItemViewDelegate is null"); + } + int indexToRemove = delegates.indexOfValue(delegate); + + if (indexToRemove >= 0) + { + delegates.removeAt(indexToRemove); + } + return this; + } + + public ItemViewDelegateManager removeDelegate(int itemType) + { + int indexToRemove = delegates.indexOfKey(itemType); + + if (indexToRemove >= 0) + { + delegates.removeAt(indexToRemove); + } + return this; + } + + public int getItemViewType(T item, int position) + { + int delegatesCount = delegates.size(); + for (int i = delegatesCount - 1; i >= 0; i--) + { + ItemViewDelegate delegate = delegates.valueAt(i); + if (delegate.isForViewType( item, position)) + { + return delegates.keyAt(i); + } + } + throw new IllegalArgumentException( + "No ItemViewDelegate added that matches position=" + position + " in data source"); + } + + public void convert(ViewHolder holder, T item, int position) + { + int delegatesCount = delegates.size(); + for (int i = 0; i < delegatesCount; i++) + { + ItemViewDelegate delegate = delegates.valueAt(i); + + if (delegate.isForViewType( item, position)) + { + delegate.convert(holder, item, position); + return; + } + } + throw new IllegalArgumentException( + "No ItemViewDelegateManager added that matches position=" + position + " in data source"); + } + + + public ItemViewDelegate getItemViewDelegate(int viewType) + { + return delegates.get(viewType); + } + + public int getItemViewLayoutId(int viewType) + { + return getItemViewDelegate(viewType).getItemViewLayoutId(); + } + + public int getItemViewType(ItemViewDelegate itemViewDelegate) + { + return delegates.indexOfValue(itemViewDelegate); + } +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/utils/WrapperUtils.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/utils/WrapperUtils.java index 5839a57..d0a2d7d 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/utils/WrapperUtils.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/utils/WrapperUtils.java @@ -1,53 +1,53 @@ -package com.zhy.adapter.recyclerview.utils; - -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.StaggeredGridLayoutManager; -import android.view.ViewGroup; - -/** - * Created by zhy on 16/6/28. - */ -public class WrapperUtils -{ - public interface SpanSizeCallback - { - int getSpanSize(GridLayoutManager layoutManager , GridLayoutManager.SpanSizeLookup oldLookup, int position); - } - - public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) - { - innerAdapter.onAttachedToRecyclerView(recyclerView); - - RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); - if (layoutManager instanceof GridLayoutManager) - { - final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; - final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup(); - - gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() - { - @Override - public int getSpanSize(int position) - { - return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position); - } - }); - gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount()); - } - } - - public static void setFullSpan(RecyclerView.ViewHolder holder) - { - ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); - - if (lp != null - && lp instanceof StaggeredGridLayoutManager.LayoutParams) - { - - StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; - - p.setFullSpan(true); - } - } -} +package com.zhy.adapter.recyclerview.utils; + +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.StaggeredGridLayoutManager; +import android.view.ViewGroup; + +/** + * Created by zhy on 16/6/28. + */ +public class WrapperUtils +{ + public interface SpanSizeCallback + { + int getSpanSize(GridLayoutManager layoutManager , GridLayoutManager.SpanSizeLookup oldLookup, int position); + } + + public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) + { + innerAdapter.onAttachedToRecyclerView(recyclerView); + + RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); + if (layoutManager instanceof GridLayoutManager) + { + final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; + final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup(); + + gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() + { + @Override + public int getSpanSize(int position) + { + return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position); + } + }); + gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount()); + } + } + + public static void setFullSpan(RecyclerView.ViewHolder holder) + { + ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); + + if (lp != null + && lp instanceof StaggeredGridLayoutManager.LayoutParams) + { + + StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; + + p.setFullSpan(true); + } + } +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/EmptyWrapper.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/EmptyWrapper.java index 3720d97..2eb35b5 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/EmptyWrapper.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/EmptyWrapper.java @@ -1,124 +1,125 @@ -package com.zhy.adapter.recyclerview.wrapper; - -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.view.ViewGroup; - -import com.zhy.adapter.recyclerview.base.ViewHolder; -import com.zhy.adapter.recyclerview.utils.WrapperUtils; - - -/** - * Created by zhy on 16/6/23. - */ -public class EmptyWrapper extends RecyclerView.Adapter -{ - public static final int ITEM_TYPE_EMPTY = Integer.MAX_VALUE - 1; - - private RecyclerView.Adapter mInnerAdapter; - private View mEmptyView; - private int mEmptyLayoutId; - - - public EmptyWrapper(RecyclerView.Adapter adapter) - { - mInnerAdapter = adapter; - } - - private boolean isEmpty() - { - return (mEmptyView != null || mEmptyLayoutId != 0) && mInnerAdapter.getItemCount() == 0; - } - - - @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) - { - if (isEmpty()) - { - ViewHolder holder; - if (mEmptyView != null) - { - holder = ViewHolder.createViewHolder(parent.getContext(), mEmptyView); - } else - { - holder = ViewHolder.createViewHolder(parent.getContext(), parent, mEmptyLayoutId); - } - return holder; - } - return mInnerAdapter.onCreateViewHolder(parent, viewType); - } - - @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) - { - WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils.SpanSizeCallback() - { - @Override - public int getSpanSize(GridLayoutManager gridLayoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) - { - if (isEmpty()) - { - return gridLayoutManager.getSpanCount(); - } - if (oldLookup != null) - { - return oldLookup.getSpanSize(position); - } - return 1; - } - }); - - - } - - @Override - public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) - { - mInnerAdapter.onViewAttachedToWindow(holder); - if (isEmpty()) - { - WrapperUtils.setFullSpan(holder); - } - } - - - @Override - public int getItemViewType(int position) - { - if (isEmpty()) - { - return ITEM_TYPE_EMPTY; - } - return mInnerAdapter.getItemViewType(position); - } - - @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) - { - if (isEmpty()) - { - return; - } - mInnerAdapter.onBindViewHolder(holder, position); - } - - @Override - public int getItemCount() - { - if (isEmpty()) return 1; - return mInnerAdapter.getItemCount(); - } - - public void setEmptyView(View emptyView) - { - mEmptyView = emptyView; - } - - public void setEmptyView(int layoutId) - { - mEmptyLayoutId = layoutId; - } - -} +package com.zhy.adapter.recyclerview.wrapper; + +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.ViewGroup; + +import com.zhy.adapter.recyclerview.base.ViewHolder; +import com.zhy.adapter.recyclerview.utils.WrapperUtils; + + +/** + * Created by zhy on 16/6/23. + */ +public class EmptyWrapper extends RecyclerView.Adapter +{ + public static final int ITEM_TYPE_EMPTY = Integer.MAX_VALUE - 1; + + private RecyclerView.Adapter mInnerAdapter; + private View mEmptyView; + private int mEmptyLayoutId; + + + public EmptyWrapper(RecyclerView.Adapter adapter) + { + mInnerAdapter = adapter; + } + + private boolean isEmpty() + { + return (mEmptyView != null || mEmptyLayoutId != 0) && mInnerAdapter.getItemCount() == 0; + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) + { + if (isEmpty()) + { + ViewHolder holder; + if (mEmptyView != null) + { + holder = ViewHolder.createViewHolder(parent.getContext(), mEmptyView); + } else + { + holder = ViewHolder.createViewHolder(parent.getContext(), parent, mEmptyLayoutId); + } + return holder; + } + return mInnerAdapter.onCreateViewHolder(parent, viewType); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) + { + WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils.SpanSizeCallback() + { + @Override + public int getSpanSize(GridLayoutManager gridLayoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) + { + if (isEmpty()) + { + return gridLayoutManager.getSpanCount(); + } + if (oldLookup != null) + { + return oldLookup.getSpanSize(position); + } + return 1; + } + }); + + + } + + @Override + public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) + { + mInnerAdapter.onViewAttachedToWindow(holder); + if (isEmpty()) + { + WrapperUtils.setFullSpan(holder); + } + } + + + @Override + public int getItemViewType(int position) + { + if (isEmpty()) + { + return ITEM_TYPE_EMPTY; + } + return mInnerAdapter.getItemViewType(position); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) + { + if (isEmpty()) + { + return; + } + mInnerAdapter.onBindViewHolder(holder, position); + } + + @Override + public int getItemCount() + { + if (isEmpty()) return 1; + return mInnerAdapter.getItemCount(); + } + + + + public void setEmptyView(View emptyView) + { + mEmptyView = emptyView; + } + + public void setEmptyView(int layoutId) + { + mEmptyLayoutId = layoutId; + } + +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/HeaderAndFooterWrapper.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/HeaderAndFooterWrapper.java index 463b69d..fa5fe7d 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/HeaderAndFooterWrapper.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/HeaderAndFooterWrapper.java @@ -1,152 +1,152 @@ -package com.zhy.adapter.recyclerview.wrapper; - -import android.support.v4.util.SparseArrayCompat; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.view.ViewGroup; - -import com.zhy.adapter.recyclerview.base.ViewHolder; -import com.zhy.adapter.recyclerview.utils.WrapperUtils; - - -/** - * Created by zhy on 16/6/23. - */ -public class HeaderAndFooterWrapper extends RecyclerView.Adapter -{ - private static final int BASE_ITEM_TYPE_HEADER = 100000; - private static final int BASE_ITEM_TYPE_FOOTER = 200000; - - private SparseArrayCompat mHeaderViews = new SparseArrayCompat<>(); - private SparseArrayCompat mFootViews = new SparseArrayCompat<>(); - - private RecyclerView.Adapter mInnerAdapter; - - public HeaderAndFooterWrapper(RecyclerView.Adapter adapter) - { - mInnerAdapter = adapter; - } - - @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) - { - if (mHeaderViews.get(viewType) != null) - { - ViewHolder holder = ViewHolder.createViewHolder(parent.getContext(), mHeaderViews.get(viewType)); - return holder; - - } else if (mFootViews.get(viewType) != null) - { - ViewHolder holder = ViewHolder.createViewHolder(parent.getContext(), mFootViews.get(viewType)); - return holder; - } - return mInnerAdapter.onCreateViewHolder(parent, viewType); - } - - @Override - public int getItemViewType(int position) - { - if (isHeaderViewPos(position)) - { - return mHeaderViews.keyAt(position); - } else if (isFooterViewPos(position)) - { - return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount()); - } - return mInnerAdapter.getItemViewType(position - getHeadersCount()); - } - - private int getRealItemCount() - { - return mInnerAdapter.getItemCount(); - } - - - @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) - { - if (isHeaderViewPos(position)) - { - return; - } - if (isFooterViewPos(position)) - { - return; - } - mInnerAdapter.onBindViewHolder(holder, position - getHeadersCount()); - } - - @Override - public int getItemCount() - { - return getHeadersCount() + getFootersCount() + getRealItemCount(); - } - - @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) - { - WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils.SpanSizeCallback() - { - @Override - public int getSpanSize(GridLayoutManager layoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) - { - int viewType = getItemViewType(position); - if (mHeaderViews.get(viewType) != null) - { - return layoutManager.getSpanCount(); - } else if (mFootViews.get(viewType) != null) - { - return layoutManager.getSpanCount(); - } - if (oldLookup != null) - return oldLookup.getSpanSize(position); - return 1; - } - }); - } - - @Override - public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) - { - mInnerAdapter.onViewAttachedToWindow(holder); - int position = holder.getLayoutPosition(); - if (isHeaderViewPos(position) || isFooterViewPos(position)) - { - WrapperUtils.setFullSpan(holder); - } - } - - private boolean isHeaderViewPos(int position) - { - return position < getHeadersCount(); - } - - private boolean isFooterViewPos(int position) - { - return position >= getHeadersCount() + getRealItemCount(); - } - - - public void addHeaderView(View view) - { - mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view); - } - - public void addFootView(View view) - { - mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view); - } - - public int getHeadersCount() - { - return mHeaderViews.size(); - } - - public int getFootersCount() - { - return mFootViews.size(); - } - - -} +package com.zhy.adapter.recyclerview.wrapper; + +import android.support.v4.util.SparseArrayCompat; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.ViewGroup; + +import com.zhy.adapter.recyclerview.base.ViewHolder; +import com.zhy.adapter.recyclerview.utils.WrapperUtils; + + +/** + * Created by zhy on 16/6/23. + */ +public class HeaderAndFooterWrapper extends RecyclerView.Adapter +{ + private static final int BASE_ITEM_TYPE_HEADER = 100000; + private static final int BASE_ITEM_TYPE_FOOTER = 200000; + + private SparseArrayCompat mHeaderViews = new SparseArrayCompat<>(); + private SparseArrayCompat mFootViews = new SparseArrayCompat<>(); + + private RecyclerView.Adapter mInnerAdapter; + + public HeaderAndFooterWrapper(RecyclerView.Adapter adapter) + { + mInnerAdapter = adapter; + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) + { + if (mHeaderViews.get(viewType) != null) + { + ViewHolder holder = ViewHolder.createViewHolder(parent.getContext(), mHeaderViews.get(viewType)); + return holder; + + } else if (mFootViews.get(viewType) != null) + { + ViewHolder holder = ViewHolder.createViewHolder(parent.getContext(), mFootViews.get(viewType)); + return holder; + } + return mInnerAdapter.onCreateViewHolder(parent, viewType); + } + + @Override + public int getItemViewType(int position) + { + if (isHeaderViewPos(position)) + { + return mHeaderViews.keyAt(position); + } else if (isFooterViewPos(position)) + { + return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount()); + } + return mInnerAdapter.getItemViewType(position - getHeadersCount()); + } + + private int getRealItemCount() + { + return mInnerAdapter.getItemCount(); + } + + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) + { + if (isHeaderViewPos(position)) + { + return; + } + if (isFooterViewPos(position)) + { + return; + } + mInnerAdapter.onBindViewHolder(holder, position - getHeadersCount()); + } + + @Override + public int getItemCount() + { + return getHeadersCount() + getFootersCount() + getRealItemCount(); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) + { + WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils.SpanSizeCallback() + { + @Override + public int getSpanSize(GridLayoutManager layoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) + { + int viewType = getItemViewType(position); + if (mHeaderViews.get(viewType) != null) + { + return layoutManager.getSpanCount(); + } else if (mFootViews.get(viewType) != null) + { + return layoutManager.getSpanCount(); + } + if (oldLookup != null) + return oldLookup.getSpanSize(position); + return 1; + } + }); + } + + @Override + public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) + { + mInnerAdapter.onViewAttachedToWindow(holder); + int position = holder.getLayoutPosition(); + if (isHeaderViewPos(position) || isFooterViewPos(position)) + { + WrapperUtils.setFullSpan(holder); + } + } + + private boolean isHeaderViewPos(int position) + { + return position < getHeadersCount(); + } + + private boolean isFooterViewPos(int position) + { + return position >= getHeadersCount() + getRealItemCount(); + } + + + public void addHeaderView(View view) + { + mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view); + } + + public void addFootView(View view) + { + mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view); + } + + public int getHeadersCount() + { + return mHeaderViews.size(); + } + + public int getFootersCount() + { + return mFootViews.size(); + } + + +} diff --git a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/LoadmoreWrapper.java b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/LoadmoreWrapper.java index b5ae913..894b8d7 100644 --- a/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/LoadmoreWrapper.java +++ b/baseadapter-recyclerview/src/main/java/com/zhy/adapter/recyclerview/wrapper/LoadmoreWrapper.java @@ -1,163 +1,162 @@ -package com.zhy.adapter.recyclerview.wrapper; - -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.StaggeredGridLayoutManager; -import android.util.Log; -import android.view.View; -import android.view.ViewGroup; - -import com.zhy.adapter.recyclerview.base.ViewHolder; -import com.zhy.adapter.recyclerview.utils.WrapperUtils; - - -/** - * Created by zhy on 16/6/23. - */ -public class LoadMoreWrapper extends RecyclerView.Adapter -{ - public static final int ITEM_TYPE_LOAD_MORE = Integer.MAX_VALUE - 2; - - private RecyclerView.Adapter mInnerAdapter; - private View mLoadMoreView; - private int mLoadMoreLayoutId; - - public LoadMoreWrapper(RecyclerView.Adapter adapter) - { - mInnerAdapter = adapter; - } - - private boolean hasLoadMore() - { - return mLoadMoreView != null || mLoadMoreLayoutId != 0; - } - - - private boolean isShowLoadMore(int position) - { - return hasLoadMore() && (position >= mInnerAdapter.getItemCount()); - } - - @Override - public int getItemViewType(int position) - { - if (isShowLoadMore(position)) - { - return ITEM_TYPE_LOAD_MORE; - } - return mInnerAdapter.getItemViewType(position); - } - - @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) - { - if (viewType == ITEM_TYPE_LOAD_MORE) - { - ViewHolder holder; - if (mLoadMoreView != null) - { - holder = ViewHolder.createViewHolder(parent.getContext(), mLoadMoreView); - } else - { - holder = ViewHolder.createViewHolder(parent.getContext(), parent, mLoadMoreLayoutId); - } - return holder; - } - return mInnerAdapter.onCreateViewHolder(parent, viewType); - } - - @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) - { - if (isShowLoadMore(position)) - { - if (mOnLoadMoreListener != null) - { - mOnLoadMoreListener.onLoadMoreRequested(); - } - return; - } - mInnerAdapter.onBindViewHolder(holder, position); - } - - @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) - { - WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils.SpanSizeCallback() - { - @Override - public int getSpanSize(GridLayoutManager layoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) - { - if (isShowLoadMore(position)) - { - return layoutManager.getSpanCount(); - } - if (oldLookup != null) - { - return oldLookup.getSpanSize(position); - } - return 1; - } - }); - } - - - @Override - public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) - { - mInnerAdapter.onViewAttachedToWindow(holder); - - if (isShowLoadMore(holder.getLayoutPosition())) - { - setFullSpan(holder); - } - } - - private void setFullSpan(RecyclerView.ViewHolder holder) - { - ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); - - if (lp != null - && lp instanceof StaggeredGridLayoutManager.LayoutParams) - { - StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; - - p.setFullSpan(true); - } - } - - @Override - public int getItemCount() - { - return mInnerAdapter.getItemCount() + (hasLoadMore() ? 1 : 0); - } - - - public interface OnLoadMoreListener - { - void onLoadMoreRequested(); - } - - private OnLoadMoreListener mOnLoadMoreListener; - - public LoadMoreWrapper setOnLoadMoreListener(OnLoadMoreListener loadMoreListener) - { - if (loadMoreListener != null) - { - mOnLoadMoreListener = loadMoreListener; - } - return this; - } - - public LoadMoreWrapper setLoadMoreView(View loadMoreView) - { - mLoadMoreView = loadMoreView; - return this; - } - - public LoadMoreWrapper setLoadMoreView(int layoutId) - { - mLoadMoreLayoutId = layoutId; - return this; - } -} +package com.zhy.adapter.recyclerview.wrapper; + +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.StaggeredGridLayoutManager; +import android.view.View; +import android.view.ViewGroup; + +import com.zhy.adapter.recyclerview.base.ViewHolder; +import com.zhy.adapter.recyclerview.utils.WrapperUtils; + + +/** + * Created by zhy on 16/6/23. + */ +public class LoadMoreWrapper extends RecyclerView.Adapter +{ + public static final int ITEM_TYPE_LOAD_MORE = Integer.MAX_VALUE - 2; + + private RecyclerView.Adapter mInnerAdapter; + private View mLoadMoreView; + private int mLoadMoreLayoutId; + + public LoadMoreWrapper(RecyclerView.Adapter adapter) + { + mInnerAdapter = adapter; + } + + private boolean hasLoadMore() + { + return mLoadMoreView != null || mLoadMoreLayoutId != 0; + } + + + private boolean isShowLoadMore(int position) + { + return hasLoadMore() && (position >= mInnerAdapter.getItemCount()); + } + + @Override + public int getItemViewType(int position) + { + if (isShowLoadMore(position)) + { + return ITEM_TYPE_LOAD_MORE; + } + return mInnerAdapter.getItemViewType(position); + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) + { + if (viewType == ITEM_TYPE_LOAD_MORE) + { + ViewHolder holder; + if (mLoadMoreView != null) + { + holder = ViewHolder.createViewHolder(parent.getContext(), mLoadMoreView); + } else + { + holder = ViewHolder.createViewHolder(parent.getContext(), parent, mLoadMoreLayoutId); + } + return holder; + } + return mInnerAdapter.onCreateViewHolder(parent, viewType); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) + { + if (isShowLoadMore(position)) + { + if (mOnLoadMoreListener != null) + { + mOnLoadMoreListener.onLoadMoreRequested(); + } + return; + } + mInnerAdapter.onBindViewHolder(holder, position); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) + { + WrapperUtils.onAttachedToRecyclerView(mInnerAdapter, recyclerView, new WrapperUtils.SpanSizeCallback() + { + @Override + public int getSpanSize(GridLayoutManager layoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) + { + if (isShowLoadMore(position)) + { + return layoutManager.getSpanCount(); + } + if (oldLookup != null) + { + return oldLookup.getSpanSize(position); + } + return 1; + } + }); + } + + + @Override + public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) + { + mInnerAdapter.onViewAttachedToWindow(holder); + + if (isShowLoadMore(holder.getLayoutPosition())) + { + setFullSpan(holder); + } + } + + private void setFullSpan(RecyclerView.ViewHolder holder) + { + ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); + + if (lp != null + && lp instanceof StaggeredGridLayoutManager.LayoutParams) + { + StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; + + p.setFullSpan(true); + } + } + + @Override + public int getItemCount() + { + return mInnerAdapter.getItemCount() + (hasLoadMore() ? 1 : 0); + } + + + public interface OnLoadMoreListener + { + void onLoadMoreRequested(); + } + + private OnLoadMoreListener mOnLoadMoreListener; + + public LoadMoreWrapper setOnLoadMoreListener(OnLoadMoreListener loadMoreListener) + { + if (loadMoreListener != null) + { + mOnLoadMoreListener = loadMoreListener; + } + return this; + } + + public LoadMoreWrapper setLoadMoreView(View loadMoreView) + { + mLoadMoreView = loadMoreView; + return this; + } + + public LoadMoreWrapper setLoadMoreView(int layoutId) + { + mLoadMoreLayoutId = layoutId; + return this; + } +} diff --git a/baseadapter-recyclerview/src/test/java/com/zhy/baseadapter_recyclerview/ExampleUnitTest.java b/baseadapter-recyclerview/src/test/java/com/zhy/baseadapter_recyclerview/ExampleUnitTest.java index c777b24..363a793 100644 --- a/baseadapter-recyclerview/src/test/java/com/zhy/baseadapter_recyclerview/ExampleUnitTest.java +++ b/baseadapter-recyclerview/src/test/java/com/zhy/baseadapter_recyclerview/ExampleUnitTest.java @@ -1,17 +1,17 @@ -package com.zhy.baseadapter_recyclerview; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest -{ - @Test - public void addition_isCorrect() throws Exception - { - assertEquals(4, 2 + 2); - } +package com.zhy.baseadapter_recyclerview; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * To work on unit tests, switch the Test Artifact in the Build Variants view. + */ +public class ExampleUnitTest +{ + @Test + public void addition_isCorrect() throws Exception + { + assertEquals(4, 2 + 2); + } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index cb720a1..bf69489 100644 --- a/build.gradle +++ b/build.gradle @@ -1,31 +1,31 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. - -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' - classpath 'com.novoda:bintray-release:0.3.4' - - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } -} - -allprojects { - repositories { - jcenter() - } -} - -//ext { -// userOrg = 'hongyangandroid' -// groupId = 'com.zhy' -// artifactId = 'base-adapter' -// publishVersion = '2.0.0' -// desc = 'a easy adapter for listview ,recyclerview, gridview . ' -// website = 'https://github.com/hongyangAndroid/baseAdapter' -// licences = ['Apache-2.0'] -//} - +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.novoda:bintray-release:0.3.4' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + jcenter() + } +} + +//ext { +// userOrg = 'hongyangandroid' +// groupId = 'com.zhy' +// artifactId = 'base-adapter' +// publishVersion = '2.0.0' +// desc = 'a easy adapter for listview ,recyclerview, gridview . ' +// website = 'https://github.com/hongyangAndroid/baseAdapter' +// licences = ['Apache-2.0'] +//} + diff --git a/gradle.properties b/gradle.properties index 1d3591c..9b6ae2a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,18 +1,18 @@ -# Project-wide Gradle settings. - -# IDE (e.g. Android Studio) users: -# Gradle settings configured through the IDE *will override* -# any settings specified in this file. - -# For more details on how to configure your build environment visit -# http://www.gradle.org/docs/current/userguide/build_environment.html - -# Specifies the JVM arguments used for the daemon process. -# The setting is particularly useful for tweaking memory settings. -# Default value: -Xmx10248m -XX:MaxPermSize=256m -# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 - -# When configured, Gradle will run in incubating parallel mode. -# This option should only be used with decoupled projects. More details, visit -# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index edce31f..059382a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Jun 28 12:55:08 GMT+08:00 2016 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +#Tue Jun 28 12:55:08 GMT+08:00 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip diff --git a/proguard-rules.pro b/proguard-rules.pro index 1c6c033..9224660 100644 --- a/proguard-rules.pro +++ b/proguard-rules.pro @@ -1,17 +1,17 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/sample/.gitignore b/sample/.gitignore index 796b96d..3543521 100644 --- a/sample/.gitignore +++ b/sample/.gitignore @@ -1 +1 @@ -/build +/build diff --git a/sample/build.gradle b/sample/build.gradle index de4f876..fef61a8 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,30 +1,30 @@ -apply plugin: 'com.android.application' - -android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" - - defaultConfig { - applicationId "com.zhy.sample" - minSdkVersion 10 - targetSdkVersion 23 - versionCode 1 - versionName "1.0" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.4.0' - compile 'com.android.support:recyclerview-v7:23.4.0' - compile 'com.zhy:base-adapter:3.0.2' - compile 'com.zhy:base-rvadapter:3.0.2' -// compile project(':base-adapter-library') -// compile project(':baseadapter-recyclerview') -} +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + applicationId "com.zhy.sample" + minSdkVersion 10 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:appcompat-v7:23.4.0' + compile 'com.android.support:recyclerview-v7:23.4.0' +// compile 'com.zhy:base-adapter:3.0.2' +// compile 'com.zhy:base-rvadapter:3.0.2' + compile project(':base-adapter-library') + compile project(':baseadapter-recyclerview') +} diff --git a/sample/proguard-rules.pro b/sample/proguard-rules.pro index 1c6c033..9224660 100644 --- a/sample/proguard-rules.pro +++ b/sample/proguard-rules.pro @@ -1,17 +1,17 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/zhy/android/sdk/android-sdk-macosx/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/sample/sample.iml b/sample/sample.iml index 95b9dd1..3e7e715 100644 --- a/sample/sample.iml +++ b/sample/sample.iml @@ -1,121 +1,100 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/src/androidTest/java/com/zhy/sample/ApplicationTest.java b/sample/src/androidTest/java/com/zhy/sample/ApplicationTest.java index 04c18e4..48d5de4 100644 --- a/sample/src/androidTest/java/com/zhy/sample/ApplicationTest.java +++ b/sample/src/androidTest/java/com/zhy/sample/ApplicationTest.java @@ -1,15 +1,15 @@ -package com.zhy.sample; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase -{ - public ApplicationTest() - { - super(Application.class); - } +package com.zhy.sample; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase +{ + public ApplicationTest() + { + super(Application.class); + } } \ No newline at end of file diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 7b9a288..dfc3751 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -1,33 +1,33 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + diff --git a/sample/src/main/java/com/zhy/sample/DividerItemDecoration.java b/sample/src/main/java/com/zhy/sample/DividerItemDecoration.java index 5f7656a..82d5e58 100644 --- a/sample/src/main/java/com/zhy/sample/DividerItemDecoration.java +++ b/sample/src/main/java/com/zhy/sample/DividerItemDecoration.java @@ -1,104 +1,104 @@ -package com.zhy.sample;/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * limitations under the License. - */ - -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.View; - - -/** - * This class is from the v7 samples of the Android SDK. It's not by me! - *

- * See the license above for details. - */ -public class DividerItemDecoration extends RecyclerView.ItemDecoration { - - private static final int[] ATTRS = new int[]{ - android.R.attr.listDivider - }; - - public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; - - public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; - - private Drawable mDivider; - - private int mOrientation; - - public DividerItemDecoration(Context context, int orientation) { - final TypedArray a = context.obtainStyledAttributes(ATTRS); - mDivider = a.getDrawable(0); - a.recycle(); - setOrientation(orientation); - } - - public void setOrientation(int orientation) { - if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { - throw new IllegalArgumentException("invalid orientation"); - } - mOrientation = orientation; - } - - @Override - public void onDraw(Canvas c, RecyclerView parent) { - - if (mOrientation == VERTICAL_LIST) { - drawVertical(c, parent); - } else { - drawHorizontal(c, parent); - } - - } - - - public void drawVertical(Canvas c, RecyclerView parent) { - final int left = parent.getPaddingLeft(); - final int right = parent.getWidth() - parent.getPaddingRight(); - - final int childCount = parent.getChildCount(); - for (int i = 0; i < childCount; i++) { - final View child = parent.getChildAt(i); - RecyclerView v = new RecyclerView(parent.getContext()); - final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child - .getLayoutParams(); - final int top = child.getBottom() + params.bottomMargin; - final int bottom = top + mDivider.getIntrinsicHeight(); - mDivider.setBounds(left, top, right, bottom); - mDivider.draw(c); - } - } - - public void drawHorizontal(Canvas c, RecyclerView parent) { - final int top = parent.getPaddingTop(); - final int bottom = parent.getHeight() - parent.getPaddingBottom(); - - final int childCount = parent.getChildCount(); - for (int i = 0; i < childCount; i++) { - final View child = parent.getChildAt(i); - final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child - .getLayoutParams(); - final int left = child.getRight() + params.rightMargin; - final int right = left + mDivider.getIntrinsicHeight(); - mDivider.setBounds(left, top, right, bottom); - mDivider.draw(c); - } - } - - @Override - public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { - if (mOrientation == VERTICAL_LIST) { - outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); - } else { - outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); - } - } +package com.zhy.sample;/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * limitations under the License. + */ + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; + + +/** + * This class is from the v7 samples of the Android SDK. It's not by me! + *

+ * See the license above for details. + */ +public class DividerItemDecoration extends RecyclerView.ItemDecoration { + + private static final int[] ATTRS = new int[]{ + android.R.attr.listDivider + }; + + public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; + + public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; + + private Drawable mDivider; + + private int mOrientation; + + public DividerItemDecoration(Context context, int orientation) { + final TypedArray a = context.obtainStyledAttributes(ATTRS); + mDivider = a.getDrawable(0); + a.recycle(); + setOrientation(orientation); + } + + public void setOrientation(int orientation) { + if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { + throw new IllegalArgumentException("invalid orientation"); + } + mOrientation = orientation; + } + + @Override + public void onDraw(Canvas c, RecyclerView parent) { + + if (mOrientation == VERTICAL_LIST) { + drawVertical(c, parent); + } else { + drawHorizontal(c, parent); + } + + } + + + public void drawVertical(Canvas c, RecyclerView parent) { + final int left = parent.getPaddingLeft(); + final int right = parent.getWidth() - parent.getPaddingRight(); + + final int childCount = parent.getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = parent.getChildAt(i); + RecyclerView v = new RecyclerView(parent.getContext()); + final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child + .getLayoutParams(); + final int top = child.getBottom() + params.bottomMargin; + final int bottom = top + mDivider.getIntrinsicHeight(); + mDivider.setBounds(left, top, right, bottom); + mDivider.draw(c); + } + } + + public void drawHorizontal(Canvas c, RecyclerView parent) { + final int top = parent.getPaddingTop(); + final int bottom = parent.getHeight() - parent.getPaddingBottom(); + + final int childCount = parent.getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = parent.getChildAt(i); + final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child + .getLayoutParams(); + final int left = child.getRight() + params.rightMargin; + final int right = left + mDivider.getIntrinsicHeight(); + mDivider.setBounds(left, top, right, bottom); + mDivider.draw(c); + } + } + + @Override + public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { + if (mOrientation == VERTICAL_LIST) { + outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); + } else { + outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); + } + } } \ No newline at end of file diff --git a/sample/src/main/java/com/zhy/sample/MainActivity.java b/sample/src/main/java/com/zhy/sample/MainActivity.java index d36cfca..57bdd06 100644 --- a/sample/src/main/java/com/zhy/sample/MainActivity.java +++ b/sample/src/main/java/com/zhy/sample/MainActivity.java @@ -1,78 +1,84 @@ -package com.zhy.sample; - -import android.content.Intent; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.view.View; -import android.widget.AdapterView; -import android.widget.ListView; - -import com.zhy.adapter.abslistview.CommonAdapter; -import com.zhy.adapter.abslistview.ViewHolder; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class MainActivity extends AppCompatActivity -{ - private List mDatas = new ArrayList<>(Arrays.asList("MultiItem ListView", - "RecyclerView", - "MultiItem RecyclerView")); - private ListView mListView; - - private View mEmptyView ; - - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - mListView = ((ListView) findViewById(R.id.id_listview_list)); - mEmptyView = findViewById(R.id.id_empty_view); - mListView.setAdapter(new CommonAdapter(this, R.layout.item_list, mDatas) - { - @Override - public void convert(ViewHolder holder, String o, int pos) - { - holder.setText(R.id.id_item_list_title, o); - } - }); - - mListView.setEmptyView(mEmptyView); -// TextView t1 = new TextView(this); -// t1.setText("Header 1"); -// TextView t2 = new TextView(this); -// t2.setText("Header 2"); -// mListView.addHeaderView(t1); -// mListView.addHeaderView(t2); - mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() - { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) - { - Intent intent = null; - switch (position) - { - case 0: - default: - intent = new Intent(MainActivity.this, MultiItemListViewActivity.class); - break; - case 1: - intent = new Intent(MainActivity.this, RecyclerViewActivity.class); - break; - case 2: - intent = new Intent(MainActivity.this, MultiItemRvActivity.class); - break; - - } - if (intent != null) - startActivity(intent); - } - }); - } - - -} +package com.zhy.sample; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; + +import com.zhy.adapter.abslistview.CommonAdapter; +import com.zhy.adapter.abslistview.ViewHolder; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class MainActivity extends AppCompatActivity +{ + private List mDatas = new ArrayList<>(Arrays.asList("MultiItem ListView", + "RecyclerView", + "MultiItem RecyclerView")); + private ListView mListView; + + private View mEmptyView ; + + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mListView = ((ListView) findViewById(R.id.id_listview_list)); + mEmptyView = findViewById(R.id.id_empty_view); + mListView.setAdapter(new CommonAdapter(this, R.layout.item_list, mDatas) + { + @Override + public void convert(ViewHolder holder, String o, int pos) + { + holder.setText(R.id.id_item_list_title, o); + } + + @Override + public void onViewHolderCreated(ViewHolder holder, View itemView) + { + super.onViewHolderCreated(holder, itemView); + } + }); + + mListView.setEmptyView(mEmptyView); +// TextView t1 = new TextView(this); +// t1.setText("Header 1"); +// TextView t2 = new TextView(this); +// t2.setText("Header 2"); +// mListView.addHeaderView(t1); +// mListView.addHeaderView(t2); + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() + { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) + { + Intent intent = null; + switch (position) + { + case 0: + default: + intent = new Intent(MainActivity.this, MultiItemListViewActivity.class); + break; + case 1: + intent = new Intent(MainActivity.this, RecyclerViewActivity.class); + break; + case 2: + intent = new Intent(MainActivity.this, MultiItemRvActivity.class); + break; + + } + if (intent != null) + startActivity(intent); + } + }); + } + + +} diff --git a/sample/src/main/java/com/zhy/sample/MultiItemListViewActivity.java b/sample/src/main/java/com/zhy/sample/MultiItemListViewActivity.java index 7978492..9132d76 100644 --- a/sample/src/main/java/com/zhy/sample/MultiItemListViewActivity.java +++ b/sample/src/main/java/com/zhy/sample/MultiItemListViewActivity.java @@ -1,30 +1,30 @@ -package com.zhy.sample; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.widget.ListView; - -import com.zhy.adapter.abslistview.CommonAdapter; -import com.zhy.sample.adapter.lv.ChatAdapter; -import com.zhy.sample.bean.ChatMessage; - -public class MultiItemListViewActivity extends AppCompatActivity -{ - - private ListView mListView; - private CommonAdapter mAdapter ; - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - - mListView = (ListView) findViewById(R.id.id_listview_list); - mListView.setDivider(null); - mListView.setAdapter(new ChatAdapter(this, ChatMessage.MOCK_DATAS)); - - } - -} +package com.zhy.sample; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.widget.ListView; + +import com.zhy.adapter.abslistview.CommonAdapter; +import com.zhy.sample.adapter.lv.ChatAdapter; +import com.zhy.sample.bean.ChatMessage; + +public class MultiItemListViewActivity extends AppCompatActivity +{ + + private ListView mListView; + private CommonAdapter mAdapter ; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + + mListView = (ListView) findViewById(R.id.id_listview_list); + mListView.setDivider(null); + mListView.setAdapter(new ChatAdapter(this, ChatMessage.MOCK_DATAS)); + + } + +} diff --git a/sample/src/main/java/com/zhy/sample/MultiItemRvActivity.java b/sample/src/main/java/com/zhy/sample/MultiItemRvActivity.java index cfff5a7..29475ef 100644 --- a/sample/src/main/java/com/zhy/sample/MultiItemRvActivity.java +++ b/sample/src/main/java/com/zhy/sample/MultiItemRvActivity.java @@ -1,85 +1,85 @@ -package com.zhy.sample; - -import android.os.Bundle; -import android.os.Handler; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.Toast; - -import com.zhy.adapter.recyclerview.CommonAdapter; -import com.zhy.adapter.recyclerview.wrapper.LoadMoreWrapper; -import com.zhy.sample.adapter.rv.ChatAdapterForRv; -import com.zhy.sample.bean.ChatMessage; - -import java.util.ArrayList; -import java.util.List; - -public class MultiItemRvActivity extends AppCompatActivity -{ - private RecyclerView mRecyclerView; - - private LoadMoreWrapper mLoadMoreWrapper; - private List mDatas = new ArrayList<>(); - - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_recyclerview); - - - mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); - - mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - - mDatas.addAll(ChatMessage.MOCK_DATAS); - ChatAdapterForRv adapter = new ChatAdapterForRv(this, mDatas); - - mLoadMoreWrapper = new LoadMoreWrapper(adapter); - mLoadMoreWrapper.setLoadMoreView(LayoutInflater.from(this).inflate(R.layout.default_loading, mRecyclerView, false)); - mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() - { - @Override - public void onLoadMoreRequested() - { - new Handler().postDelayed(new Runnable() - { - @Override - public void run() - { - boolean coming = Math.random() > 0.5; - ChatMessage msg = null; - msg = new ChatMessage(coming ? R.drawable.renma : R.drawable.xiaohei, coming ? "人马" : "xiaohei", "where are you " + mDatas.size(), - null, coming); - mDatas.add(msg); - mLoadMoreWrapper.notifyDataSetChanged(); - - } - }, 3000); - } - }); - - adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() - { - @Override - public void onItemClick(View view, RecyclerView.ViewHolder holder, ChatMessage o, int position) - { - Toast.makeText(MultiItemRvActivity.this, "Click:" + position + " => " + o.getContent(), Toast.LENGTH_SHORT).show(); - } - - @Override - public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, ChatMessage o, int position) - { - Toast.makeText(MultiItemRvActivity.this, "LongClick:" + position + " => " + o.getContent(), Toast.LENGTH_SHORT).show(); - return false; - } - }); - mRecyclerView.setAdapter(mLoadMoreWrapper); - } - - -} +package com.zhy.sample; + +import android.os.Bundle; +import android.os.Handler; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Toast; + +import com.zhy.adapter.recyclerview.CommonAdapter; +import com.zhy.adapter.recyclerview.wrapper.LoadMoreWrapper; +import com.zhy.sample.adapter.rv.ChatAdapterForRv; +import com.zhy.sample.bean.ChatMessage; + +import java.util.ArrayList; +import java.util.List; + +public class MultiItemRvActivity extends AppCompatActivity +{ + private RecyclerView mRecyclerView; + + private LoadMoreWrapper mLoadMoreWrapper; + private List mDatas = new ArrayList<>(); + + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_recyclerview); + + + mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); + + mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + + mDatas.addAll(ChatMessage.MOCK_DATAS); + ChatAdapterForRv adapter = new ChatAdapterForRv(this, mDatas); + + mLoadMoreWrapper = new LoadMoreWrapper(adapter); + mLoadMoreWrapper.setLoadMoreView(LayoutInflater.from(this).inflate(R.layout.default_loading, mRecyclerView, false)); + mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() + { + @Override + public void onLoadMoreRequested() + { + new Handler().postDelayed(new Runnable() + { + @Override + public void run() + { + boolean coming = Math.random() > 0.5; + ChatMessage msg = null; + msg = new ChatMessage(coming ? R.drawable.renma : R.drawable.xiaohei, coming ? "人马" : "xiaohei", "where are you " + mDatas.size(), + null, coming); + mDatas.add(msg); + mLoadMoreWrapper.notifyDataSetChanged(); + + } + }, 3000); + } + }); + + adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() + { + @Override + public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) + { + Toast.makeText(MultiItemRvActivity.this, "Click:" + position , Toast.LENGTH_SHORT).show(); + } + + @Override + public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) + { + Toast.makeText(MultiItemRvActivity.this, "LongClick:" + position , Toast.LENGTH_SHORT).show(); + return false; + } + }); + mRecyclerView.setAdapter(mLoadMoreWrapper); + } + + +} diff --git a/sample/src/main/java/com/zhy/sample/RecyclerViewActivity.java b/sample/src/main/java/com/zhy/sample/RecyclerViewActivity.java index 21f630f..5318f89 100644 --- a/sample/src/main/java/com/zhy/sample/RecyclerViewActivity.java +++ b/sample/src/main/java/com/zhy/sample/RecyclerViewActivity.java @@ -1,181 +1,181 @@ -package com.zhy.sample; - -import android.os.Bundle; -import android.os.Handler; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.StaggeredGridLayoutManager; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.TextView; -import android.widget.Toast; - -import com.zhy.adapter.recyclerview.CommonAdapter; -import com.zhy.adapter.recyclerview.base.ViewHolder; -import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper; -import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper; -import com.zhy.adapter.recyclerview.wrapper.LoadMoreWrapper; - -import java.util.ArrayList; -import java.util.List; - -public class RecyclerViewActivity extends AppCompatActivity -{ - - private RecyclerView mRecyclerView; - private List mDatas = new ArrayList<>(); - private CommonAdapter mAdapter; - private HeaderAndFooterWrapper mHeaderAndFooterWrapper; - private EmptyWrapper mEmptyWrapper; - private LoadMoreWrapper mLoadMoreWrapper; - - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_recyclerview); - - initDatas(); - - mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); -// mRecyclerView.setHasFixedSize(true); - mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); -// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); - mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); - - mAdapter = new CommonAdapter(this, R.layout.item_list, mDatas) - { - @Override - protected void convert(ViewHolder holder, String s, int position) - { - holder.setText(R.id.id_item_list_title, s + " : " + holder.getAdapterPosition() + " , " + holder.getLayoutPosition()); - } - }; - - initHeaderAndFooter(); - -// initEmptyView(); - - mLoadMoreWrapper = new LoadMoreWrapper(mHeaderAndFooterWrapper); - mLoadMoreWrapper.setLoadMoreView(R.layout.default_loading); - mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() - { - @Override - public void onLoadMoreRequested() - { - new Handler().postDelayed(new Runnable() - { - @Override - public void run() - { - for (int i = 0; i < 10; i++) - { - mDatas.add("Add:" + i); - } - mLoadMoreWrapper.notifyDataSetChanged(); - - } - }, 3000); - } - }); - - mRecyclerView.setAdapter(mLoadMoreWrapper); - mAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() - { - @Override - public void onItemClick(View view, RecyclerView.ViewHolder holder, String o, int position) - { - Toast.makeText(RecyclerViewActivity.this, "pos = " + position, Toast.LENGTH_SHORT).show(); - mAdapter.notifyItemRemoved(position); - } - - @Override - public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, String o, int position) - { - return false; - } - }); - } - - private void initEmptyView() - { - mEmptyWrapper = new EmptyWrapper(mAdapter); - mEmptyWrapper.setEmptyView(LayoutInflater.from(this).inflate(R.layout.empty_view, mRecyclerView, false)); - } - - private void initHeaderAndFooter() - { - mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter); - - TextView t1 = new TextView(this); - t1.setText("Header 1"); - TextView t2 = new TextView(this); - t2.setText("Header 2"); - mHeaderAndFooterWrapper.addHeaderView(t1); - mHeaderAndFooterWrapper.addHeaderView(t2); - } - - private void initDatas() - { - for (int i = 'A'; i <= 'z'; i++) - { - mDatas.add((char) i + ""); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) - { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_recycler_view, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) - { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - switch (id) - { - case R.id.action_linear: - mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - break; - case R.id.action_grid: - mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); - break; - case R.id.action_staggered: - mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); - break; - } - mRecyclerView.setAdapter(mLoadMoreWrapper); - - return super.onOptionsItemSelected(item); - } - - @Override - public void onStart() - { - super.onStart(); - - - } - - @Override - public void onStop() - { - super.onStop(); - - - } -} +package com.zhy.sample; + +import android.os.Bundle; +import android.os.Handler; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.StaggeredGridLayoutManager; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +import com.zhy.adapter.recyclerview.CommonAdapter; +import com.zhy.adapter.recyclerview.base.ViewHolder; +import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper; +import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper; +import com.zhy.adapter.recyclerview.wrapper.LoadMoreWrapper; + +import java.util.ArrayList; +import java.util.List; + +public class RecyclerViewActivity extends AppCompatActivity +{ + + private RecyclerView mRecyclerView; + private List mDatas = new ArrayList<>(); + private CommonAdapter mAdapter; + private HeaderAndFooterWrapper mHeaderAndFooterWrapper; + private EmptyWrapper mEmptyWrapper; + private LoadMoreWrapper mLoadMoreWrapper; + + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_recyclerview); + + initDatas(); + + mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); +// mRecyclerView.setHasFixedSize(true); + mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); +// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); + mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); + + mAdapter = new CommonAdapter(this, R.layout.item_list, mDatas) + { + @Override + protected void convert(ViewHolder holder, String s, int position) + { + holder.setText(R.id.id_item_list_title, s + " : " + holder.getAdapterPosition() + " , " + holder.getLayoutPosition()); + } + }; + + initHeaderAndFooter(); + +// initEmptyView(); + + mLoadMoreWrapper = new LoadMoreWrapper(mHeaderAndFooterWrapper); + mLoadMoreWrapper.setLoadMoreView(R.layout.default_loading); + mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() + { + @Override + public void onLoadMoreRequested() + { + new Handler().postDelayed(new Runnable() + { + @Override + public void run() + { + for (int i = 0; i < 10; i++) + { + mDatas.add("Add:" + i); + } + mLoadMoreWrapper.notifyDataSetChanged(); + + } + }, 3000); + } + }); + + mRecyclerView.setAdapter(mLoadMoreWrapper); + mAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() + { + @Override + public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) + { + Toast.makeText(RecyclerViewActivity.this, "pos = " + position, Toast.LENGTH_SHORT).show(); + mAdapter.notifyItemRemoved(position); + } + + @Override + public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) + { + return false; + } + }); + } + + private void initEmptyView() + { + mEmptyWrapper = new EmptyWrapper(mAdapter); + mEmptyWrapper.setEmptyView(LayoutInflater.from(this).inflate(R.layout.empty_view, mRecyclerView, false)); + } + + private void initHeaderAndFooter() + { + mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter); + + TextView t1 = new TextView(this); + t1.setText("Header 1"); + TextView t2 = new TextView(this); + t2.setText("Header 2"); + mHeaderAndFooterWrapper.addHeaderView(t1); + mHeaderAndFooterWrapper.addHeaderView(t2); + } + + private void initDatas() + { + for (int i = 'A'; i <= 'z'; i++) + { + mDatas.add((char) i + ""); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) + { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_recycler_view, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) + { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + switch (id) + { + case R.id.action_linear: + mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + break; + case R.id.action_grid: + mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); + break; + case R.id.action_staggered: + mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); + break; + } + mRecyclerView.setAdapter(mLoadMoreWrapper); + + return super.onOptionsItemSelected(item); + } + + @Override + public void onStart() + { + super.onStart(); + + + } + + @Override + public void onStop() + { + super.onStop(); + + + } +} diff --git a/sample/src/main/java/com/zhy/sample/adapter/lv/ChatAdapter.java b/sample/src/main/java/com/zhy/sample/adapter/lv/ChatAdapter.java index c860d02..be7f72e 100644 --- a/sample/src/main/java/com/zhy/sample/adapter/lv/ChatAdapter.java +++ b/sample/src/main/java/com/zhy/sample/adapter/lv/ChatAdapter.java @@ -1,23 +1,23 @@ -package com.zhy.sample.adapter.lv; - -import android.content.Context; - -import com.zhy.adapter.abslistview.MultiItemTypeAdapter; -import com.zhy.sample.bean.ChatMessage; - -import java.util.List; - -/** - * Created by zhy on 15/9/4. - */ -public class ChatAdapter extends MultiItemTypeAdapter -{ - public ChatAdapter(Context context, List datas) - { - super(context, datas); - - addItemViewDelegate(new MsgSendItemDelagate()); - addItemViewDelegate(new MsgComingItemDelagate()); - } - -} +package com.zhy.sample.adapter.lv; + +import android.content.Context; + +import com.zhy.adapter.abslistview.MultiItemTypeAdapter; +import com.zhy.sample.bean.ChatMessage; + +import java.util.List; + +/** + * Created by zhy on 15/9/4. + */ +public class ChatAdapter extends MultiItemTypeAdapter +{ + public ChatAdapter(Context context, List datas) + { + super(context, datas); + + addItemViewDelegate(new MsgSendItemDelagate()); + addItemViewDelegate(new MsgComingItemDelagate()); + } + +} diff --git a/sample/src/main/java/com/zhy/sample/adapter/lv/MsgComingItemDelagate.java b/sample/src/main/java/com/zhy/sample/adapter/lv/MsgComingItemDelagate.java index 6d00fd1..1021485 100644 --- a/sample/src/main/java/com/zhy/sample/adapter/lv/MsgComingItemDelagate.java +++ b/sample/src/main/java/com/zhy/sample/adapter/lv/MsgComingItemDelagate.java @@ -1,33 +1,33 @@ -package com.zhy.sample.adapter.lv; - -import com.zhy.adapter.abslistview.ViewHolder; -import com.zhy.adapter.abslistview.base.ItemViewDelegate; -import com.zhy.sample.R; -import com.zhy.sample.bean.ChatMessage; - -/** - * Created by zhy on 16/6/22. - */ -public class MsgComingItemDelagate implements ItemViewDelegate -{ - - @Override - public int getItemViewLayoutId() - { - return R.layout.main_chat_from_msg; - } - - @Override - public boolean isForViewType(ChatMessage item, int position) - { - return item.isComMeg(); - } - - @Override - public void convert(ViewHolder holder, ChatMessage chatMessage, int position) - { - holder.setText(R.id.chat_from_content, chatMessage.getContent()); - holder.setText(R.id.chat_from_name, chatMessage.getName()); - holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); - } -} +package com.zhy.sample.adapter.lv; + +import com.zhy.adapter.abslistview.ViewHolder; +import com.zhy.adapter.abslistview.base.ItemViewDelegate; +import com.zhy.sample.R; +import com.zhy.sample.bean.ChatMessage; + +/** + * Created by zhy on 16/6/22. + */ +public class MsgComingItemDelagate implements ItemViewDelegate +{ + + @Override + public int getItemViewLayoutId() + { + return R.layout.main_chat_from_msg; + } + + @Override + public boolean isForViewType(ChatMessage item, int position) + { + return item.isComMeg(); + } + + @Override + public void convert(ViewHolder holder, ChatMessage chatMessage, int position) + { + holder.setText(R.id.chat_from_content, chatMessage.getContent()); + holder.setText(R.id.chat_from_name, chatMessage.getName()); + holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); + } +} diff --git a/sample/src/main/java/com/zhy/sample/adapter/lv/MsgSendItemDelagate.java b/sample/src/main/java/com/zhy/sample/adapter/lv/MsgSendItemDelagate.java index 45744f5..4a2b6fe 100644 --- a/sample/src/main/java/com/zhy/sample/adapter/lv/MsgSendItemDelagate.java +++ b/sample/src/main/java/com/zhy/sample/adapter/lv/MsgSendItemDelagate.java @@ -1,33 +1,33 @@ -package com.zhy.sample.adapter.lv; - -import com.zhy.adapter.abslistview.ViewHolder; -import com.zhy.adapter.abslistview.base.ItemViewDelegate; -import com.zhy.sample.R; -import com.zhy.sample.bean.ChatMessage; - -/** - * Created by zhy on 16/6/22. - */ -public class MsgSendItemDelagate implements ItemViewDelegate -{ - - @Override - public int getItemViewLayoutId() - { - return R.layout.main_chat_send_msg; - } - - @Override - public boolean isForViewType(ChatMessage item, int position) - { - return !item.isComMeg(); - } - - @Override - public void convert(ViewHolder holder, ChatMessage chatMessage, int position) - { - holder.setText(R.id.chat_send_content, chatMessage.getContent()); - holder.setText(R.id.chat_send_name, chatMessage.getName()); - holder.setImageResource(R.id.chat_send_icon, chatMessage.getIcon()); - } -} +package com.zhy.sample.adapter.lv; + +import com.zhy.adapter.abslistview.ViewHolder; +import com.zhy.adapter.abslistview.base.ItemViewDelegate; +import com.zhy.sample.R; +import com.zhy.sample.bean.ChatMessage; + +/** + * Created by zhy on 16/6/22. + */ +public class MsgSendItemDelagate implements ItemViewDelegate +{ + + @Override + public int getItemViewLayoutId() + { + return R.layout.main_chat_send_msg; + } + + @Override + public boolean isForViewType(ChatMessage item, int position) + { + return !item.isComMeg(); + } + + @Override + public void convert(ViewHolder holder, ChatMessage chatMessage, int position) + { + holder.setText(R.id.chat_send_content, chatMessage.getContent()); + holder.setText(R.id.chat_send_name, chatMessage.getName()); + holder.setImageResource(R.id.chat_send_icon, chatMessage.getIcon()); + } +} diff --git a/sample/src/main/java/com/zhy/sample/adapter/rv/ChatAdapterForRv.java b/sample/src/main/java/com/zhy/sample/adapter/rv/ChatAdapterForRv.java index 151e7c5..422b1b8 100644 --- a/sample/src/main/java/com/zhy/sample/adapter/rv/ChatAdapterForRv.java +++ b/sample/src/main/java/com/zhy/sample/adapter/rv/ChatAdapterForRv.java @@ -1,22 +1,22 @@ -package com.zhy.sample.adapter.rv; - -import android.content.Context; - -import com.zhy.adapter.recyclerview.MultiItemTypeAdapter; -import com.zhy.sample.bean.ChatMessage; - -import java.util.List; - -/** - * Created by zhy on 15/9/4. - */ -public class ChatAdapterForRv extends MultiItemTypeAdapter -{ - public ChatAdapterForRv(Context context, List datas) - { - super(context, datas); - - addItemViewDelegate(new MsgSendItemDelagate()); - addItemViewDelegate(new MsgComingItemDelagate()); - } -} +package com.zhy.sample.adapter.rv; + +import android.content.Context; + +import com.zhy.adapter.recyclerview.MultiItemTypeAdapter; +import com.zhy.sample.bean.ChatMessage; + +import java.util.List; + +/** + * Created by zhy on 15/9/4. + */ +public class ChatAdapterForRv extends MultiItemTypeAdapter +{ + public ChatAdapterForRv(Context context, List datas) + { + super(context, datas); + + addItemViewDelegate(new MsgSendItemDelagate()); + addItemViewDelegate(new MsgComingItemDelagate()); + } +} diff --git a/sample/src/main/java/com/zhy/sample/adapter/rv/MsgComingItemDelagate.java b/sample/src/main/java/com/zhy/sample/adapter/rv/MsgComingItemDelagate.java index 92d9288..dc4f39a 100644 --- a/sample/src/main/java/com/zhy/sample/adapter/rv/MsgComingItemDelagate.java +++ b/sample/src/main/java/com/zhy/sample/adapter/rv/MsgComingItemDelagate.java @@ -1,33 +1,33 @@ -package com.zhy.sample.adapter.rv; - -import com.zhy.adapter.recyclerview.base.ItemViewDelegate; -import com.zhy.adapter.recyclerview.base.ViewHolder; -import com.zhy.sample.R; -import com.zhy.sample.bean.ChatMessage; - -/** - * Created by zhy on 16/6/22. - */ -public class MsgComingItemDelagate implements ItemViewDelegate -{ - - @Override - public int getItemViewLayoutId() - { - return R.layout.main_chat_from_msg; - } - - @Override - public boolean isForViewType(ChatMessage item, int position) - { - return item.isComMeg(); - } - - @Override - public void convert(ViewHolder holder, ChatMessage chatMessage, int position) - { - holder.setText(R.id.chat_from_content, chatMessage.getContent()); - holder.setText(R.id.chat_from_name, chatMessage.getName()); - holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); - } -} +package com.zhy.sample.adapter.rv; + +import com.zhy.adapter.recyclerview.base.ItemViewDelegate; +import com.zhy.adapter.recyclerview.base.ViewHolder; +import com.zhy.sample.R; +import com.zhy.sample.bean.ChatMessage; + +/** + * Created by zhy on 16/6/22. + */ +public class MsgComingItemDelagate implements ItemViewDelegate +{ + + @Override + public int getItemViewLayoutId() + { + return R.layout.main_chat_from_msg; + } + + @Override + public boolean isForViewType(ChatMessage item, int position) + { + return item.isComMeg(); + } + + @Override + public void convert(ViewHolder holder, ChatMessage chatMessage, int position) + { + holder.setText(R.id.chat_from_content, chatMessage.getContent()); + holder.setText(R.id.chat_from_name, chatMessage.getName()); + holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); + } +} diff --git a/sample/src/main/java/com/zhy/sample/adapter/rv/MsgSendItemDelagate.java b/sample/src/main/java/com/zhy/sample/adapter/rv/MsgSendItemDelagate.java index 823757c..0f69786 100644 --- a/sample/src/main/java/com/zhy/sample/adapter/rv/MsgSendItemDelagate.java +++ b/sample/src/main/java/com/zhy/sample/adapter/rv/MsgSendItemDelagate.java @@ -1,33 +1,33 @@ -package com.zhy.sample.adapter.rv; - -import com.zhy.adapter.recyclerview.base.ItemViewDelegate; -import com.zhy.adapter.recyclerview.base.ViewHolder; -import com.zhy.sample.R; -import com.zhy.sample.bean.ChatMessage; - -/** - * Created by zhy on 16/6/22. - */ -public class MsgSendItemDelagate implements ItemViewDelegate -{ - - @Override - public int getItemViewLayoutId() - { - return R.layout.main_chat_send_msg; - } - - @Override - public boolean isForViewType(ChatMessage item, int position) - { - return !item.isComMeg(); - } - - @Override - public void convert(ViewHolder holder, ChatMessage chatMessage, int position) - { - holder.setText(R.id.chat_send_content, chatMessage.getContent()); - holder.setText(R.id.chat_send_name, chatMessage.getName()); - holder.setImageResource(R.id.chat_send_icon, chatMessage.getIcon()); - } -} +package com.zhy.sample.adapter.rv; + +import com.zhy.adapter.recyclerview.base.ItemViewDelegate; +import com.zhy.adapter.recyclerview.base.ViewHolder; +import com.zhy.sample.R; +import com.zhy.sample.bean.ChatMessage; + +/** + * Created by zhy on 16/6/22. + */ +public class MsgSendItemDelagate implements ItemViewDelegate +{ + + @Override + public int getItemViewLayoutId() + { + return R.layout.main_chat_send_msg; + } + + @Override + public boolean isForViewType(ChatMessage item, int position) + { + return !item.isComMeg(); + } + + @Override + public void convert(ViewHolder holder, ChatMessage chatMessage, int position) + { + holder.setText(R.id.chat_send_content, chatMessage.getContent()); + holder.setText(R.id.chat_send_name, chatMessage.getName()); + holder.setImageResource(R.id.chat_send_icon, chatMessage.getIcon()); + } +} diff --git a/sample/src/main/java/com/zhy/sample/bean/ChatMessage.java b/sample/src/main/java/com/zhy/sample/bean/ChatMessage.java index 3c4e282..c7aba0b 100755 --- a/sample/src/main/java/com/zhy/sample/bean/ChatMessage.java +++ b/sample/src/main/java/com/zhy/sample/bean/ChatMessage.java @@ -1,169 +1,169 @@ -package com.zhy.sample.bean; - -import com.zhy.sample.R; - -import java.util.ArrayList; -import java.util.List; - -public class ChatMessage -{ - private int icon; - private String name; - private String content; - private String createDate; - private boolean isComMeg; - - public final static int RECIEVE_MSG = 0; - public final static int SEND_MSG = 1; - - public ChatMessage(int icon, String name, String content, - String createDate, boolean isComMeg) - { - this.icon = icon; - this.name = name; - this.content = content; - this.createDate = createDate; - this.isComMeg = isComMeg; - } - - public boolean isComMeg() - { - return isComMeg; - } - - public void setComMeg(boolean isComMeg) - { - this.isComMeg = isComMeg; - } - - public String getContent() - { - return content; - } - - public void setContent(String content) - { - this.content = content; - } - - public int getIcon() - { - return icon; - } - - public void setIcon(int icon) - { - this.icon = icon; - } - - public String getName() - { - return name; - } - - public void setName(String name) - { - this.name = name; - } - - public String getCreateDate() - { - return createDate; - } - - public void setCreateDate(String createDate) - { - this.createDate = createDate; - } - - @Override - public String toString() - { - return "ChatMessage [icon=" + icon + ", name=" + name + ", content=" - + content + ", createDate=" + createDate +", isComing = "+ isComMeg()+ "]"; - } - - public static List MOCK_DATAS = new ArrayList<>(); - - static { - ChatMessage msg = null; - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", - null, true); - MOCK_DATAS.add(msg); - msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", - null, false); - MOCK_DATAS.add(msg); - } - - -} +package com.zhy.sample.bean; + +import com.zhy.sample.R; + +import java.util.ArrayList; +import java.util.List; + +public class ChatMessage +{ + private int icon; + private String name; + private String content; + private String createDate; + private boolean isComMeg; + + public final static int RECIEVE_MSG = 0; + public final static int SEND_MSG = 1; + + public ChatMessage(int icon, String name, String content, + String createDate, boolean isComMeg) + { + this.icon = icon; + this.name = name; + this.content = content; + this.createDate = createDate; + this.isComMeg = isComMeg; + } + + public boolean isComMeg() + { + return isComMeg; + } + + public void setComMeg(boolean isComMeg) + { + this.isComMeg = isComMeg; + } + + public String getContent() + { + return content; + } + + public void setContent(String content) + { + this.content = content; + } + + public int getIcon() + { + return icon; + } + + public void setIcon(int icon) + { + this.icon = icon; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getCreateDate() + { + return createDate; + } + + public void setCreateDate(String createDate) + { + this.createDate = createDate; + } + + @Override + public String toString() + { + return "ChatMessage [icon=" + icon + ", name=" + name + ", content=" + + content + ", createDate=" + createDate +", isComing = "+ isComMeg()+ "]"; + } + + public static List MOCK_DATAS = new ArrayList<>(); + + static { + ChatMessage msg = null; + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.renma, "renma", "where are you ", + null, true); + MOCK_DATAS.add(msg); + msg = new ChatMessage(R.drawable.xiaohei, "xiaohei", "where are you ", + null, false); + MOCK_DATAS.add(msg); + } + + +} diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index fc9cf7a..195d758 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -1,19 +1,19 @@ - - - - - - - + + + + + + + diff --git a/sample/src/main/res/layout/activity_multi_item_list_view.xml b/sample/src/main/res/layout/activity_multi_item_list_view.xml index d8ca001..b915f32 100644 --- a/sample/src/main/res/layout/activity_multi_item_list_view.xml +++ b/sample/src/main/res/layout/activity_multi_item_list_view.xml @@ -1,16 +1,16 @@ - - - - - + + + + + diff --git a/sample/src/main/res/layout/activity_multi_item_rv.xml b/sample/src/main/res/layout/activity_multi_item_rv.xml index 5ea946f..cce4fc8 100644 --- a/sample/src/main/res/layout/activity_multi_item_rv.xml +++ b/sample/src/main/res/layout/activity_multi_item_rv.xml @@ -1,16 +1,16 @@ - - - - - + + + + + diff --git a/sample/src/main/res/layout/activity_recyclerview.xml b/sample/src/main/res/layout/activity_recyclerview.xml index ad1bff3..2bcec8e 100644 --- a/sample/src/main/res/layout/activity_recyclerview.xml +++ b/sample/src/main/res/layout/activity_recyclerview.xml @@ -1,16 +1,16 @@ - - - - - + + + + + diff --git a/sample/src/main/res/layout/activity_rv_width_header.xml b/sample/src/main/res/layout/activity_rv_width_header.xml index e248531..2d6d13e 100644 --- a/sample/src/main/res/layout/activity_rv_width_header.xml +++ b/sample/src/main/res/layout/activity_rv_width_header.xml @@ -1,16 +1,16 @@ - - - - - + + + + + diff --git a/sample/src/main/res/layout/default_loading.xml b/sample/src/main/res/layout/default_loading.xml index e795a63..391b293 100755 --- a/sample/src/main/res/layout/default_loading.xml +++ b/sample/src/main/res/layout/default_loading.xml @@ -1,21 +1,21 @@ - - - - - - + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/empty_view.xml b/sample/src/main/res/layout/empty_view.xml index c4c3f14..d7dee27 100644 --- a/sample/src/main/res/layout/empty_view.xml +++ b/sample/src/main/res/layout/empty_view.xml @@ -1,13 +1,13 @@ - - - - - - + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/header.xml b/sample/src/main/res/layout/header.xml index 302a64f..0f93a94 100644 --- a/sample/src/main/res/layout/header.xml +++ b/sample/src/main/res/layout/header.xml @@ -1,11 +1,11 @@ - - - + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/item_list.xml b/sample/src/main/res/layout/item_list.xml index d8609e8..5ac0d56 100644 --- a/sample/src/main/res/layout/item_list.xml +++ b/sample/src/main/res/layout/item_list.xml @@ -1,14 +1,14 @@ - - - - - - + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/main_chat_from_msg.xml b/sample/src/main/res/layout/main_chat_from_msg.xml index 25e9521..a4ad61b 100755 --- a/sample/src/main/res/layout/main_chat_from_msg.xml +++ b/sample/src/main/res/layout/main_chat_from_msg.xml @@ -1,50 +1,50 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/main_chat_send_msg.xml b/sample/src/main/res/layout/main_chat_send_msg.xml index 9a950ad..73571b3 100755 --- a/sample/src/main/res/layout/main_chat_send_msg.xml +++ b/sample/src/main/res/layout/main_chat_send_msg.xml @@ -1,51 +1,51 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/menu/menu_main.xml b/sample/src/main/res/menu/menu_main.xml index fa078bd..bcd0b32 100644 --- a/sample/src/main/res/menu/menu_main.xml +++ b/sample/src/main/res/menu/menu_main.xml @@ -1,5 +1,5 @@ -

- + + diff --git a/sample/src/main/res/menu/menu_multi_item_list_view.xml b/sample/src/main/res/menu/menu_multi_item_list_view.xml index 267bcee..4627322 100644 --- a/sample/src/main/res/menu/menu_multi_item_list_view.xml +++ b/sample/src/main/res/menu/menu_multi_item_list_view.xml @@ -1,9 +1,9 @@ - - - + + + diff --git a/sample/src/main/res/menu/menu_multi_item_rv.xml b/sample/src/main/res/menu/menu_multi_item_rv.xml index f70acad..a59ecfa 100644 --- a/sample/src/main/res/menu/menu_multi_item_rv.xml +++ b/sample/src/main/res/menu/menu_multi_item_rv.xml @@ -1,9 +1,9 @@ - - - + + + diff --git a/sample/src/main/res/menu/menu_recycler_view.xml b/sample/src/main/res/menu/menu_recycler_view.xml index c3fef35..eba6517 100644 --- a/sample/src/main/res/menu/menu_recycler_view.xml +++ b/sample/src/main/res/menu/menu_recycler_view.xml @@ -1,19 +1,19 @@ - - - - - - - + + + + + + + diff --git a/sample/src/main/res/menu/menu_rv_width_header.xml b/sample/src/main/res/menu/menu_rv_width_header.xml index 6f65d43..2f77ebb 100644 --- a/sample/src/main/res/menu/menu_rv_width_header.xml +++ b/sample/src/main/res/menu/menu_rv_width_header.xml @@ -1,9 +1,9 @@ - - - + + + diff --git a/sample/src/main/res/values-w820dp/dimens.xml b/sample/src/main/res/values-w820dp/dimens.xml index 63fc816..62df187 100644 --- a/sample/src/main/res/values-w820dp/dimens.xml +++ b/sample/src/main/res/values-w820dp/dimens.xml @@ -1,6 +1,6 @@ - - - 64dp - + + + 64dp + diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml index 47c8224..295b5a9 100644 --- a/sample/src/main/res/values/dimens.xml +++ b/sample/src/main/res/values/dimens.xml @@ -1,5 +1,5 @@ - - - 16dp - 16dp - + + + 16dp + 16dp + diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index 0b65811..21965b0 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -1,10 +1,10 @@ - - sample - - Hello world! - Settings - MultiItemListViewActivity - RecyclerViewActivity - MultiItemRvActivity - RvWidthHeaderActivity - + + sample + + Hello world! + Settings + MultiItemListViewActivity + RecyclerViewActivity + MultiItemRvActivity + RvWidthHeaderActivity + diff --git a/sample/src/main/res/values/styles.xml b/sample/src/main/res/values/styles.xml index 766ab99..084b42b 100644 --- a/sample/src/main/res/values/styles.xml +++ b/sample/src/main/res/values/styles.xml @@ -1,8 +1,8 @@ - - - - - - + + + + + + diff --git a/settings.gradle b/settings.gradle index 0205238..d4e35ad 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':sample', ':base-adapter-library', ':baseadapter-recyclerview' +include ':sample', ':base-adapter-library', ':baseadapter-recyclerview'