├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── jarRepositories.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yuyh │ │ └── easyadapter │ │ ├── AdapterImageLoader.java │ │ ├── abslistview │ │ ├── EasyLVAdapter.java │ │ └── EasyLVHolder.java │ │ ├── helper │ │ ├── DataHelper.java │ │ └── ViewHelper.java │ │ └── recyclerview │ │ ├── EasyRVAdapter.java │ │ └── EasyRVHolder.java │ └── res │ └── values │ ├── ids.xml │ └── strings.xml ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yuyh │ │ └── easyadapter │ │ ├── App.java │ │ ├── Bean.java │ │ ├── DividerItemDecoration.java │ │ ├── GridItemDecoration.java │ │ ├── ListViewActivity.java │ │ ├── ListViewAdapter.java │ │ ├── MainActivity.java │ │ ├── RecyclerViewActivity.java │ │ └── RecyclerViewAdapter.java │ └── res │ ├── layout │ ├── activity_list_view.xml │ ├── activity_main.xml │ ├── activity_recycler_view.xml │ ├── item_header.xml │ ├── item_list_view_1.xml │ └── item_list_view_2.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | EasyAdapter -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 22 | 42 | 59 | 60 | 61 | 62 | 63 | 64 | 66 | 67 | $USER_HOME$/.subversion 68 | 69 | 70 | 71 | 72 | 73 | 1.8 74 | 75 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright (c) 2016 smuyyh. All right reserved. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EasyAdapter 2 | #### Android万能适配器,通吃所有的AbsListView、RecyclerView。 3 | 4 | ### 添加依赖 5 | ``` 6 | buildscript { 7 | repositories { 8 | ... 9 | maven { url "https://jitpack.io" } 10 | } 11 | dependencies { 12 | ... 13 | } 14 | } 15 | ``` 16 | 17 | ``` 18 | dependencies { 19 | implementation 'com.github.smuyyh:EasyAdapter:1.3.0' 20 | } 21 | ``` 22 | 23 | ### 用法 24 | 25 | #### step1 26 | 27 | ```java 28 | 29 | // ListView 30 | public class ListViewAdapter extends EasyLVAdapter { 31 | 32 | public ListViewAdapter(Context context, List list, int... layoutIds) { 33 | super(context, list, layoutIds); 34 | } 35 | 36 | @Override 37 | public void convert(EasyLVHolder holder, int position, final Bean bean) { 38 | holder.setText(R.id.tv, bean.name) 39 | .setText(R.id.tv2, bean.name2) 40 | .setImageResource(R.id.img, context.getResource().getDrawable(bean.imgRes)); 41 | } 42 | 43 | @Override 44 | public int getLayoutIndex(int position, Bean item) { 45 | // 若需多样式布局,则重写该方法 46 | if (position % 2 == 0) 47 | return 1; 48 | else return 0; 49 | } 50 | } 51 | 52 | // RecyclerView 53 | public class RecyclerViewAdapter extends EasyRVAdapter { 54 | public RecyclerViewAdapter(Context context, List list, int... layoutIds) { 55 | super(context, list, layoutIds); 56 | } 57 | 58 | @Override 59 | protected void onBindData(EasyRVHolder viewHolder, final int position, Bean item) { 60 | viewHolder.setText(R.id.tv, item.name); 61 | // 自定义点击可覆盖父类的setOnItemClickListener 62 | /*viewHolder.getItemView().setOnClickListener(new View.OnClickListener() { 63 | @Override 64 | public void onClick(View v) { 65 | Toast.makeText(mContext, position+"---", Toast.LENGTH_SHORT).show(); 66 | } 67 | });*/ 68 | viewHolder.setImageUrl(R.id.iv, "https://img.25pp.com//ppnews/zixun_img/6fc/096/1444271700253231.jpg"); 69 | } 70 | 71 | @Override 72 | public int getLayoutIndex(int position, Bean item) { 73 | if (position % 2 == 0) 74 | return 0; 75 | else return 1; 76 | } 77 | } 78 | ``` 79 | 80 | #### step2 81 | 82 | ```java 83 | // 1、若需要调用adapter.setImageUrl,可以在Application配置全局统一回调 84 | AdapterImageLoader.init(new AdapterImageLoader.ImageLoader() { 85 | @Override 86 | public void loadImage(Context context, String url, ImageView view) { 87 | Glide.with(context) 88 | .load(url) 89 | .into(view); 90 | } 91 | }); 92 | 93 | // 2、若adapter需要单独定制ImageLoader,比如placeHolder,则重写该方法,以替换全局初始化的ImageLoader 94 | @Override 95 | public AdapterImageLoader.ImageLoader getImageLoader() { 96 | return new AdapterImageLoader.ImageLoader() { 97 | @Override 98 | public void loadImage(Context context, String url, ImageView view) { 99 | Glide.with(context) 100 | .load(url) 101 | .override(10, 10) 102 | .into(view); 103 | } 104 | }; 105 | } 106 | ``` 107 | 108 | #### step3 109 | 110 | ```java 111 | ListView lv = (ListView) findViewById(R.id.lv); 112 | lv.setAdapter(new ListViewAdapter(this, list, R.layout.item_list_view_1, R.layout.item_list_view_2)); 113 | ``` 114 | 115 | ### 数据操作 116 | EasyXXAdapter提供以下方法,便于对List数据的增删改。 117 | ```java 118 | boolean addAll(List list); 119 | 120 | boolean addAll(int position, List list); 121 | 122 | void add(T data); 123 | 124 | void add(int position, T data); 125 | 126 | void clear(); 127 | 128 | boolean contains(T data); 129 | 130 | T getData(int index); 131 | 132 | void modify(T oldData, T newData); 133 | 134 | void modify(int index, T newData); 135 | 136 | boolean remove(T data); 137 | 138 | void remove(int index); 139 | 140 | // RV 独有 点击事件 141 | void setOnItemClickListener(OnItemClickListener itemClickListener); 142 | 143 | // RV 独有 长按事件 144 | void setOnItemLongClickListener(OnItemLongClickListener itemLongClickListener); 145 | ``` 146 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | google() 4 | mavenCentral() 5 | maven { url "https://jitpack.io" } 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:3.4.2' 9 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | google() 16 | mavenCentral() 17 | maven { url "https://jitpack.io" } 18 | } 19 | 20 | tasks.withType(Javadoc) { 21 | options.addStringOption('Xdoclint:none', '-quiet') 22 | options.addStringOption('encoding', 'UTF-8') 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smuyyh/EasyAdapter/35a001ae96e6a03db72cc8a68a7b9dc5408cb573/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Aug 05 17:34:35 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group = 'com.yuyang.library' 5 | 6 | android { 7 | compileSdkVersion 28 8 | 9 | defaultConfig { 10 | minSdkVersion 15 11 | targetSdkVersion 28 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | lintOptions { 25 | abortOnError false 26 | } 27 | 28 | } 29 | 30 | dependencies { 31 | api 'com.android.support:appcompat-v7:28.0.0' 32 | api("com.android.support:recyclerview-v7:28.0.0") 33 | } 34 | 35 | task androidJavadocs(type: Javadoc) { 36 | source = android.sourceSets.main.java.srcDirs 37 | } 38 | 39 | task androidJavadocsJar(type: Jar) { 40 | classifier = 'javadoc' 41 | from androidJavadocs.destinationDir 42 | } 43 | task androidSourcesJar(type: Jar) { 44 | classifier = 'sources' 45 | from android.sourceSets.main.java.srcDirs 46 | } 47 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/yuyuhang/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/AdapterImageLoader.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.content.Context; 4 | import android.widget.ImageView; 5 | 6 | /** 7 | * 若需调用setImageUrl加载网络图片,则注册全局ImageLoader回调 8 | *

