├── .gitignore ├── .idea ├── checkstyle-idea.xml ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── addsubutils ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jmf │ │ └── addsubutils │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── jmf │ │ │ └── addsubutils │ │ │ └── AddSubUtils.java │ └── res │ │ ├── drawable-xxhdpi │ │ ├── addsubutils_ic_minus.png │ │ └── addsubutils_ic_plus.png │ │ ├── drawable │ │ ├── addsubutils_add_sub_bg.xml │ │ ├── addsubutils_left_selector.xml │ │ ├── addsubutils_right_selector.xml │ │ └── divider_horizontal.xml │ │ ├── layout │ │ ├── add_sub_end_layout.xml │ │ ├── add_sub_layout.xml │ │ └── add_sub_start_layout.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── color.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── jmf │ └── addsubutils │ └── ExampleUnitTest.java ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jmf │ │ └── addsubutilstest │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── jmf │ │ │ └── addsubutilstest │ │ │ ├── ListViewActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── MyRecyclerAdapter.java │ │ │ ├── RecyclerActivity.java │ │ │ └── bean │ │ │ └── AddSubBean.java │ └── res │ │ ├── drawable-hdpi │ │ ├── book_1.jpg │ │ ├── book_10.jpg │ │ ├── book_11.jpg │ │ ├── book_2.jpg │ │ ├── book_3.jpg │ │ ├── book_4.jpg │ │ ├── book_5.jpg │ │ ├── book_6.jpg │ │ ├── book_7.jpg │ │ ├── book_8.jpg │ │ └── book_9.jpg │ │ ├── drawable-xxhdpi │ │ ├── ic_item.jpg │ │ ├── ic_left.png │ │ ├── ic_right.png │ │ ├── minus.png │ │ └── plus.png │ │ ├── drawable │ │ ├── btn_left_selector.xml │ │ ├── btn_right_selector.xml │ │ ├── left_selector.xml │ │ └── right_selector.xml │ │ ├── layout │ │ ├── activity_list_view.xml │ │ ├── activity_main.xml │ │ ├── activity_recycler.xml │ │ ├── listview_item.xml │ │ └── recyclerview_item.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 │ └── test │ └── java │ └── com │ └── jmf │ └── addsubutilstest │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image └── addsubutils_all.gif └── 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 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/checkstyle-idea.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /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 {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android商城购物车加减控件AddSubUtils 2 | 3 | 对购物车加减按钮控件的简单封装,几行代码就搞定,采用链式调用,而且样式支持自定义,并且在ListView中和RecyclerView中同样适用.这里的使用步骤讲的不是很细致,需要的朋友可以看下博客,里面讲了封装的过程和想法。如果你还在为商城购物车中的加减控件而烦恼,不妨试一下这个加减控件,相信你会爱上它。如果觉得有用,欢迎star和fork. 4 | 5 | # 相关博客 6 | 7 | [商城购物车加减控件的简单封装](http://blog.csdn.net/sinat_36668731/article/details/77163019) 8 | 9 | [商城购物车加减控件的简单封装(续),解决ListView中数据错乱的问题](http://blog.csdn.net/sinat_36668731/article/details/77337091) 10 | 11 | 12 | # 实例效果 13 | 14 | ![addsubutils_all.gif](https://github.com/Jmengfei/AddSubUtils/blob/master/image/addsubutils_all.gif) 15 | 16 | # 联系方式: 17 | 18 | ![这里写图片描述](http://img.blog.csdn.net/20170809175419051?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2luYXRfMzY2Njg3MzE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 19 | 20 | - 如果您有什么问题或者建议,欢迎加入qq群,这里还可以学习Kotlin。 21 | 22 | # Attributes属性(addsubutils布局文件中调用) 23 | 24 | | Attributes |forma |describe| 25 | | ----------- |-------|------- | 26 | |editable |boolean|是否可以手动输入| 27 | |location |string |输入框的位置(在左边还是右边),默认中间| 28 | |ImageWidth |dimension|左右2边+-按钮的宽度| 29 | |contentWidth |dimension|中间EditText的宽度| 30 | |contentTextSize|dimension|中间EditText的字体大小| 31 | |contentTextColor|color |中间字体的颜色| 32 | |all_background|color/reference|整个控件的background| 33 | |leftBackground|color/reference|左面控件的背景| 34 | |rightBackground|color/reference|右面控件的背景| 35 | |contentBackground|color/reference|中间控件的背景| 36 | |leftResources|color/reference|左面控件的资源| 37 | |rightResources|color/reference|右面控件的资源| 38 | 39 | # 使用步骤 40 | 41 | ### 1.依赖AddSubUtils 42 | ``` 43 | dependencies { 44 | compile 'com.mengfei:AddSubUtils:1.5.0' // 最新版本 45 | } 46 | ``` 47 | 或者下载源码包,链接:https://github.com/Jmengfei/AddSubUtils ,引用本地lib,并且在build.gradle中添加: 48 | ``` 49 | dependencies { 50 | compile project(':addsubutils') 51 | } 52 | ``` 53 | 54 | ### 2. 在xml代码中引用 55 | 56 | ``` 57 | 61 | ``` 62 | 你也可以自定义样式: 63 | 64 | ``` 65 | 80 | ``` 81 | 82 | ### 3.在Activity或者Fragment中配置AddSubUtils 83 | 84 | ``` 85 | AddSubUtils addSubUtils = (AddSubUtils) findViewById(R.id.add_sub); 86 | addSubUtils.setBuyMax(30) // 最大购买数,默认为int的最大值 87 | .setInventory(50) // 库存,默认为int的最大值 88 | .setCurrentNumber(5) // 设置当前数,默认为1 89 | .setStep(5) // 步长,默认为1 90 | .setBuyMin(2) // 购买的最小值,默认为1 91 | .setOnWarnListener(new AddSubUtils.OnWarnListener() { 92 | @Override 93 | public void onWarningForInventory(int inventory) { 94 | Toast.makeText(MainActivity.this, "当前库存:" + inventory, Toast.LENGTH_SHORT).show(); 95 | } 96 | 97 | @Override 98 | public void onWarningForBuyMax(int max) { 99 | Toast.makeText(MainActivity.this, "超过最大购买数:" + max, Toast.LENGTH_SHORT).show(); 100 | } 101 | 102 | @Override 103 | public void onWarningForBuyMin(int min) { 104 | Toast.makeText(MainActivity.this, "低于最小购买数:" + min, Toast.LENGTH_SHORT).show(); 105 | } 106 | }); 107 | ``` 108 | 109 | ### 4. 如果你是在ListView或者RecyclerView中使用: 110 | 111 | ``` 112 | holder.list_item_utils 113 | .setStep(1) 114 | .setBuyMax(30) 115 | .setPosition(position) // 传入当前位置,一定要传,不然数据会错乱 116 | .setCurrentNumber(dataBean.getValue()) 117 | .setInventory(50) 118 | .setOnWarnListener(new AddSubUtils.OnWarnListener() { 119 | @Override 120 | public void onWarningForInventory(int inventory) { 121 | Toast.makeText(ListViewActivity.this, "不能超过当前库存:" + inventory, Toast.LENGTH_SHORT).show(); 122 | } 123 | 124 | @Override 125 | public void onWarningForBuyMax(int max) { 126 | Toast.makeText(ListViewActivity.this, "超过最大购买数:" + max, Toast.LENGTH_SHORT).show(); 127 | } 128 | 129 | @Override 130 | public void onWarningForBuyMin(int min) { 131 | Toast.makeText(ListViewActivity.this, "低于最小购买数:" + min, Toast.LENGTH_SHORT).show(); 132 | } 133 | }) 134 | .setOnChangeValueListener(new AddSubUtils.OnChangeValueListener() { 135 | @Override 136 | public void onChangeValue(int value,int position) { 137 | setValue(position,value); // 使用传回来的position设置数据 138 | } 139 | }); 140 | ``` 141 | 注意:这里setPosition()是一定要有的 142 | 143 | 采用的是链式调用,这里你只需要传入你关心的值即可。 144 | 145 | # 关于我 146 | 147 | 一个想搞事情的Android攻城狮 148 | 149 | csdn主页:http://blog.csdn.net/sinat_36668731?viewmode=list 150 | 151 | 掘金主页:https://juejin.im/user/582991ff570c3500587d2da9 152 | 153 | 简书主页:http://www.jianshu.com/u/0e9fd9fee3a6 154 | -------------------------------------------------------------------------------- /addsubutils/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /addsubutils/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release'//添加 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.2" 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 25 11 | versionCode 2 12 | versionName "1.5.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 27 | exclude group: 'com.android.support', module: 'support-annotations' 28 | }) 29 | compile 'com.android.support:appcompat-v7:25.3.0' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | 33 | 34 | //添加 35 | publish { 36 | userOrg = 'mendeveloper'//bintray.com用户名 37 | repoName = 'maven'//远程仓库名字,不指明,默认是上传到maven 38 | groupId = 'com.mengfei'//jcenter上的路径 39 | artifactId = 'AddSubUtils'//项目名称 40 | publishVersion = '1.5.0'//版本号111 41 | desc = 'A simple package for the addition and subtraction of the shopping cart,' + 42 | ' a few lines of code to get, the use of chain calls, ' + 43 | 'and style support for custom'//描述,不重要 44 | website = 'https://github.com/Jmengfei/AddSubUtils'//网站,不重要;尽量模拟github上的地址,例如我这样的;当然你有地址最好了 45 | } 46 | 47 | -------------------------------------------------------------------------------- /addsubutils/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\programFiles\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 | -------------------------------------------------------------------------------- /addsubutils/src/androidTest/java/com/jmf/addsubutils/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutils; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.jmf.addsubutils.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /addsubutils/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /addsubutils/src/main/java/com/jmf/addsubutils/AddSubUtils.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutils; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.content.res.TypedArray; 6 | import android.graphics.drawable.Drawable; 7 | import android.os.Build; 8 | import android.text.Editable; 9 | import android.text.TextWatcher; 10 | import android.text.method.DigitsKeyListener; 11 | import android.util.AttributeSet; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.widget.EditText; 15 | import android.widget.ImageView; 16 | import android.widget.LinearLayout; 17 | 18 | 19 | /** 20 | * Created by 贾梦飞 on 2017/8/10 10:55. 21 | * QQ:821176301 22 | * 微信:j821176301 23 | * desc:类似购物车功能的增加和加减 24 | */ 25 | 26 | public class AddSubUtils extends LinearLayout implements View.OnClickListener, TextWatcher { 27 | 28 | private int mBuyMax = Integer.MAX_VALUE; // 最大购买数量,默认最大值 29 | private int inputValue = 1; //购买数量 30 | private int inventory = Integer.MAX_VALUE; //商品库存,默认最大值 31 | private int mBuyMin = 1;// 商品最小购买数量,默认值为1 32 | private int mStep = 1; // 步长--每次增加的个数,默认是1 33 | private int mPosition = 0; // 设置改变的位置,默认是0; //集合数据中会用到 34 | 35 | private OnWarnListener mOnWarnListener; 36 | private OnChangeValueListener mOnChangeValueListener; 37 | 38 | private EditText etInput; 39 | private ImageView icPlus; 40 | private ImageView icMinus; 41 | 42 | 43 | public AddSubUtils(Context context) { 44 | this(context, null); 45 | } 46 | 47 | public AddSubUtils(Context context, AttributeSet attrs) { 48 | this(context, attrs, 0); 49 | } 50 | 51 | public AddSubUtils(Context context, AttributeSet attrs, int defStyleAttr) { 52 | super(context, attrs, defStyleAttr); 53 | init(context, attrs, defStyleAttr); 54 | } 55 | 56 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 57 | private void init(Context context, AttributeSet attrs, int defStyleAttr) { 58 | //得到属性 59 | if (attrs != null) { 60 | TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.AddSubUtils); 61 | boolean editable = typeArray.getBoolean(R.styleable.AddSubUtils_editable, true); 62 | String location = typeArray.getString(R.styleable.AddSubUtils_location); 63 | // 左右两面的宽度 64 | int ImageWidth = typeArray.getDimensionPixelSize(R.styleable.AddSubUtils_ImageWidth, -1); 65 | // 中间内容框的宽度 66 | int contentWidth = typeArray.getDimensionPixelSize(R.styleable.AddSubUtils_contentWidth, -1); 67 | // 中间字体的大小 68 | int contentTextSize = typeArray.getDimensionPixelSize(R.styleable.AddSubUtils_contentTextSize, -1); 69 | // 中间字体的颜色 70 | int contentTextColor = typeArray.getColor(R.styleable.AddSubUtils_contentTextColor, 0xff000000); 71 | // 整个控件的background 72 | Drawable background = typeArray.getDrawable(R.styleable.AddSubUtils_all_background); 73 | // 左面控件的背景 74 | Drawable leftBackground = typeArray.getDrawable(R.styleable.AddSubUtils_leftBackground); 75 | // 右面控件的背景 76 | Drawable rightBackground = typeArray.getDrawable(R.styleable.AddSubUtils_rightBackground); 77 | // 中间控件的背景 78 | Drawable contentBackground = typeArray.getDrawable(R.styleable.AddSubUtils_contentBackground); 79 | // 左面控件的资源 80 | Drawable leftResources = typeArray.getDrawable(R.styleable.AddSubUtils_leftResources); 81 | // 右面控件的资源 82 | Drawable rightResources = typeArray.getDrawable(R.styleable.AddSubUtils_rightResources); 83 | // 资源回收 84 | typeArray.recycle(); 85 | 86 | if("start".equals(location)) { 87 | //把布局和当前类形成整体 88 | LayoutInflater.from(context).inflate(R.layout.add_sub_start_layout, this); 89 | }else if("end".equals(location)) { 90 | //把布局和当前类形成整体 91 | LayoutInflater.from(context).inflate(R.layout.add_sub_end_layout, this); 92 | }else { 93 | //把布局和当前类形成整体 94 | LayoutInflater.from(context).inflate(R.layout.add_sub_layout, this); 95 | } 96 | 97 | icPlus = (ImageView) findViewById(R.id.ic_plus); 98 | icMinus = (ImageView) findViewById(R.id.ic_minus); 99 | etInput = (EditText) findViewById(R.id.et_input); 100 | 101 | icPlus.setOnClickListener(this); 102 | icMinus.setOnClickListener(this); 103 | etInput.addTextChangedListener(this); 104 | 105 | setEditable(editable); 106 | etInput.setTextColor(contentTextColor); 107 | 108 | // 设置两边按钮的宽度 109 | if (ImageWidth > 0) { 110 | LayoutParams textParams = new LayoutParams(ImageWidth, LayoutParams.MATCH_PARENT); 111 | icPlus.setLayoutParams(textParams); 112 | icMinus.setLayoutParams(textParams); 113 | } 114 | 115 | // 设置中间输入框的宽度 116 | if (contentWidth > 0) { 117 | LayoutParams textParams = new LayoutParams(contentWidth, LayoutParams.MATCH_PARENT); 118 | etInput.setLayoutParams(textParams); 119 | } 120 | if (contentTextColor > 0) { 121 | etInput.setTextSize(contentTextColor); 122 | } 123 | if(contentTextSize > 0) { 124 | etInput.setTextSize(contentTextSize); 125 | } 126 | if (background != null) { 127 | setBackgroundDrawable(background); 128 | } else { 129 | setBackgroundResource(R.drawable.addsubutils_add_sub_bg); 130 | } 131 | 132 | if (contentBackground != null) { 133 | etInput.setBackground(contentBackground); 134 | } 135 | 136 | if(leftBackground != null) { 137 | icMinus.setBackground(leftBackground); 138 | } 139 | 140 | if (rightBackground != null){ 141 | icPlus.setBackground(rightBackground); 142 | } 143 | if (leftResources != null){ 144 | icMinus.setImageDrawable(leftResources); 145 | } 146 | if(rightResources != null) { 147 | icPlus.setImageDrawable(rightResources); 148 | } 149 | } 150 | } 151 | 152 | private void setEditable(boolean editable) { 153 | if (editable) { 154 | etInput.setFocusable(true); 155 | etInput.setKeyListener(new DigitsKeyListener()); 156 | } else { 157 | etInput.setFocusable(false); 158 | etInput.setKeyListener(null); 159 | } 160 | } 161 | 162 | @Override 163 | public void onClick(View view) { 164 | int id = view.getId(); 165 | if (id == R.id.ic_plus) { 166 | // 加 167 | if (inputValue < Math.min(mBuyMax, inventory)) { 168 | inputValue += mStep; 169 | //正常添加 170 | etInput.setText("" + inputValue); 171 | } else if (inventory < mBuyMax) { 172 | //库存不足 173 | warningForInventory(); 174 | } else { 175 | //超过最大购买数 176 | warningForBuyMax(); 177 | } 178 | } else if (id == R.id.ic_minus) { 179 | // 减 180 | if (inputValue > mBuyMin) { 181 | inputValue -= mStep; 182 | etInput.setText(inputValue + ""); 183 | } else { 184 | // 低于最小购买数 185 | warningForBuyMin(); 186 | } 187 | } else if (id == R.id.et_input) { 188 | // 输入框 189 | etInput.setSelection(etInput.getText().toString().length()); 190 | } 191 | } 192 | 193 | /** 194 | * 低于最小购买数 195 | * Warning for buy min. 196 | */ 197 | private void warningForBuyMin() { 198 | if (mOnWarnListener != null) mOnWarnListener.onWarningForBuyMin(mBuyMin); 199 | } 200 | 201 | /** 202 | * 超过的最大购买数限制 203 | * Warning for buy max. 204 | */ 205 | private void warningForBuyMax() { 206 | if (mOnWarnListener != null) mOnWarnListener.onWarningForBuyMax(mBuyMax); 207 | } 208 | 209 | /** 210 | * 超过的库存限制 211 | * Warning for inventory. 212 | */ 213 | private void warningForInventory() { 214 | if (mOnWarnListener != null) mOnWarnListener.onWarningForInventory(inventory); 215 | } 216 | 217 | @Override 218 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 219 | 220 | } 221 | 222 | @Override 223 | public void onTextChanged(CharSequence s, int start, int before, int count) { 224 | onNumberInput(); 225 | } 226 | 227 | /** 228 | * 监听输入的数据变化 229 | */ 230 | private void onNumberInput() { 231 | //当前数量 232 | int count = getNumber(); 233 | if (count < mBuyMin) { 234 | //手动输入 235 | inputValue = mBuyMin; 236 | etInput.setText(inputValue + ""); 237 | if(mOnChangeValueListener != null) { 238 | mOnChangeValueListener.onChangeValue(inputValue,mPosition); 239 | } 240 | return; 241 | } 242 | int limit = Math.min(mBuyMax, inventory); 243 | if (count > limit) { 244 | if (inventory < mBuyMax) { 245 | //库存不足 246 | warningForInventory(); 247 | } else { 248 | //超过最大购买数 249 | warningForBuyMax(); 250 | } 251 | }else{ 252 | inputValue = count; 253 | if(mOnChangeValueListener != null) { 254 | mOnChangeValueListener.onChangeValue(inputValue,mPosition); 255 | } 256 | } 257 | } 258 | 259 | public AddSubUtils setCurrentNumber(int currentNumber) { 260 | if (currentNumber < mBuyMin){ 261 | inputValue = mBuyMin; 262 | } else { 263 | inputValue = Math.min(Math.min(mBuyMax, inventory), currentNumber); 264 | } 265 | etInput.setText(inputValue + ""); 266 | return this; 267 | } 268 | 269 | public int getInventory() { 270 | return inventory; 271 | } 272 | 273 | public AddSubUtils setInventory(int inventory) { 274 | this.inventory = inventory; 275 | return this; 276 | } 277 | 278 | public AddSubUtils setBean(Object bean) { 279 | this.inventory = inventory; 280 | return this; 281 | } 282 | 283 | public int getBuyMax() { 284 | return mBuyMax; 285 | } 286 | 287 | public AddSubUtils setBuyMax(int buyMax) { 288 | mBuyMax = buyMax; 289 | return this; 290 | } 291 | public AddSubUtils setPosition(int position) { 292 | mPosition = position; 293 | return this; 294 | } 295 | public int getPosition() { 296 | return mPosition; 297 | } 298 | 299 | public AddSubUtils setBuyMin(int buyMin) { 300 | mBuyMin = buyMin; 301 | return this; 302 | } 303 | 304 | public AddSubUtils setOnWarnListener(OnWarnListener onWarnListener) { 305 | mOnWarnListener = onWarnListener; 306 | return this; 307 | } 308 | 309 | public AddSubUtils setOnChangeValueListener(OnChangeValueListener onChangeValueListener) { 310 | mOnChangeValueListener = onChangeValueListener; 311 | return this; 312 | } 313 | public int getStep() { 314 | return mStep; 315 | } 316 | 317 | public AddSubUtils setStep(int step) { 318 | mStep = step; 319 | return this; 320 | } 321 | 322 | 323 | @Override 324 | public void afterTextChanged(Editable s) { 325 | 326 | } 327 | 328 | /** 329 | * 得到输入框的数量 330 | * 331 | * @return 332 | */ 333 | public int getNumber() { 334 | try { 335 | return Integer.parseInt(etInput.getText().toString()); 336 | } catch (NumberFormatException e) { 337 | 338 | } 339 | etInput.setText(mBuyMin + ""); 340 | return mBuyMin; 341 | } 342 | 343 | public interface OnWarnListener { 344 | 345 | void onWarningForInventory(int inventory); 346 | 347 | void onWarningForBuyMax(int max); 348 | 349 | void onWarningForBuyMin(int min); 350 | } 351 | 352 | public interface OnChangeValueListener { 353 | 354 | void onChangeValue(int value,int position); 355 | } 356 | } 357 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/drawable-xxhdpi/addsubutils_ic_minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/addsubutils/src/main/res/drawable-xxhdpi/addsubutils_ic_minus.png -------------------------------------------------------------------------------- /addsubutils/src/main/res/drawable-xxhdpi/addsubutils_ic_plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/addsubutils/src/main/res/drawable-xxhdpi/addsubutils_ic_plus.png -------------------------------------------------------------------------------- /addsubutils/src/main/res/drawable/addsubutils_add_sub_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/drawable/addsubutils_left_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 17 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/drawable/addsubutils_right_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 17 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/drawable/divider_horizontal.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 7 | 8 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/layout/add_sub_end_layout.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 17 | 18 | 27 | 28 | 43 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/layout/add_sub_layout.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 17 | 18 | 33 | 34 | 43 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/layout/add_sub_start_layout.xml: -------------------------------------------------------------------------------- 1 | 7 | 22 | 23 | 32 | 33 | 34 | 35 | 44 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/values/color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #999 4 | #fff 5 | 6 | #FFE082 7 | #FFD54F 8 | #FFCA28 9 | 10 | #C5E1A5 11 | #AED581 12 | #9CCC65 13 | 14 | #80CBC4 15 | #4DB6AC 16 | #26A69A 17 | 18 | 19 | #999 20 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 30dp 4 | 10dp 5 | 15sp 6 | 65dp 7 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AddSubUtils 3 | 4 | -------------------------------------------------------------------------------- /addsubutils/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | -------------------------------------------------------------------------------- /addsubutils/src/test/java/com/jmf/addsubutils/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutils; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.jmf.addsubutilstest" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.0' 28 | testCompile 'junit:junit:4.12' 29 | // compile project(':addsubutils') 30 | compile 'com.mengfei:AddSubUtils:1.5.0' 31 | compile 'com.android.support:recyclerview-v7:25.3.0' 32 | compile 'com.github.bumptech.glide:glide:3.7.0' 33 | } 34 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\programFiles\android_SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/jmf/addsubutilstest/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutilstest; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.jmf.addsubutilstest", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/jmf/addsubutilstest/ListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutilstest; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | import android.widget.ImageView; 9 | import android.widget.ListView; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import com.bumptech.glide.Glide; 14 | import com.jmf.addsubutils.AddSubUtils; 15 | import com.jmf.addsubutilstest.bean.AddSubBean; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | import java.util.Random; 20 | 21 | public class ListViewActivity extends AppCompatActivity { 22 | private ListView mListView; 23 | 24 | private int[] bookImages = {R.drawable.book_1, R.drawable.book_2, R.drawable.book_3, R.drawable.book_4, 25 | R.drawable.book_5, R.drawable.book_6, R.drawable.book_7, R.drawable.book_8, R.drawable.book_9 26 | , R.drawable.book_10, R.drawable.book_11}; 27 | 28 | private String[] bookNames = {"Android开发艺术探索", "Android第一行代码", "深入理解Android内核设计思想 第2版", 29 | "Android 源码设计模式解析与实战", "Android群英传", "Android开发进阶 从小工到专家", "Android系统源代码情景分析(修订版)", 30 | "疯狂Android讲义", "腾讯Android自动化测试实战", "React Native入门与实战", "看见"}; 31 | 32 | private List mList = new ArrayList<>(); 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_list_view); 38 | 39 | initData(); 40 | 41 | mListView = (ListView) findViewById(R.id.activity_list_view); 42 | mListView.setAdapter(new MyAdapter()); 43 | } 44 | 45 | private void initData() { 46 | mList.add(new AddSubBean(bookNames[0], getBookValue(), bookImages[0])); 47 | mList.add(new AddSubBean(bookNames[1], getBookValue(), bookImages[1])); 48 | mList.add(new AddSubBean(bookNames[2], getBookValue(), bookImages[2])); 49 | mList.add(new AddSubBean(bookNames[3], getBookValue(), bookImages[3])); 50 | mList.add(new AddSubBean(bookNames[4], getBookValue(), bookImages[4])); 51 | mList.add(new AddSubBean(bookNames[5], getBookValue(), bookImages[5])); 52 | mList.add(new AddSubBean(bookNames[6], getBookValue(), bookImages[6])); 53 | mList.add(new AddSubBean(bookNames[7], getBookValue(), bookImages[7])); 54 | mList.add(new AddSubBean(bookNames[8], getBookValue(), bookImages[8])); 55 | mList.add(new AddSubBean(bookNames[9], getBookValue(), bookImages[9])); 56 | mList.add(new AddSubBean(bookNames[10], getBookValue(), bookImages[10])); 57 | } 58 | 59 | private int getBookValue() { 60 | Random random = new Random(); 61 | int bookValue = random.nextInt(10); 62 | return 1; 63 | } 64 | 65 | class MyAdapter extends BaseAdapter { 66 | 67 | @Override 68 | public int getCount() { 69 | return mList.size(); 70 | } 71 | 72 | @Override 73 | public Object getItem(int position) { 74 | return mList.get(position); 75 | } 76 | 77 | @Override 78 | public long getItemId(int position) { 79 | return 0; 80 | } 81 | 82 | @Override 83 | public View getView( int position, View convertView, ViewGroup parent) { 84 | ViewHolder holder; 85 | if (convertView == null) { 86 | convertView = View.inflate(ListViewActivity.this, R.layout.listview_item, null); 87 | holder = new ViewHolder(convertView); 88 | convertView.setTag(holder); 89 | } else { 90 | holder = (ViewHolder) convertView.getTag(); 91 | } 92 | AddSubBean dataBean = mList.get(position); 93 | holder.tv_item.setText(dataBean.getName()); 94 | Glide.with(ListViewActivity.this).load(dataBean.getImageId()).into(holder.iv_item); 95 | 96 | holder.list_item_utils 97 | .setStep(1) 98 | .setBuyMax(30) 99 | .setPosition(position) 100 | .setCurrentNumber(dataBean.getValue()) 101 | .setInventory(50) 102 | .setOnWarnListener(new AddSubUtils.OnWarnListener() { 103 | @Override 104 | public void onWarningForInventory(int inventory) { 105 | Toast.makeText(ListViewActivity.this, "不能超过当前库存:" + inventory, Toast.LENGTH_SHORT).show(); 106 | } 107 | 108 | @Override 109 | public void onWarningForBuyMax(int max) { 110 | Toast.makeText(ListViewActivity.this, "超过最大购买数:" + max, Toast.LENGTH_SHORT).show(); 111 | } 112 | 113 | @Override 114 | public void onWarningForBuyMin(int min) { 115 | Toast.makeText(ListViewActivity.this, "低于最小购买数:" + min, Toast.LENGTH_SHORT).show(); 116 | } 117 | }) 118 | .setOnChangeValueListener(new AddSubUtils.OnChangeValueListener() { 119 | @Override 120 | public void onChangeValue(int value,int position) { 121 | setValue(position,value); 122 | } 123 | }); 124 | return convertView; 125 | } 126 | } 127 | 128 | private void setValue(final int position, int inputValue) { 129 | mList.get(position).setValue(inputValue); 130 | } 131 | 132 | static class ViewHolder { 133 | ImageView iv_item; 134 | AddSubUtils list_item_utils; 135 | TextView tv_item; 136 | 137 | ViewHolder(View view) { 138 | iv_item = (ImageView) view.findViewById(R.id.iv_item); 139 | tv_item = (TextView) view.findViewById(R.id.tv_item); 140 | list_item_utils = (AddSubUtils) view.findViewById(R.id.list_item_utils); 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /app/src/main/java/com/jmf/addsubutilstest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutilstest; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.Toast; 9 | 10 | import com.jmf.addsubutils.AddSubUtils; 11 | 12 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 13 | private Button btnRecycler; 14 | private Button btnList; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | AddSubUtils addSubUtils = (AddSubUtils) findViewById(R.id.add_sub_0); 22 | addSubUtils.setBuyMax(30) 23 | .setInventory(50) 24 | .setCurrentNumber(5) 25 | .setStep(5) 26 | .setBuyMin(2) 27 | .setOnWarnListener(new AddSubUtils.OnWarnListener() { 28 | @Override 29 | public void onWarningForInventory(int inventory) { 30 | Toast.makeText(MainActivity.this, "当前库存:" + inventory, Toast.LENGTH_SHORT).show(); 31 | } 32 | 33 | @Override 34 | public void onWarningForBuyMax(int max) { 35 | Toast.makeText(MainActivity.this, "超过最大购买数:" + max, Toast.LENGTH_SHORT).show(); 36 | } 37 | 38 | @Override 39 | public void onWarningForBuyMin(int min) { 40 | Toast.makeText(MainActivity.this, "低于最小购买数:" + min, Toast.LENGTH_SHORT).show(); 41 | } 42 | 43 | }); 44 | 45 | btnRecycler = (Button) findViewById(R.id.btn_recycler); 46 | btnList = (Button) findViewById(R.id.btn_list); 47 | 48 | btnList.setOnClickListener(this); 49 | btnRecycler.setOnClickListener(this); 50 | } 51 | 52 | @Override 53 | public void onClick(View v) { 54 | switch (v.getId()) { 55 | case R.id.btn_recycler : 56 | Intent intent = new Intent(MainActivity.this,RecyclerActivity.class); 57 | startActivity(intent); 58 | break; 59 | case R.id.btn_list : 60 | Intent intent1 = new Intent(MainActivity.this,ListViewActivity.class); 61 | startActivity(intent1); 62 | break; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/jmf/addsubutilstest/MyRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutilstest; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.bumptech.glide.Glide; 13 | import com.jmf.addsubutils.AddSubUtils; 14 | import com.jmf.addsubutilstest.bean.AddSubBean; 15 | 16 | import java.util.List; 17 | 18 | import static com.jmf.addsubutilstest.R.id.recycler_item_utils; 19 | import static com.jmf.addsubutilstest.R.id.recycler_iv_item; 20 | 21 | /** 22 | * Created by 贾梦飞 on 2017/8/14 18:17. 23 | * QQ:821176301 24 | * 微信:j821176301 25 | * desc: 26 | */ 27 | 28 | public class MyRecyclerAdapter extends RecyclerView.Adapter { 29 | private List mList; 30 | private Context mContext ; 31 | private LayoutInflater inflater; 32 | public MyRecyclerAdapter(Context context, List mList) { 33 | this.mContext = context; 34 | this.mList = mList; 35 | inflater = LayoutInflater.from(mContext); 36 | } 37 | 38 | @Override 39 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 40 | return new MyViewHolder(inflater.inflate(R.layout.recyclerview_item,parent,false)); 41 | } 42 | 43 | @Override 44 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 45 | MyViewHolder myViewHolder = (MyViewHolder) holder; 46 | myViewHolder.initData(position); 47 | } 48 | 49 | @Override 50 | public int getItemCount() { 51 | return mList.size(); 52 | } 53 | 54 | private class MyViewHolder extends RecyclerView.ViewHolder { 55 | ImageView iv_item; 56 | AddSubUtils list_item_utils; 57 | TextView tv_item; 58 | public MyViewHolder(View itemView) { 59 | super(itemView); 60 | iv_item = (ImageView)itemView.findViewById(recycler_iv_item); 61 | tv_item = (TextView) itemView.findViewById(R.id.recycler_tv_item); 62 | list_item_utils = (AddSubUtils) itemView.findViewById(recycler_item_utils); 63 | } 64 | 65 | public void initData(int position) { 66 | tv_item.setText(mList.get(position).getName()); 67 | Glide.with(mContext).load(mList.get(position).getImageId()).into(iv_item); 68 | list_item_utils.setStep(2) 69 | .setPosition(position) 70 | .setCurrentNumber(mList.get(position).getValue()) 71 | .setBuyMax(50) 72 | .setInventory(36) 73 | .setOnWarnListener(new AddSubUtils.OnWarnListener() { 74 | @Override 75 | public void onWarningForInventory(int inventory) { 76 | Toast.makeText(mContext, "当前库存:" + inventory, Toast.LENGTH_SHORT).show(); 77 | } 78 | 79 | @Override 80 | public void onWarningForBuyMax(int max) { 81 | Toast.makeText(mContext, "超过最大购买数:" + max, Toast.LENGTH_SHORT).show(); 82 | } 83 | 84 | @Override 85 | public void onWarningForBuyMin(int min) { 86 | Toast.makeText(mContext, "低于最小购买数:" + min, Toast.LENGTH_SHORT).show(); 87 | } 88 | }) 89 | .setOnChangeValueListener(new AddSubUtils.OnChangeValueListener() { 90 | @Override 91 | public void onChangeValue(int value, int position) { 92 | setValue(position,value); 93 | } 94 | }); 95 | } 96 | } 97 | 98 | private void setValue(int position, int value) { 99 | mList.get(position).setValue(value); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/com/jmf/addsubutilstest/RecyclerActivity.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutilstest; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | import com.jmf.addsubutilstest.bean.AddSubBean; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Random; 13 | 14 | public class RecyclerActivity extends AppCompatActivity { 15 | 16 | private RecyclerView mRecyclerView; 17 | private MyRecyclerAdapter adapter ; 18 | private List mList = new ArrayList<>(); 19 | 20 | private int[] bookImages = {R.drawable.book_1, R.drawable.book_2, R.drawable.book_3, R.drawable.book_4, 21 | R.drawable.book_5, R.drawable.book_6, R.drawable.book_7, R.drawable.book_8, R.drawable.book_9 22 | , R.drawable.book_10, R.drawable.book_11}; 23 | 24 | private String[] bookNames = {"Android开发艺术探索", "Android第一行代码", "深入理解Android内核设计思想 第2版", 25 | "Android 源码设计模式解析与实战", "Android群英传", "Android开发进阶 从小工到专家", "Android系统源代码情景分析(修订版)", 26 | "疯狂Android讲义", "腾讯Android自动化测试实战", "React Native入门与实战", "看见"}; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_recycler); 32 | 33 | initData(); 34 | 35 | mRecyclerView = (RecyclerView) findViewById(R.id.activity_recycler); 36 | adapter = new MyRecyclerAdapter(RecyclerActivity.this,mList); 37 | mRecyclerView.setAdapter(adapter); 38 | //设置线性布局 39 | mRecyclerView.setLayoutManager(new LinearLayoutManager(RecyclerActivity.this, LinearLayoutManager.VERTICAL, false)); 40 | } 41 | 42 | private void initData() { 43 | mList.add(new AddSubBean(bookNames[0], getBookValue(), bookImages[0])); 44 | mList.add(new AddSubBean(bookNames[1], getBookValue(), bookImages[1])); 45 | mList.add(new AddSubBean(bookNames[2], getBookValue(), bookImages[2])); 46 | mList.add(new AddSubBean(bookNames[3], getBookValue(), bookImages[3])); 47 | mList.add(new AddSubBean(bookNames[4], getBookValue(), bookImages[4])); 48 | mList.add(new AddSubBean(bookNames[5], getBookValue(), bookImages[5])); 49 | mList.add(new AddSubBean(bookNames[6], getBookValue(), bookImages[6])); 50 | mList.add(new AddSubBean(bookNames[7], getBookValue(), bookImages[7])); 51 | mList.add(new AddSubBean(bookNames[8], getBookValue(), bookImages[8])); 52 | mList.add(new AddSubBean(bookNames[9], getBookValue(), bookImages[9])); 53 | mList.add(new AddSubBean(bookNames[10], getBookValue(), bookImages[10])); 54 | } 55 | 56 | private int getBookValue() { 57 | Random random = new Random(); 58 | int bookValue = random.nextInt(10); 59 | return 1; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/jmf/addsubutilstest/bean/AddSubBean.java: -------------------------------------------------------------------------------- 1 | package com.jmf.addsubutilstest.bean; 2 | 3 | /** 4 | * Created by 贾梦飞 on 2017/8/15 9:37. 5 | * QQ:821176301 6 | * 微信:j821176301 7 | * desc: 8 | */ 9 | 10 | public class AddSubBean { 11 | private String name; // 物品的名称 12 | private int value; // 物品当前值 13 | private int imageId; 14 | 15 | public AddSubBean(String name, int value, int imageId) { 16 | this.name = name; 17 | this.value = value; 18 | this.imageId = imageId; 19 | } 20 | 21 | public int getImageId() { 22 | return imageId; 23 | } 24 | 25 | public void setImageId(int imageId) { 26 | imageId = imageId; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public int getValue() { 38 | return value; 39 | } 40 | 41 | public void setValue(int value) { 42 | this.value = value; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_10.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_11.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_4.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_5.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_6.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_7.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_8.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/book_9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-hdpi/book_9.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_item.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-xxhdpi/ic_item.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-xxhdpi/ic_left.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-xxhdpi/ic_right.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-xxhdpi/minus.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jmengfei/AddSubUtils/f9348a71346b670240ca21289b011dd8dd92f9bf/app/src/main/res/drawable-xxhdpi/plus.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_left_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_right_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/left_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/right_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_list_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 17 | 18 | 29 | 30 | 45 | 46 |