9 | * Created by yuyuhang on 2018/6/14. 10 | */ 11 | public class AdapterImageLoader { 12 | 13 | public static ImageLoader sImageLoader; 14 | 15 | public static void init(ImageLoader loader) { 16 | sImageLoader = loader; 17 | } 18 | 19 | public interface ImageLoader { 20 | 21 | void loadImage(Context context, String url, ImageView view); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/abslistview/EasyLVAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter.abslistview; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | 9 | import com.yuyh.easyadapter.AdapterImageLoader; 10 | import com.yuyh.easyadapter.helper.DataHelper; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * @author yuyh. 16 | * @date 2016/7/21. 17 | */ 18 | public abstract class EasyLVAdapter extends BaseAdapter implements DataHelper { 19 | 20 | protected Context mContext; 21 | protected List mList; 22 | protected int[] layoutIds; 23 | protected LayoutInflater mLInflater; 24 | 25 | public EasyLVAdapter(Context context, List list, int... layoutIds) { 26 | this.mContext = context; 27 | this.mList = list; 28 | this.layoutIds = layoutIds; 29 | this.mLInflater = LayoutInflater.from(mContext); 30 | } 31 | 32 | public EasyLVAdapter(Context context, List list) { 33 | this.mContext = context; 34 | this.mList = list; 35 | this.mLInflater = LayoutInflater.from(mContext); 36 | } 37 | 38 | 39 | @Override 40 | public int getCount() { 41 | return mList == null ? 0 : mList.size(); 42 | } 43 | 44 | @Override 45 | public Object getItem(int position) { 46 | return mList == null ? null : mList.get(position); 47 | } 48 | 49 | @Override 50 | public long getItemId(int position) { 51 | return position; 52 | } 53 | 54 | @Override 55 | public View getView(int position, View convertView, ViewGroup parent) { 56 | int layoutId = getViewCheckLayoutId(position); 57 | EasyLVHolder holder = EasyLVHolder.get(mContext, position, convertView, parent, layoutId, getImageLoader()); 58 | convert(holder, position, mList.get(position)); 59 | return holder.getConvertView(layoutId); 60 | } 61 | 62 | private int getViewCheckLayoutId(int position) { 63 | int layoutId; 64 | if (layoutIds == null || layoutIds.length == 0) { 65 | layoutId = getLayoutId(position, mList.get(position)); 66 | } else { 67 | layoutId = layoutIds[getLayoutIndex(position, mList.get(position))]; 68 | } 69 | return layoutId; 70 | } 71 | 72 | /** 73 | * 若构造函数没有指定layoutIds, 则必须重写该方法 74 | * 75 | * @param position 76 | * @param item 77 | * @return layoutId 78 | */ 79 | public int getLayoutId(int position, T item) { 80 | return 0; 81 | } 82 | 83 | /** 84 | * 指定item布局样式在layoutIds的索引。默认为第一个 85 | * 86 | * @param position 87 | * @param item 88 | * @return 89 | */ 90 | public int getLayoutIndex(int position, T item) { 91 | return 0; 92 | } 93 | 94 | /** 95 | * 指定网络图片加载器,不指定则默认用全局注册的 96 | * 97 | * @return 98 | */ 99 | public AdapterImageLoader.ImageLoader getImageLoader() { 100 | return null; 101 | } 102 | 103 | public abstract void convert(EasyLVHolder holder, int position, T t); 104 | 105 | @Override 106 | public boolean addAll(List list) { 107 | boolean result = mList.addAll(list); 108 | notifyDataSetChanged(); 109 | return result; 110 | } 111 | 112 | @Override 113 | public boolean addAll(int position, List list) { 114 | boolean result = mList.addAll(position, list); 115 | notifyDataSetChanged(); 116 | return result; 117 | } 118 | 119 | @Override 120 | public void add(T data) { 121 | mList.add(data); 122 | notifyDataSetChanged(); 123 | } 124 | 125 | @Override 126 | public void add(int position, T data) { 127 | mList.add(position, data); 128 | notifyDataSetChanged(); 129 | } 130 | 131 | @Override 132 | public void clear() { 133 | mList.clear(); 134 | notifyDataSetChanged(); 135 | } 136 | 137 | @Override 138 | public boolean contains(T data) { 139 | return mList.contains(data); 140 | } 141 | 142 | @Override 143 | public T getData(int index) { 144 | return mList.get(index); 145 | } 146 | 147 | @Override 148 | public void modify(T oldData, T newData) { 149 | modify(mList.indexOf(oldData), newData); 150 | } 151 | 152 | @Override 153 | public void modify(int index, T newData) { 154 | mList.set(index, newData); 155 | notifyDataSetChanged(); 156 | } 157 | 158 | @Override 159 | public boolean remove(T data) { 160 | boolean result = mList.remove(data); 161 | notifyDataSetChanged(); 162 | return result; 163 | } 164 | 165 | @Override 166 | public void remove(int index) { 167 | mList.remove(index); 168 | notifyDataSetChanged(); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/abslistview/EasyLVHolder.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter.abslistview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.graphics.drawable.Drawable; 8 | import android.os.Build; 9 | import android.util.SparseArray; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.view.animation.AlphaAnimation; 14 | import android.widget.Checkable; 15 | import android.widget.ImageView; 16 | import android.widget.TextView; 17 | 18 | import com.yuyh.easyadapter.AdapterImageLoader; 19 | import com.yuyh.easyadapter.helper.ViewHelper; 20 | 21 | /** 22 | * @author yuyh. 23 | * @date 2016/7/21. 24 | */ 25 | public class EasyLVHolder implements ViewHelper.AbsListView { 26 | 27 | /** 28 | * findViewById后保存View集合 29 | */ 30 | private SparseArray mViews = new SparseArray<>(); 31 | private SparseArray mConvertViews = new SparseArray<>(); 32 | 33 | private View mConvertView; 34 | protected int mPosition; 35 | protected int mLayoutId; 36 | protected Context mContext; 37 | 38 | protected AdapterImageLoader.ImageLoader imageLoader; 39 | 40 | protected EasyLVHolder(Context context, int position, ViewGroup parent, int layoutId, AdapterImageLoader.ImageLoader imageLoader) { 41 | this.mConvertView = mConvertViews.get(layoutId); 42 | this.mPosition = position; 43 | this.mContext = context; 44 | this.mLayoutId = layoutId; 45 | this.imageLoader = imageLoader; 46 | if (mConvertView == null) { 47 | mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); 48 | mConvertViews.put(layoutId, mConvertView); 49 | mConvertView.setTag(this); 50 | } 51 | } 52 | 53 | protected EasyLVHolder() { 54 | } 55 | 56 | public static EasyLVHolder get(Context context, int position, View convertView, ViewGroup parent, int layoutId, AdapterImageLoader.ImageLoader imageLoader) { 57 | if (convertView == null) { 58 | return new EasyLVHolder(context, position, parent, layoutId, imageLoader); 59 | } else { 60 | EasyLVHolder holder = (EasyLVHolder) convertView.getTag(); 61 | if (holder.mLayoutId != layoutId) { 62 | return new EasyLVHolder(context, position, parent, layoutId, imageLoader); 63 | } 64 | holder.setPosition(position); 65 | return holder; 66 | } 67 | } 68 | 69 | /** 70 | * 获取item布局 71 | * 72 | * @return 73 | */ 74 | public View getConvertView() { 75 | return mConvertViews.valueAt(0); 76 | } 77 | 78 | public View getConvertView(int layoutId) { 79 | return mConvertViews.get(layoutId); 80 | } 81 | 82 | public V getView(int viewId) { 83 | View view = mViews.get(viewId); 84 | if (view == null) { 85 | view = mConvertView.findViewById(viewId); 86 | mViews.put(viewId, view); 87 | } 88 | return (V) view; 89 | } 90 | 91 | public void setPosition(int mPosition) { 92 | this.mPosition = mPosition; 93 | } 94 | 95 | public int getLayoutId() { 96 | return mLayoutId; 97 | } 98 | 99 | @Override 100 | public EasyLVHolder setText(int viewId, String value) { 101 | TextView view = getView(viewId); 102 | view.setText(value); 103 | return this; 104 | } 105 | 106 | @Override 107 | public EasyLVHolder setTextColor(int viewId, int color) { 108 | TextView view = getView(viewId); 109 | view.setTextColor(color); 110 | return this; 111 | } 112 | 113 | @Override 114 | public EasyLVHolder setTextColorRes(int viewId, int colorRes) { 115 | TextView view = getView(viewId); 116 | view.setTextColor(mContext.getResources().getColor(colorRes, null)); 117 | return this; 118 | } 119 | 120 | @Override 121 | public EasyLVHolder setImageResource(int viewId, int imgResId) { 122 | ImageView view = getView(viewId); 123 | view.setImageResource(imgResId); 124 | return this; 125 | } 126 | 127 | @Override 128 | public EasyLVHolder setBackgroundColor(int viewId, int color) { 129 | View view = getView(viewId); 130 | view.setBackgroundColor(color); 131 | return this; 132 | } 133 | 134 | @Override 135 | public EasyLVHolder setBackgroundColorRes(int viewId, int colorRes) { 136 | View view = getView(viewId); 137 | view.setBackgroundResource(colorRes); 138 | return this; 139 | } 140 | 141 | @Override 142 | public EasyLVHolder setImageDrawable(int viewId, Drawable drawable) { 143 | ImageView view = getView(viewId); 144 | view.setImageDrawable(drawable); 145 | return this; 146 | } 147 | 148 | @Override 149 | public EasyLVHolder setImageDrawableRes(int viewId, int drawableRes) { 150 | Drawable drawable = mContext.getResources().getDrawable(drawableRes, null); 151 | return setImageDrawable(viewId, drawable); 152 | } 153 | 154 | @Override 155 | public EasyLVHolder setImageUrl(int viewId, String imgUrl) { 156 | ImageView imageView = getView(viewId); 157 | if (imageLoader != null) { 158 | imageLoader.loadImage(mContext, imgUrl, imageView); 159 | } else if (AdapterImageLoader.sImageLoader != null) { 160 | AdapterImageLoader.sImageLoader.loadImage(mContext, imgUrl, imageView); 161 | } 162 | return this; 163 | } 164 | 165 | @Override 166 | public EasyLVHolder setImageBitmap(int viewId, Bitmap imgBitmap) { 167 | ImageView view = getView(viewId); 168 | view.setImageBitmap(imgBitmap); 169 | return this; 170 | } 171 | 172 | @Override 173 | public EasyLVHolder setVisible(int viewId, boolean visible) { 174 | View view = getView(viewId); 175 | view.setVisibility(visible ? View.VISIBLE : View.GONE); 176 | return this; 177 | } 178 | 179 | @Override 180 | public EasyLVHolder setVisible(int viewId, int visible) { 181 | View view = getView(viewId); 182 | view.setVisibility(visible); 183 | return this; 184 | } 185 | 186 | @Override 187 | public EasyLVHolder setTag(int viewId, Object tag) { 188 | View view = getView(viewId); 189 | view.setTag(tag); 190 | return this; 191 | } 192 | 193 | @Override 194 | public EasyLVHolder setTag(int viewId, int key, Object tag) { 195 | View view = getView(viewId); 196 | view.setTag(key, tag); 197 | return this; 198 | } 199 | 200 | @Override 201 | public EasyLVHolder setChecked(int viewId, boolean checked) { 202 | Checkable view = getView(viewId); 203 | view.setChecked(checked); 204 | return this; 205 | } 206 | 207 | @Override 208 | public EasyLVHolder setAlpha(int viewId, float value) { 209 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 210 | getView(viewId).setAlpha(value); 211 | } else { 212 | AlphaAnimation alpha = new AlphaAnimation(value, value); 213 | alpha.setDuration(0); 214 | alpha.setFillAfter(true); 215 | getView(viewId).startAnimation(alpha); 216 | } 217 | return this; 218 | } 219 | 220 | @Override 221 | public EasyLVHolder setTypeface(int viewId, Typeface typeface) { 222 | TextView view = getView(viewId); 223 | view.setTypeface(typeface); 224 | view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); 225 | return this; 226 | } 227 | 228 | @Override 229 | public EasyLVHolder setTypeface(Typeface typeface, int... viewIds) { 230 | for (int viewId : viewIds) { 231 | TextView view = getView(viewId); 232 | view.setTypeface(typeface); 233 | view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); 234 | } 235 | return this; 236 | } 237 | 238 | @Override 239 | public EasyLVHolder setOnClickListener(int viewId, View.OnClickListener listener) { 240 | View view = getView(viewId); 241 | view.setOnClickListener(listener); 242 | return this; 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/helper/DataHelper.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter.helper; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author yuyh. 7 | * @date 2016/7/21. 8 | */ 9 | public interface DataHelper { 10 | 11 | boolean addAll(List list); 12 | 13 | boolean addAll(int position, List list); 14 | 15 | void add(T data); 16 | 17 | void add(int position, T data); 18 | 19 | void clear(); 20 | 21 | boolean contains(T data); 22 | 23 | T getData(int index); 24 | 25 | void modify(T oldData, T newData); 26 | 27 | void modify(int index, T newData); 28 | 29 | boolean remove(T data); 30 | 31 | void remove(int index); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/helper/ViewHelper.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter.helper; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Typeface; 5 | import android.graphics.drawable.Drawable; 6 | import android.view.View; 7 | 8 | import com.yuyh.easyadapter.abslistview.EasyLVHolder; 9 | import com.yuyh.easyadapter.recyclerview.EasyRVHolder; 10 | 11 | /** 12 | * @author yuyh. 13 | * @date 2016/7/21. 14 | */ 15 | public interface ViewHelper { 16 | 17 | interface AbsListView { 18 | /** 19 | * 设置textView文本内容 20 | * 21 | * @param viewId viewId 22 | * @param value 文本内容 23 | * @return viewHolder viewHolder viewHolder 24 | */ 25 | VH setText(int viewId, String value); 26 | 27 | /** 28 | * 设置textView文本颜色 29 | * 30 | * @param viewId viewId 31 | * @param color 颜色数值 32 | * @return viewHolder 33 | */ 34 | VH setTextColor(int viewId, int color); 35 | 36 | /** 37 | * 设置textView文本颜色 38 | * 39 | * @param viewId viewId 40 | * @param colorRes 颜色Id 41 | * @return viewHolder 42 | */ 43 | VH setTextColorRes(int viewId, int colorRes); 44 | 45 | /** 46 | * 设置imgView的图片,通过Id设置 47 | * 48 | * @param viewId viewId 49 | * @param imgResId 图片Id 50 | * @return viewHolder viewHolder 51 | */ 52 | VH setImageResource(int viewId, int imgResId); 53 | 54 | /** 55 | * 设置背景颜色 56 | * 57 | * @param viewId viewId 58 | * @param color 颜色数值 59 | * @return viewHolder viewHolder 60 | */ 61 | VH setBackgroundColor(int viewId, int color); 62 | 63 | /** 64 | * 设置背景颜色 65 | * 66 | * @param viewId viewId 67 | * @param colorRes 颜色Id 68 | * @return viewHolder 69 | */ 70 | VH setBackgroundColorRes(int viewId, int colorRes); 71 | 72 | /** 73 | * 设置img的Drawable 74 | * 75 | * @param viewId viewId 76 | * @param drawable drawable 77 | * @return viewHolder 78 | */ 79 | VH setImageDrawable(int viewId, Drawable drawable); 80 | 81 | /** 82 | * 设置img的Drawable 83 | * 84 | * @param viewId viewId 85 | * @param drawableRes drawableId 86 | * @return viewHolder 87 | */ 88 | VH setImageDrawableRes(int viewId, int drawableRes); 89 | 90 | /** 91 | * 设置img图片路径 92 | * 93 | * @param viewId viewId 94 | * @param imgUrl 图片路径 95 | * @return viewHolder 96 | */ 97 | VH setImageUrl(int viewId, String imgUrl); 98 | 99 | /** 100 | * 设置img图片Bitmap 101 | * 102 | * @param viewId viewId 103 | * @param imgBitmap imgBitmap 104 | * @return viewHolder 105 | */ 106 | VH setImageBitmap(int viewId, Bitmap imgBitmap); 107 | 108 | /** 109 | * 设置控件是否显示 110 | * 111 | * @param viewId viewId 112 | * @param visible true(visible)/false(gone) 113 | * @return viewHolder 114 | */ 115 | VH setVisible(int viewId, boolean visible); 116 | 117 | /** 118 | * 设置控件是否显示 119 | * 120 | * @param viewId viewId 121 | * @param visible visible,invisible,gone 122 | * @return viewHolder 123 | */ 124 | VH setVisible(int viewId, int visible); 125 | 126 | /** 127 | * 设置控件的tag 128 | * 129 | * @param viewId viewId 130 | * @param tag tag 131 | * @return viewHolder 132 | */ 133 | VH setTag(int viewId, Object tag); 134 | 135 | /** 136 | * 设置控件tag 137 | * 138 | * @param viewId viewId 139 | * @param key tag的key 140 | * @param tag tag 141 | * @return viewHolder 142 | */ 143 | VH setTag(int viewId, int key, Object tag); 144 | 145 | /** 146 | * 设置Checkable控件的选择情况 147 | * 148 | * @param viewId viewId 149 | * @param checked 选择 150 | * @return viewHolder 151 | */ 152 | VH setChecked(int viewId, boolean checked); 153 | 154 | /** 155 | * 设置控件透明效果 156 | * 157 | * @param viewId viewId 158 | * @param value 透明值 159 | * @return viewHolder 160 | */ 161 | VH setAlpha(int viewId, float value); 162 | 163 | /** 164 | * 设置TextView字体 165 | * 166 | * @param viewId viewId 167 | * @param typeface typeface 168 | * @return viewHolder 169 | */ 170 | VH setTypeface(int viewId, Typeface typeface); 171 | 172 | /** 173 | * 设置多个TextView字体 174 | * 175 | * @param typeface typeface 176 | * @param viewIds viewId组合 177 | * @return viewHolder 178 | */ 179 | VH setTypeface(Typeface typeface, int... viewIds); 180 | 181 | /** 182 | * 设置监听 183 | * 184 | * @param viewId 185 | * @param listener 186 | * @return 187 | */ 188 | VH setOnClickListener(int viewId, View.OnClickListener listener); 189 | } 190 | 191 | interface RecyclerView { 192 | /** 193 | * 设置textView文本内容 194 | * 195 | * @param viewId viewId 196 | * @param value 文本内容 197 | * @return viewHolder viewHolder viewHolder 198 | */ 199 | VH setText(int viewId, String value); 200 | 201 | /** 202 | * 设置textView文本颜色 203 | * 204 | * @param viewId viewId 205 | * @param color 颜色数值 206 | * @return viewHolder 207 | */ 208 | VH setTextColor(int viewId, int color); 209 | 210 | /** 211 | * 设置textView文本颜色 212 | * 213 | * @param viewId viewId 214 | * @param colorRes 颜色Id 215 | * @return viewHolder 216 | */ 217 | VH setTextColorRes(int viewId, int colorRes); 218 | 219 | /** 220 | * 设置imgView的图片,通过Id设置 221 | * 222 | * @param viewId viewId 223 | * @param imgResId 图片Id 224 | * @return viewHolder viewHolder 225 | */ 226 | VH setImageResource(int viewId, int imgResId); 227 | 228 | /** 229 | * 设置背景颜色 230 | * 231 | * @param viewId viewId 232 | * @param color 颜色数值 233 | * @return viewHolder viewHolder 234 | */ 235 | VH setBackgroundColor(int viewId, int color); 236 | 237 | /** 238 | * 设置背景颜色 239 | * 240 | * @param viewId viewId 241 | * @param colorRes 颜色Id 242 | * @return viewHolder 243 | */ 244 | VH setBackgroundColorRes(int viewId, int colorRes); 245 | 246 | /** 247 | * 设置img的Drawable 248 | * 249 | * @param viewId viewId 250 | * @param drawable drawable 251 | * @return viewHolder 252 | */ 253 | VH setImageDrawable(int viewId, Drawable drawable); 254 | 255 | /** 256 | * 设置img的Drawable 257 | * 258 | * @param viewId viewId 259 | * @param drawableRes drawableId 260 | * @return viewHolder 261 | */ 262 | VH setImageDrawableRes(int viewId, int drawableRes); 263 | 264 | /** 265 | * 设置img图片路径 266 | * 267 | * @param viewId viewId 268 | * @param imgUrl 图片路径 269 | * @return viewHolder 270 | */ 271 | VH setImageUrl(int viewId, String imgUrl); 272 | 273 | /** 274 | * 设置img图片Bitmap 275 | * 276 | * @param viewId viewId 277 | * @param imgBitmap imgBitmap 278 | * @return viewHolder 279 | */ 280 | VH setImageBitmap(int viewId, Bitmap imgBitmap); 281 | 282 | /** 283 | * 设置控件是否显示 284 | * 285 | * @param viewId viewId 286 | * @param visible true(visible)/false(gone) 287 | * @return viewHolder 288 | */ 289 | VH setVisible(int viewId, boolean visible); 290 | 291 | /** 292 | * 设置控件是否显示 293 | * 294 | * @param viewId viewId 295 | * @param visible visible,invisible,gone 296 | * @return viewHolder 297 | */ 298 | VH setVisible(int viewId, int visible); 299 | 300 | /** 301 | * 设置控件的tag 302 | * 303 | * @param viewId viewId 304 | * @param tag tag 305 | * @return viewHolder 306 | */ 307 | VH setTag(int viewId, Object tag); 308 | 309 | /** 310 | * 设置控件tag 311 | * 312 | * @param viewId viewId 313 | * @param key tag的key 314 | * @param tag tag 315 | * @return viewHolder 316 | */ 317 | VH setTag(int viewId, int key, Object tag); 318 | 319 | /** 320 | * 设置Checkable控件的选择情况 321 | * 322 | * @param viewId viewId 323 | * @param checked 选择 324 | * @return viewHolder 325 | */ 326 | VH setChecked(int viewId, boolean checked); 327 | 328 | /** 329 | * 设置控件透明效果 330 | * 331 | * @param viewId viewId 332 | * @param value 透明值 333 | * @return viewHolder 334 | */ 335 | VH setAlpha(int viewId, float value); 336 | 337 | /** 338 | * 设置TextView字体 339 | * 340 | * @param viewId viewId 341 | * @param typeface typeface 342 | * @return viewHolder 343 | */ 344 | VH setTypeface(int viewId, Typeface typeface); 345 | 346 | /** 347 | * 设置多个TextView字体 348 | * 349 | * @param typeface typeface 350 | * @param viewIds viewId组合 351 | * @return viewHolder 352 | */ 353 | VH setTypeface(Typeface typeface, int... viewIds); 354 | 355 | /** 356 | * 设置监听 357 | * 358 | * @param viewId 359 | * @param listener 360 | * @return 361 | */ 362 | VH setOnClickListener(int viewId, View.OnClickListener listener); 363 | } 364 | 365 | } 366 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/recyclerview/EasyRVAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter.recyclerview; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.GridLayoutManager; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.support.v7.widget.StaggeredGridLayoutManager; 7 | import android.util.SparseArray; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | import com.yuyh.easyadapter.AdapterImageLoader; 13 | import com.yuyh.easyadapter.helper.DataHelper; 14 | import com.yuyh.library.easyadapter.R; 15 | 16 | import java.util.List; 17 | 18 | /** 19 | * @author yuyh. 20 | * @date 2016/7/21. 21 | */ 22 | public abstract class EasyRVAdapter extends RecyclerView.Adapter implements DataHelper { 23 | 24 | /**** 25 | * 头部相关 26 | */ 27 | public static final int TYPE_HEADER = -1, TYPE_FOOTER = -2; 28 | private View mHeaderView, mFooterView; 29 | private int headerViewId = -1, footerViewId = -2; 30 | 31 | protected Context mContext; 32 | protected List mList; 33 | protected int[] layoutIds; 34 | protected LayoutInflater mLInflater; 35 | 36 | private SparseArray mConvertViews = new SparseArray<>(); 37 | 38 | private OnItemClickListener itemClickListener; 39 | private OnItemLongClickListener itemLongClickListener; 40 | 41 | public EasyRVAdapter(Context context, List list, int... layoutIds) { 42 | this.mContext = context; 43 | this.mList = list; 44 | this.layoutIds = layoutIds; 45 | this.mLInflater = LayoutInflater.from(mContext); 46 | } 47 | 48 | @Override 49 | public EasyRVHolder onCreateViewHolder(ViewGroup parent, int viewType) { 50 | if (mHeaderView != null && viewType == TYPE_HEADER) { 51 | return new EasyRVHolder(mContext, headerViewId, mHeaderView, getImageLoader()); 52 | } 53 | if (mFooterView != null && viewType == TYPE_FOOTER) { 54 | return new EasyRVHolder(mContext, footerViewId, mFooterView, getImageLoader()); 55 | } 56 | if (viewType < 0 || viewType > layoutIds.length) { 57 | throw new ArrayIndexOutOfBoundsException("layoutIndex"); 58 | } 59 | if (layoutIds.length == 0) { 60 | throw new IllegalArgumentException("not layoutId"); 61 | } 62 | int layoutId = layoutIds[viewType]; 63 | View view = mConvertViews.get(layoutId); 64 | if (view == null) { 65 | view = mLInflater.inflate(layoutId, parent, false); 66 | } 67 | EasyRVHolder viewHolder = (EasyRVHolder) view.getTag(); 68 | if (viewHolder == null || viewHolder.getLayoutId() != layoutId) { 69 | viewHolder = new EasyRVHolder(mContext, layoutId, view, getImageLoader()); 70 | return viewHolder; 71 | } 72 | return viewHolder; 73 | } 74 | 75 | @Override 76 | public void onBindViewHolder(EasyRVHolder holder, int position) { 77 | if (getItemViewType(position) == TYPE_HEADER) 78 | return; 79 | if (getItemViewType(position) == TYPE_FOOTER) 80 | return; 81 | 82 | position = getPosition(position); 83 | final T item = mList.get(position); 84 | 85 | holder.getItemView().setTag(R.id.tag_position, position); 86 | holder.getItemView().setTag(R.id.tag_item, item); 87 | 88 | holder.getItemView().setOnClickListener(clickListener); 89 | holder.getItemView().setOnLongClickListener(longClickListener); 90 | 91 | onBindData(holder, position, item); 92 | } 93 | 94 | @Override 95 | public int getItemCount() { 96 | if (mHeaderView == null && mFooterView == null) { 97 | return mList == null ? 0 : mList.size(); 98 | } else if (mHeaderView != null && mFooterView != null) { 99 | return mList == null ? 2 : mList.size() + 2; 100 | } else { 101 | return mList == null ? 1 : mList.size() + 1; 102 | } 103 | } 104 | 105 | @Override 106 | public int getItemViewType(int position) { 107 | if (position == 0 && mHeaderView != null) { 108 | return TYPE_HEADER; 109 | } 110 | if (position == getItemCount() - 1 && mFooterView != null) { 111 | return TYPE_FOOTER; 112 | } 113 | position = getPosition(position); 114 | return getLayoutIndex(position, mList.get(position)); 115 | } 116 | 117 | /***** 118 | * 处理 GridLayoutManager 119 | * @param recyclerView 120 | */ 121 | @Override 122 | public void onAttachedToRecyclerView(RecyclerView recyclerView) { 123 | super.onAttachedToRecyclerView(recyclerView); 124 | RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); 125 | if (manager instanceof GridLayoutManager) { 126 | final GridLayoutManager gridManager = ((GridLayoutManager) manager); 127 | gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 128 | @Override 129 | public int getSpanSize(int position) { 130 | return getItemViewType(position) == TYPE_HEADER || getItemViewType(position) == TYPE_FOOTER ? gridManager.getSpanCount() : 1; 131 | } 132 | }); 133 | } 134 | } 135 | 136 | /***** 137 | * 处理 StaggeredGridLayoutManager 138 | * @param holder 139 | */ 140 | @Override 141 | public void onViewAttachedToWindow(EasyRVHolder holder) { 142 | super.onViewAttachedToWindow(holder); 143 | ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); 144 | if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) { 145 | StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; 146 | p.setFullSpan(holder.getLayoutPosition() == 0); 147 | } 148 | } 149 | 150 | private int getPosition(int position) { 151 | if (mHeaderView != null) { 152 | position = position - 1; 153 | } 154 | return position; 155 | } 156 | 157 | /** 158 | * 指定item布局样式在layoutIds的索引。默认为第一个 159 | * 160 | * @param position 161 | * @param item 162 | * @return 163 | */ 164 | public int getLayoutIndex(int position, T item) { 165 | return 0; 166 | } 167 | 168 | /** 169 | * 指定网络图片加载器,不指定则默认用全局注册的 170 | * 171 | * @return 172 | */ 173 | public AdapterImageLoader.ImageLoader getImageLoader() { 174 | return null; 175 | } 176 | 177 | /**** 178 | * 设置头部 179 | * @param headerViewId 180 | */ 181 | public View setHeaderView(int headerViewId) { 182 | return setHeaderView(headerViewId, null); 183 | } 184 | 185 | public View setHeaderView(int headerViewId, ViewGroup parent) { 186 | mHeaderView = mLInflater.inflate(headerViewId, parent, false); 187 | this.headerViewId = headerViewId; 188 | notifyItemInserted(0); 189 | return mHeaderView; 190 | } 191 | 192 | public void removeHeaderView() { 193 | if (mHeaderView != null) { 194 | mHeaderView = null; 195 | this.headerViewId = -1; 196 | notifyItemRemoved(0); 197 | } 198 | } 199 | 200 | public View setFooterView(int footerViewId) { 201 | return setFooterView(footerViewId, null); 202 | } 203 | 204 | public View setFooterView(int footerViewId, ViewGroup parent) { 205 | mFooterView = mLInflater.inflate(footerViewId, parent, false); 206 | this.footerViewId = footerViewId; 207 | notifyItemInserted(mList.size()); 208 | return mFooterView; 209 | } 210 | 211 | public void removeFooterView() { 212 | if (mFooterView != null) { 213 | mFooterView = null; 214 | this.footerViewId = -2; 215 | notifyItemRemoved(mList.size() - 1); 216 | } 217 | } 218 | 219 | /**** 220 | * 获取头部 221 | * @return 222 | */ 223 | public View getHeaderView() { 224 | return mHeaderView; 225 | } 226 | 227 | public View getFooterView() { 228 | return mFooterView; 229 | } 230 | 231 | protected abstract void onBindData(EasyRVHolder viewHolder, int position, T item); 232 | 233 | @Override 234 | public boolean addAll(List list) { 235 | boolean result = mList.addAll(list); 236 | notifyDataSetChanged(); 237 | return result; 238 | } 239 | 240 | @Override 241 | public boolean addAll(int position, List list) { 242 | boolean result = mList.addAll(position, list); 243 | notifyDataSetChanged(); 244 | return result; 245 | } 246 | 247 | @Override 248 | public void add(T data) { 249 | mList.add(data); 250 | notifyDataSetChanged(); 251 | } 252 | 253 | @Override 254 | public void add(int position, T data) { 255 | mList.add(position, data); 256 | notifyDataSetChanged(); 257 | } 258 | 259 | @Override 260 | public void clear() { 261 | mList.clear(); 262 | notifyDataSetChanged(); 263 | } 264 | 265 | @Override 266 | public boolean contains(T data) { 267 | return mList.contains(data); 268 | } 269 | 270 | @Override 271 | public T getData(int index) { 272 | return mList.get(index); 273 | } 274 | 275 | @Override 276 | public void modify(T oldData, T newData) { 277 | modify(mList.indexOf(oldData), newData); 278 | } 279 | 280 | @Override 281 | public void modify(int index, T newData) { 282 | mList.set(index, newData); 283 | notifyDataSetChanged(); 284 | } 285 | 286 | @Override 287 | public boolean remove(T data) { 288 | boolean result = mList.remove(data); 289 | notifyDataSetChanged(); 290 | return result; 291 | } 292 | 293 | @Override 294 | public void remove(int index) { 295 | mList.remove(index); 296 | notifyDataSetChanged(); 297 | } 298 | 299 | private View.OnClickListener clickListener = new View.OnClickListener() { 300 | @Override 301 | public void onClick(View v) { 302 | int position = (int) v.getTag(R.id.tag_position); 303 | T item = (T) v.getTag(R.id.tag_item); 304 | 305 | if (itemClickListener != null) { 306 | itemClickListener.onItemClick(v, position, item); 307 | } 308 | } 309 | }; 310 | 311 | private View.OnLongClickListener longClickListener = new View.OnLongClickListener() { 312 | @Override 313 | public boolean onLongClick(View v) { 314 | 315 | int position = (int) v.getTag(R.id.tag_position); 316 | T item = (T) v.getTag(R.id.tag_item); 317 | 318 | if (itemLongClickListener != null) { 319 | itemLongClickListener.onItemLongClick(v, position, item); 320 | } 321 | 322 | return true; 323 | } 324 | }; 325 | 326 | public void setOnItemClickListener(OnItemClickListener itemClickListener) { 327 | this.itemClickListener = itemClickListener; 328 | } 329 | 330 | public void setOnItemLongClickListener(OnItemLongClickListener itemLongClickListener) { 331 | this.itemLongClickListener = itemLongClickListener; 332 | } 333 | 334 | /**** 335 | * RecyclerView Item 的点击事件 336 | * @param 337 | */ 338 | public interface OnItemClickListener { 339 | void onItemClick(View view, int position, T item); 340 | } 341 | 342 | /**** 343 | * RecyclerView Item 的长按事件 344 | * @param 345 | */ 346 | public interface OnItemLongClickListener { 347 | void onItemLongClick(View view, int position, T item); 348 | } 349 | } 350 | -------------------------------------------------------------------------------- /library/src/main/java/com/yuyh/easyadapter/recyclerview/EasyRVHolder.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter.recyclerview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.graphics.drawable.Drawable; 8 | import android.os.Build; 9 | import android.support.v4.content.ContextCompat; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.SparseArray; 12 | import android.view.View; 13 | import android.view.animation.AlphaAnimation; 14 | import android.widget.Checkable; 15 | import android.widget.ImageView; 16 | import android.widget.TextView; 17 | 18 | import com.yuyh.easyadapter.AdapterImageLoader; 19 | import com.yuyh.easyadapter.helper.ViewHelper; 20 | 21 | public class EasyRVHolder extends RecyclerView.ViewHolder implements ViewHelper.RecyclerView { 22 | 23 | private SparseArray mViews = new SparseArray<>(); 24 | 25 | private View mConvertView; 26 | private int mLayoutId; 27 | protected Context mContext; 28 | 29 | private AdapterImageLoader.ImageLoader mImageLoader; 30 | 31 | public EasyRVHolder(Context context, int layoutId, View itemView, AdapterImageLoader.ImageLoader imageLoader) { 32 | super(itemView); 33 | this.mContext = context; 34 | this.mLayoutId = layoutId; 35 | this.mImageLoader = imageLoader; 36 | mConvertView = itemView; 37 | mConvertView.setTag(this); 38 | } 39 | 40 | public V getView(int viewId) { 41 | View view = mViews.get(viewId); 42 | if (view == null) { 43 | view = mConvertView.findViewById(viewId); 44 | mViews.put(viewId, view); 45 | } 46 | return (V) view; 47 | } 48 | 49 | public int getLayoutId() { 50 | return mLayoutId; 51 | } 52 | 53 | /** 54 | * 获取item布局 55 | * 56 | * @return 57 | */ 58 | public View getItemView() { 59 | return mConvertView; 60 | } 61 | 62 | public EasyRVHolder setOnItemViewClickListener(View.OnClickListener listener) { 63 | mConvertView.setOnClickListener(listener); 64 | return this; 65 | } 66 | 67 | public EasyRVHolder setOnItemViewLongClickListener(View.OnLongClickListener listener) { 68 | mConvertView.setOnLongClickListener(listener); 69 | return this; 70 | } 71 | 72 | @Override 73 | public EasyRVHolder setText(int viewId, String value) { 74 | TextView view = getView(viewId); 75 | view.setText(value); 76 | return this; 77 | } 78 | 79 | @Override 80 | public EasyRVHolder setTextColor(int viewId, int color) { 81 | TextView view = getView(viewId); 82 | view.setTextColor(color); 83 | return this; 84 | } 85 | 86 | @Override 87 | public EasyRVHolder setTextColorRes(int viewId, int colorRes) { 88 | TextView view = getView(viewId); 89 | view.setTextColor(ContextCompat.getColor(mContext, colorRes)); 90 | return this; 91 | } 92 | 93 | @Override 94 | public EasyRVHolder setImageResource(int viewId, int imgResId) { 95 | ImageView view = getView(viewId); 96 | view.setImageResource(imgResId); 97 | return this; 98 | } 99 | 100 | @Override 101 | public EasyRVHolder setBackgroundColor(int viewId, int color) { 102 | View view = getView(viewId); 103 | view.setBackgroundColor(color); 104 | return this; 105 | } 106 | 107 | @Override 108 | public EasyRVHolder setBackgroundColorRes(int viewId, int colorRes) { 109 | View view = getView(viewId); 110 | view.setBackgroundResource(colorRes); 111 | return this; 112 | } 113 | 114 | @Override 115 | public EasyRVHolder setImageDrawable(int viewId, Drawable drawable) { 116 | ImageView view = getView(viewId); 117 | view.setImageDrawable(drawable); 118 | return this; 119 | } 120 | 121 | @Override 122 | public EasyRVHolder setImageDrawableRes(int viewId, int drawableRes) { 123 | Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes); 124 | return setImageDrawable(viewId, drawable); 125 | } 126 | 127 | @Override 128 | public EasyRVHolder setImageUrl(int viewId, String imgUrl) { 129 | ImageView imageView = getView(viewId); 130 | if (mImageLoader != null) { 131 | mImageLoader.loadImage(mContext, imgUrl, imageView); 132 | } else if (AdapterImageLoader.sImageLoader != null) { 133 | AdapterImageLoader.sImageLoader.loadImage(mContext, imgUrl, imageView); 134 | } 135 | return this; 136 | } 137 | 138 | @Override 139 | public EasyRVHolder setImageBitmap(int viewId, Bitmap imgBitmap) { 140 | ImageView view = getView(viewId); 141 | view.setImageBitmap(imgBitmap); 142 | return this; 143 | } 144 | 145 | @Override 146 | public EasyRVHolder setVisible(int viewId, boolean visible) { 147 | View view = getView(viewId); 148 | view.setVisibility(visible ? View.VISIBLE : View.GONE); 149 | return this; 150 | } 151 | 152 | @Override 153 | public EasyRVHolder setVisible(int viewId, int visible) { 154 | View view = getView(viewId); 155 | view.setVisibility(visible); 156 | return this; 157 | } 158 | 159 | @Override 160 | public EasyRVHolder setTag(int viewId, Object tag) { 161 | View view = getView(viewId); 162 | view.setTag(tag); 163 | return this; 164 | } 165 | 166 | @Override 167 | public EasyRVHolder setTag(int viewId, int key, Object tag) { 168 | View view = getView(viewId); 169 | view.setTag(key, tag); 170 | return this; 171 | } 172 | 173 | @Override 174 | public EasyRVHolder setChecked(int viewId, boolean checked) { 175 | Checkable view = getView(viewId); 176 | view.setChecked(checked); 177 | return this; 178 | } 179 | 180 | @Override 181 | public EasyRVHolder setAlpha(int viewId, float value) { 182 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 183 | getView(viewId).setAlpha(value); 184 | } else { 185 | AlphaAnimation alpha = new AlphaAnimation(value, value); 186 | alpha.setDuration(0); 187 | alpha.setFillAfter(true); 188 | getView(viewId).startAnimation(alpha); 189 | } 190 | return this; 191 | } 192 | 193 | @Override 194 | public EasyRVHolder setTypeface(int viewId, Typeface typeface) { 195 | TextView view = getView(viewId); 196 | view.setTypeface(typeface); 197 | view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); 198 | return this; 199 | } 200 | 201 | @Override 202 | public EasyRVHolder setTypeface(Typeface typeface, int... viewIds) { 203 | for (int viewId : viewIds) { 204 | TextView view = getView(viewId); 205 | view.setTypeface(typeface); 206 | view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); 207 | } 208 | return this; 209 | } 210 | 211 | @Override 212 | public EasyRVHolder setOnClickListener(int viewId, View.OnClickListener listener) { 213 | View view = getView(viewId); 214 | view.setOnClickListener(listener); 215 | return this; 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /library/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | applicationId "com.yuyh.easyadapter" 8 | minSdkVersion 15 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(include: ['*.jar'], dir: 'libs') 23 | compile 'com.android.support:appcompat-v7:27.1.1' 24 | compile 'com.android.support:recyclerview-v7:27.1.1' 25 | compile 'com.github.bumptech.glide:glide:3.7.0' 26 | //compile 'com.yuyh.easyadapter:library:1.1.4' 27 | implementation 'com.github.smuyyh:EasyAdapter:1.3.0' 28 | } 29 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/yuyuhang/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/App.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.widget.ImageView; 6 | 7 | import com.bumptech.glide.Glide; 8 | 9 | /** 10 | * Created by yuyuhang on 2018/6/14. 11 | */ 12 | public class App extends Application { 13 | 14 | @Override 15 | public void onCreate() { 16 | super.onCreate(); 17 | 18 | // 若需要调用adapter.setImageUrl,可以配置全局统一回调,也可Adapter单独重写getImageLoader方法 19 | AdapterImageLoader.init(new AdapterImageLoader.ImageLoader() { 20 | @Override 21 | public void loadImage(Context context, String url, ImageView view) { 22 | Glide.with(context) 23 | .load(url) 24 | .into(view); 25 | } 26 | }); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/Bean.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | /** 4 | * @author yuyh. 5 | * @date 2016/7/21. 6 | */ 7 | public class Bean { 8 | 9 | public int imgRes; 10 | 11 | public String name; 12 | 13 | public Bean(int imgRes, String name) { 14 | this.imgRes = imgRes; 15 | this.name = name; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/DividerItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Drawable; 8 | import android.support.v7.widget.LinearLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.view.View; 11 | 12 | /** 13 | * Created by xiaoshu on 2016/7/21. 14 | */ 15 | public class DividerItemDecoration extends RecyclerView.ItemDecoration { 16 | private static final int[] ATTRS = new int[]{ 17 | android.R.attr.listDivider 18 | }; 19 | 20 | public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 21 | 22 | public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 23 | 24 | private Drawable mDivider; 25 | private int mOrientation; 26 | 27 | public DividerItemDecoration(Context context, int orientation) { 28 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 29 | mDivider = a.getDrawable(0); 30 | a.recycle(); 31 | setOrientation(orientation); 32 | } 33 | 34 | public void setOrientation(int orientation) { 35 | if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { 36 | throw new IllegalArgumentException("invalid orientation"); 37 | } 38 | mOrientation = orientation; 39 | } 40 | 41 | @Override 42 | public void onDraw(Canvas c, RecyclerView parent) { 43 | 44 | if (mOrientation == VERTICAL_LIST) { 45 | drawVertical(c, parent); 46 | } else { 47 | drawHorizontal(c, parent); 48 | } 49 | 50 | } 51 | 52 | 53 | public void drawVertical(Canvas c, RecyclerView parent) { 54 | final int left = parent.getPaddingLeft(); 55 | final int right = parent.getWidth() - parent.getPaddingRight(); 56 | 57 | final int childCount = parent.getChildCount(); 58 | for (int i = 0; i < childCount; i++) { 59 | final View child = parent.getChildAt(i); 60 | android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext()); 61 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 62 | final int top = child.getBottom() + params.bottomMargin; 63 | final int bottom = top + mDivider.getIntrinsicHeight(); 64 | mDivider.setBounds(left, top, right, bottom); 65 | mDivider.draw(c); 66 | } 67 | } 68 | 69 | public void drawHorizontal(Canvas c, RecyclerView parent) { 70 | final int top = parent.getPaddingTop(); 71 | final int bottom = parent.getHeight() - parent.getPaddingBottom(); 72 | 73 | final int childCount = parent.getChildCount(); 74 | for (int i = 0; i < childCount; i++) { 75 | final View child = parent.getChildAt(i); 76 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 77 | final int left = child.getRight() + params.rightMargin; 78 | final int right = left + mDivider.getIntrinsicHeight(); 79 | mDivider.setBounds(left, top, right, bottom); 80 | mDivider.draw(c); 81 | } 82 | } 83 | 84 | @Override 85 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 86 | if (mOrientation == VERTICAL_LIST){ 87 | outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); 88 | }else { 89 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/GridItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Drawable; 8 | import android.support.v7.widget.GridLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.support.v7.widget.StaggeredGridLayoutManager; 11 | import android.view.View; 12 | 13 | /** 14 | * Created by xiaob on 2016/10/22. 15 | */ 16 | 17 | public class GridItemDecoration extends RecyclerView.ItemDecoration { 18 | private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 19 | private Drawable mDivider; 20 | private boolean hasHeader, hasFooter; 21 | 22 | public GridItemDecoration(Context context) { 23 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 24 | mDivider = a.getDrawable(0); 25 | a.recycle(); 26 | } 27 | 28 | public GridItemDecoration(Context context, boolean hasHeader, boolean hasFooter) { 29 | this(context); 30 | this.hasHeader = hasHeader; 31 | this.hasFooter = hasFooter; 32 | } 33 | 34 | @Override 35 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 36 | 37 | drawHorizontal(c, parent); 38 | drawVertical(c, parent); 39 | 40 | } 41 | 42 | private int getSpanCount(RecyclerView parent) { 43 | // 列数 44 | int spanCount = -1; 45 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 46 | if (layoutManager instanceof GridLayoutManager) { 47 | 48 | spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); 49 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 50 | spanCount = ((StaggeredGridLayoutManager) layoutManager) 51 | .getSpanCount(); 52 | } 53 | return spanCount; 54 | } 55 | 56 | public void drawHorizontal(Canvas c, RecyclerView parent) { 57 | int childCount = parent.getChildCount(); 58 | for (int i = 0; i < childCount; i++) { 59 | final View child = parent.getChildAt(i); 60 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 61 | final int left = child.getLeft() - params.leftMargin; 62 | final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth(); 63 | final int top = child.getBottom() + params.bottomMargin; 64 | final int bottom = top + mDivider.getIntrinsicHeight(); 65 | mDivider.setBounds(left, top, right, bottom); 66 | mDivider.draw(c); 67 | } 68 | } 69 | 70 | public void drawVertical(Canvas c, RecyclerView parent) { 71 | final int childCount = parent.getChildCount(); 72 | for (int i = 0; i < childCount; i++) { 73 | final View child = parent.getChildAt(i); 74 | 75 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 76 | final int top = child.getTop() - params.topMargin; 77 | final int bottom = child.getBottom() + params.bottomMargin; 78 | final int left = child.getRight() + params.rightMargin; 79 | final int right = left + mDivider.getIntrinsicWidth(); 80 | 81 | mDivider.setBounds(left, top, right, bottom); 82 | mDivider.draw(c); 83 | } 84 | } 85 | 86 | private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) { 87 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 88 | if (layoutManager instanceof GridLayoutManager) { 89 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 90 | { 91 | return true; 92 | } 93 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 94 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 95 | .getOrientation(); 96 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 97 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 98 | { 99 | return true; 100 | } 101 | } else { 102 | childCount = childCount - childCount % spanCount; 103 | if (pos >= childCount)// 如果是最后一列,则不需要绘制右边 104 | return true; 105 | } 106 | } 107 | return false; 108 | } 109 | 110 | @Override 111 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 112 | int position = parent.getChildAdapterPosition(view); 113 | 114 | int spanCount = getSpanCount(parent); 115 | int childCount = parent.getAdapter().getItemCount(); 116 | 117 | int pos = position; 118 | 119 | if(hasHeader || hasFooter) { 120 | if(position == 0 && hasHeader) { 121 | outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); 122 | return; 123 | } 124 | if(position == state.getItemCount()-1 && hasFooter){ 125 | outRect.set(0, 0, 0, 0); 126 | return; 127 | }else { 128 | pos = position - 1; 129 | } 130 | } 131 | 132 | if (isLastColum(parent, pos, spanCount, childCount)) { 133 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); 134 | } else { 135 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/ListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.widget.ListView; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | public class ListViewActivity extends AppCompatActivity { 11 | 12 | private ListView lv; 13 | 14 | private List list = Arrays.asList(new Bean(R.mipmap.ic_launcher, "测试1"), 15 | new Bean(R.mipmap.ic_launcher, "测试2"), 16 | new Bean(R.mipmap.ic_launcher, "测试3"), 17 | new Bean(R.mipmap.ic_launcher, "测试4"), 18 | new Bean(R.mipmap.ic_launcher, "测试5"), 19 | new Bean(R.mipmap.ic_launcher, "测试6"), 20 | new Bean(R.mipmap.ic_launcher, "测试7")); 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_list_view); 26 | lv = (ListView) findViewById(R.id.lv); 27 | lv.setAdapter(new ListViewAdapter(this, list, R.layout.item_list_view_1, R.layout.item_list_view_2)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/ListViewAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.content.Context; 4 | 5 | import com.yuyh.easyadapter.abslistview.EasyLVAdapter; 6 | import com.yuyh.easyadapter.abslistview.EasyLVHolder; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author yuyh. 12 | * @date 2016/7/21. 13 | */ 14 | public class ListViewAdapter extends EasyLVAdapter { 15 | 16 | public ListViewAdapter(Context context, List list, int... layoutIds) { 17 | super(context, list, layoutIds); 18 | } 19 | 20 | @Override 21 | public void convert(EasyLVHolder holder, int position, final Bean bean) { 22 | holder.setText(R.id.tv, bean.name); 23 | holder.setImageUrl(R.id.iv, "https://img.25pp.com//ppnews/zixun_img/6fc/096/1444271700253231.jpg"); 24 | } 25 | 26 | @Override 27 | public int getLayoutIndex(int position, Bean item) { 28 | if (position % 2 == 0) 29 | return 1; 30 | else return 0; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | } 15 | 16 | public void listView(View view) { 17 | startActivity(new Intent(this, ListViewActivity.class)); 18 | } 19 | 20 | public void recyclerView(View view) { 21 | startActivity(new Intent(this, RecyclerViewActivity.class)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/RecyclerViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.DefaultItemAnimator; 6 | import android.support.v7.widget.GridLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.yuyh.easyadapter.recyclerview.EasyRVAdapter; 13 | 14 | import java.util.Arrays; 15 | import java.util.List; 16 | 17 | public class RecyclerViewActivity extends AppCompatActivity { 18 | 19 | private RecyclerView rv; 20 | private RecyclerViewAdapter mAdapter; 21 | private List list = Arrays.asList(new Bean(R.mipmap.ic_launcher, "测试1"), 22 | new Bean(R.mipmap.ic_launcher, "测试2"), 23 | new Bean(R.mipmap.ic_launcher, "测试3"), 24 | new Bean(R.mipmap.ic_launcher, "测试4"), 25 | new Bean(R.mipmap.ic_launcher, "测试5"), 26 | new Bean(R.mipmap.ic_launcher, "测试6"), 27 | new Bean(R.mipmap.ic_launcher, "测试7"), 28 | new Bean(R.mipmap.ic_launcher, "测试8"), 29 | new Bean(R.mipmap.ic_launcher, "测试9"), 30 | new Bean(R.mipmap.ic_launcher, "测试10"), 31 | new Bean(R.mipmap.ic_launcher, "测试11"), 32 | new Bean(R.mipmap.ic_launcher, "测试12"), 33 | new Bean(R.mipmap.ic_launcher, "测试13"), 34 | new Bean(R.mipmap.ic_launcher, "测试14"), 35 | new Bean(R.mipmap.ic_launcher, "测试15"), 36 | new Bean(R.mipmap.ic_launcher, "测试16")); 37 | 38 | @Override 39 | protected void onCreate(Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | setContentView(R.layout.activity_recycler_view); 42 | rv = (RecyclerView) findViewById(R.id.rv); 43 | rv.setLayoutManager(new GridLayoutManager(this, 3)); 44 | mAdapter = new RecyclerViewAdapter(this, list, R.layout.item_list_view_1, R.layout.item_list_view_2); 45 | rv.setAdapter(mAdapter); 46 | View mHeader = mAdapter.setHeaderView(R.layout.item_header);//此方法需要在 setLayoutManager 之前调用,否则会找不到LayoutManager,从而无法处理 GridLayoutManager 此方法会返回View 47 | TextView tvHeaderName = (TextView) mHeader.findViewById(R.id.tv_name); 48 | tvHeaderName.setText("这是头部"); 49 | View mFooter = mAdapter.setFooterView(R.layout.item_header); 50 | TextView tvFooterName = (TextView) mFooter.findViewById(R.id.tv_name); 51 | tvFooterName.setText("这是尾部"); 52 | mAdapter.setOnItemClickListener(new EasyRVAdapter.OnItemClickListener() { 53 | @Override 54 | public void onItemClick(View v, int position, Bean item) { 55 | Toast.makeText(RecyclerViewActivity.this, "position是:" + position + "-->" + "点击了:"+item.name, Toast.LENGTH_LONG).show(); 56 | } 57 | }); 58 | rv.setItemAnimator(new DefaultItemAnimator()); 59 | rv.addItemDecoration(new GridItemDecoration(this, true, true)); 60 | rv.setHasFixedSize(true); 61 | 62 | //mAdapter.setHeaderView(R.layout.item_header, rv); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /sample/src/main/java/com/yuyh/easyadapter/RecyclerViewAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yuyh.easyadapter; 2 | 3 | import android.content.Context; 4 | import android.widget.ImageView; 5 | 6 | import com.bumptech.glide.Glide; 7 | import com.yuyh.easyadapter.recyclerview.EasyRVAdapter; 8 | import com.yuyh.easyadapter.recyclerview.EasyRVHolder; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @author yuyh. 14 | * @date 2016/7/21. 15 | */ 16 | public class RecyclerViewAdapter extends EasyRVAdapter { 17 | public RecyclerViewAdapter(Context context, List list, int... layoutIds) { 18 | super(context, list, layoutIds); 19 | } 20 | 21 | @Override 22 | protected void onBindData(EasyRVHolder viewHolder, final int position, Bean item) { 23 | viewHolder.setText(R.id.tv, item.name); 24 | // 自定义点击可覆盖父类的setOnItemClickListener 25 | /*viewHolder.getItemView().setOnClickListener(new View.OnClickListener() { 26 | @Override 27 | public void onClick(View v) { 28 | Toast.makeText(mContext, position+"---", Toast.LENGTH_SHORT).show(); 29 | } 30 | });*/ 31 | viewHolder.setImageUrl(R.id.iv, "https://img.25pp.com//ppnews/zixun_img/6fc/096/1444271700253231.jpg"); 32 | } 33 | 34 | @Override 35 | public int getLayoutIndex(int position, Bean item) { 36 | if (position % 2 == 0) 37 | return 0; 38 | else return 1; 39 | } 40 | 41 | @Override 42 | public AdapterImageLoader.ImageLoader getImageLoader() { 43 | // 重写该方法,以替换全局初始化的ImageLoader 44 | return new AdapterImageLoader.ImageLoader() { 45 | @Override 46 | public void loadImage(Context context, String url, ImageView view) { 47 | Glide.with(context) 48 | .load(url) 49 | .override(10, 10) 50 | .into(view); 51 | } 52 | }; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_list_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 |