├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── README_CN.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── publish.sh ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cc │ │ └── taylorzhang │ │ └── singleclick │ │ └── sample │ │ ├── JavaSampleActivity.java │ │ ├── JavaSampleViewModel.java │ │ ├── LogUtil.kt │ │ ├── MainActivity.kt │ │ └── MainViewModel.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_java_sample.xml │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── themes.xml ├── settings.gradle └── single-click ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── gradle.properties └── src ├── androidTest ├── AndroidManifest.xml └── java │ └── cc │ └── taylorzhang │ └── singleclick │ ├── ClickableSpanSingleClickTest.kt │ ├── SingleClickTestActivity.kt │ └── ViewSingleClickTest.kt └── main ├── AndroidManifest.xml ├── java └── cc │ └── taylorzhang │ └── singleclick │ ├── SingleClickUtil.kt │ ├── SpannableStringBuilder.kt │ └── View.kt └── res └── values └── ids.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | *.apk 3 | .gradle 4 | /local.properties 5 | .DS_Store 6 | /build 7 | .idea 8 | /captures 9 | .externalNativeBuild -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## Version 1.0.0 (2021-03-21) 4 | 5 | Initial release. 6 | 7 | ## Version 1.1.0 (2021-09-13) 8 | 9 | Optimized for use in Java. -------------------------------------------------------------------------------- /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 2021 Taylor Zhang 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 | English | [简体中文](README_CN.md) 2 | 3 | # SingleClick 4 | 5 | A library that gracefully handles repeated clicks in Android. 6 | 7 | [![Maven Central](https://img.shields.io/maven-central/v/cc.taylorzhang/single-click.svg?style=flat)](https://search.maven.org/artifact/cc.taylorzhang/single-click) 8 | [![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14) 9 | [![License](https://img.shields.io/badge/License-Apache--2.0-brightgreen.svg?style=flat)](LICENSE) 10 | 11 | ## Download 12 | 13 | ```groovy 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | implementation 'cc.taylorzhang:single-click:1.1.0' 20 | } 21 | ``` 22 | 23 | ## Usage 24 | 25 | ### View 26 | 27 | kotlin: 28 | 29 | ```kotlin 30 | btn1.onSingleClick { 31 | // handle single click 32 | } 33 | 34 | btn2.onSingleClick(interval = 2000, isShareSingleClick = false) { 35 | // handle single click 36 | } 37 | ``` 38 | 39 | java: 40 | 41 | ```java 42 | SingleClickUtil.onSingleClick(btn1, v -> { 43 | // handle single click 44 | }); 45 | 46 | SingleClickUtil.onSingleClick(btn2, 2000, false, v -> { 47 | // handle single click 48 | }); 49 | ``` 50 | 51 | ### DataBinding 52 | 53 | layout: 54 | 55 | ```xml 56 | 63 | ``` 64 | 65 | kotlin: 66 | 67 | ```kotlin 68 | class YourViewModel : ViewModel() { 69 | 70 | fun handleClick() { 71 | // handle single click 72 | } 73 | } 74 | ``` 75 | 76 | java: 77 | 78 | ```java 79 | public class YourViewModel extends ViewModel { 80 | 81 | public void handleClick() { 82 | // handle single click 83 | } 84 | } 85 | ``` 86 | 87 | ### Rich Text 88 | 89 | kotlin: 90 | 91 | ```kotlin 92 | tvText.movementMethod = LinkMovementMethod.getInstance() 93 | tvText.highlightColor = Color.TRANSPARENT 94 | tvText.text = buildSpannedString { 95 | append("normalText") 96 | onSingleClick({ 97 | // handle single click 98 | }) { 99 | color(Color.GREEN) { append("clickText") } 100 | } 101 | } 102 | ``` 103 | 104 | java: 105 | 106 | ```java 107 | tvText.setMovementMethod(LinkMovementMethod.getInstance()); 108 | tvText.setHighlightColor(Color.TRANSPARENT); 109 | SpannedString spannedString = SpannableStringBuilderKt.buildSpannedString(builder -> { 110 | builder.append("normalText"); 111 | SingleClickUtil.onSingleClick(builder, v -> { 112 | // handle single click 113 | }, builder1 -> { 114 | SpannableStringBuilderKt.color(builder1, Color.GREEN, builder2 -> { 115 | builder.append("clickText"); 116 | return null; 117 | }); 118 | return null; 119 | }); 120 | return null; 121 | }); 122 | tvText.setText(spannedString); 123 | ``` 124 | 125 | ### RecyclerView 126 | 127 | Adapter use [BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper), the code is similar if you use other adapters. 128 | 129 | Kotlin Item Click: 130 | 131 | ```kotlin 132 | adapter.setOnItemClickListener { _, view, _ -> 133 | view.determineTriggerSingleClick { 134 | // handle single click 135 | } 136 | } 137 | ``` 138 | 139 | Kotlin Item Child Click: 140 | 141 | ```kotlin 142 | adapter.addChildClickViewIds(R.id.btn1, R.id.btn2) 143 | adapter.setOnItemChildClickListener { _, view, _ -> 144 | when (view.id) { 145 | R.id.btn1 -> { 146 | // handle normal click 147 | } 148 | R.id.btn2 -> view.determineTriggerSingleClick { 149 | // handle single click 150 | } 151 | } 152 | } 153 | ``` 154 | 155 | Java Item Click: 156 | 157 | ```java 158 | adapter.setOnItemClickListener((adapter1, view, position) -> { 159 | SingleClickUtil.determineTriggerSingleClick(view, v -> { 160 | // handle single click 161 | }); 162 | }); 163 | ``` 164 | 165 | Java Item Child Click: 166 | 167 | ```java 168 | adapter.setOnItemChildClickListener((adapter1, view, position) -> { 169 | if (view.getId() == R.id.btn1) { 170 | // handle normal click 171 | } else if (view.getId() == R.id.btn2) { 172 | SingleClickUtil.determineTriggerSingleClick(view, v -> { 173 | // handle single click 174 | }); 175 | } 176 | }); 177 | ``` 178 | 179 | ## License 180 | 181 | [Apache license 2.0](LICENSE) © Taylor Zhang -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | [English](README.md) | 简体中文 2 | 3 | # SingleClick 4 | 5 | 在 Android 中优雅处理重复点击的库。 6 | 7 | [![Maven Central](https://img.shields.io/maven-central/v/cc.taylorzhang/single-click.svg?style=flat)](https://search.maven.org/artifact/cc.taylorzhang/single-click) 8 | [![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14) 9 | [![License](https://img.shields.io/badge/License-Apache--2.0-brightgreen.svg?style=flat)](LICENSE) 10 | 11 | ## 下载 12 | 13 | ```groovy 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | implementation 'cc.taylorzhang:single-click:1.1.0' 20 | } 21 | ``` 22 | 23 | ## 使用 24 | 25 | ### View 26 | 27 | kotlin: 28 | 29 | ```kotlin 30 | btn1.onSingleClick { 31 | // 处理单次点击 32 | } 33 | 34 | btn2.onSingleClick(interval = 2000, isShareSingleClick = false) { 35 | // 处理单次点击 36 | } 37 | ``` 38 | 39 | java: 40 | 41 | ```java 42 | SingleClickUtil.onSingleClick(btn1, v -> { 43 | // 处理单次点击 44 | }); 45 | 46 | SingleClickUtil.onSingleClick(btn2, 2000, false, v -> { 47 | // 处理单次点击 48 | }); 49 | ``` 50 | 51 | ### 数据绑定 52 | 53 | 布局: 54 | 55 | ```xml 56 | 63 | ``` 64 | 65 | kotlin: 66 | 67 | ```kotlin 68 | class YourViewModel : ViewModel() { 69 | 70 | fun handleClick() { 71 | // 处理单次点击 72 | } 73 | } 74 | ``` 75 | 76 | java: 77 | 78 | ```java 79 | public class YourViewModel extends ViewModel { 80 | 81 | public void handleClick() { 82 | // 处理单次点击 83 | } 84 | } 85 | ``` 86 | 87 | ### 富文本 88 | 89 | kotlin: 90 | 91 | ```kotlin 92 | tvText.movementMethod = LinkMovementMethod.getInstance() 93 | tvText.highlightColor = Color.TRANSPARENT 94 | tvText.text = buildSpannedString { 95 | append("normalText") 96 | onSingleClick({ 97 | // 处理单次点击 98 | }) { 99 | color(Color.GREEN) { append("clickText") } 100 | } 101 | } 102 | ``` 103 | 104 | java: 105 | 106 | ```java 107 | tvText.setMovementMethod(LinkMovementMethod.getInstance()); 108 | tvText.setHighlightColor(Color.TRANSPARENT); 109 | SpannedString spannedString = SpannableStringBuilderKt.buildSpannedString(builder -> { 110 | builder.append("normalText"); 111 | SingleClickUtil.onSingleClick(builder, v -> { 112 | // 处理单次点击 113 | }, builder1 -> { 114 | SpannableStringBuilderKt.color(builder1, Color.GREEN, builder2 -> { 115 | builder.append("clickText"); 116 | return null; 117 | }); 118 | return null; 119 | }); 120 | return null; 121 | }); 122 | tvText.setText(spannedString); 123 | ``` 124 | 125 | ### RecyclerView 126 | 127 | 适配器使用 [BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper),如果你使用其他适配器代码也是类似的。 128 | 129 | Kotlin Item 点击: 130 | 131 | ```kotlin 132 | adapter.setOnItemClickListener { _, view, _ -> 133 | view.determineTriggerSingleClick { 134 | // 处理单次点击 135 | } 136 | } 137 | ``` 138 | 139 | Kotlin Item Child 点击: 140 | 141 | ```kotlin 142 | adapter.addChildClickViewIds(R.id.btn1, R.id.btn2) 143 | adapter.setOnItemChildClickListener { _, view, _ -> 144 | when (view.id) { 145 | R.id.btn1 -> { 146 | // 处理普通点击 147 | } 148 | R.id.btn2 -> view.determineTriggerSingleClick { 149 | // 处理单次点击 150 | } 151 | } 152 | } 153 | ``` 154 | 155 | Java Item 点击: 156 | 157 | ```java 158 | adapter.setOnItemClickListener((adapter1, view, position) -> { 159 | SingleClickUtil.determineTriggerSingleClick(view, v -> { 160 | // 处理普通点击 161 | }); 162 | }); 163 | ``` 164 | 165 | Java Item Child 点击: 166 | 167 | ```java 168 | adapter.setOnItemChildClickListener((adapter1, view, position) -> { 169 | if (view.getId() == R.id.btn1) { 170 | // handle normal click 171 | } else if (view.getId() == R.id.btn2) { 172 | SingleClickUtil.determineTriggerSingleClick(view, v -> { 173 | // 处理单次点击 174 | }); 175 | } 176 | }); 177 | ``` 178 | 179 | ## License 180 | 181 | [Apache license 2.0](LICENSE) © Taylor Zhang -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | ext.kotlin_version = "1.4.31" 4 | 5 | repositories { 6 | google() 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:4.2.1' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 | classpath 'com.vanniktech:gradle-maven-publish-plugin:0.15.1' 14 | 15 | // NOTE: Do not place your application dependencies here; they belong 16 | // in the individual module build.gradle files 17 | } 18 | } 19 | 20 | allprojects { 21 | repositories { 22 | google() 23 | mavenCentral() 24 | maven { url 'https://jitpack.io' } 25 | } 26 | plugins.withId("com.vanniktech.maven.publish") { 27 | mavenPublish { 28 | sonatypeHost = "S01" 29 | } 30 | } 31 | } 32 | 33 | task clean(type: Delete) { 34 | delete rootProject.buildDir 35 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Mar 17 16:41:06 CST 2021 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-6.7.1-bin.zip -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /publish.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./gradlew publish --no-daemon --no-parallel 4 | ./gradlew closeAndReleaseRepository -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | id 'kotlin-kapt' 5 | } 6 | 7 | android { 8 | compileSdkVersion 30 9 | buildToolsVersion "30.0.3" 10 | 11 | defaultConfig { 12 | applicationId "cc.taylorzhang.singleclick.sample" 13 | minSdkVersion 17 14 | targetSdkVersion 30 15 | versionCode 1 16 | versionName "1.0" 17 | 18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | 33 | kotlinOptions { 34 | jvmTarget = '1.8' 35 | } 36 | 37 | buildFeatures { 38 | dataBinding = true 39 | } 40 | } 41 | 42 | dependencies { 43 | implementation 'androidx.appcompat:appcompat:1.3.1' 44 | implementation 'androidx.activity:activity-ktx:1.3.1' 45 | implementation 'androidx.core:core-ktx:1.6.0' 46 | implementation "androidx.recyclerview:recyclerview:1.2.1" 47 | implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4' 48 | implementation project(':single-click') 49 | } -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /sample/src/main/java/cc/taylorzhang/singleclick/sample/JavaSampleActivity.java: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick.sample; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.text.SpannedString; 6 | import android.text.method.LinkMovementMethod; 7 | import android.view.Gravity; 8 | import android.widget.TextView; 9 | 10 | import androidx.annotation.NonNull; 11 | import androidx.appcompat.app.AppCompatActivity; 12 | import androidx.core.text.SpannableStringBuilderKt; 13 | import androidx.databinding.DataBindingUtil; 14 | import androidx.lifecycle.ViewModelProvider; 15 | import androidx.recyclerview.widget.LinearLayoutManager; 16 | 17 | import com.chad.library.adapter.base.BaseQuickAdapter; 18 | import com.chad.library.adapter.base.viewholder.BaseViewHolder; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Arrays; 22 | 23 | import cc.taylorzhang.singleclick.SingleClickUtil; 24 | import cc.taylorzhang.singleclick.sample.databinding.ActivityJavaSampleBinding; 25 | 26 | public class JavaSampleActivity extends AppCompatActivity { 27 | 28 | private ActivityJavaSampleBinding mBinding; 29 | 30 | private JavaSampleViewModel mViewModel; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | 36 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_java_sample); 37 | mViewModel = new ViewModelProvider(getViewModelStore(), getDefaultViewModelProviderFactory()) 38 | .get(JavaSampleViewModel.class); 39 | mBinding.setViewModel(mViewModel); 40 | 41 | initButton(); 42 | initRecyclerView(); 43 | initTextView(); 44 | } 45 | 46 | private void initButton() { 47 | SingleClickUtil.onSingleClick(mBinding.btn1, v -> { 48 | LogUtil.i("btn1 clicked"); 49 | }); 50 | 51 | SingleClickUtil.onSingleClick(mBinding.btn2, 2000, false, v -> { 52 | LogUtil.i("btn2 clicked"); 53 | }); 54 | } 55 | 56 | private void initRecyclerView() { 57 | String[] array = getResources().getStringArray(R.array.list); 58 | ArrayList list = new ArrayList<>(Arrays.asList(array)); 59 | BaseQuickAdapter adapter = new BaseQuickAdapter( 60 | android.R.layout.simple_list_item_1, list 61 | ) { 62 | 63 | @Override 64 | protected void convert(@NonNull BaseViewHolder holder, String item) { 65 | TextView text1 = holder.getView(android.R.id.text1); 66 | text1.setTextSize(20); 67 | text1.setText(item); 68 | text1.setGravity(Gravity.CENTER); 69 | } 70 | }; 71 | adapter.setOnItemClickListener((adapter1, view, position) -> { 72 | SingleClickUtil.determineTriggerSingleClick(view, v -> { 73 | LogUtil.i(adapter.getItem(position) + " clicked"); 74 | }); 75 | }); 76 | mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this)); 77 | mBinding.recyclerView.setAdapter(adapter); 78 | } 79 | 80 | private void initTextView() { 81 | mBinding.tvText.setMovementMethod(LinkMovementMethod.getInstance()); 82 | mBinding.tvText.setHighlightColor(Color.TRANSPARENT); 83 | SpannedString spannedString = SpannableStringBuilderKt.buildSpannedString(builder -> { 84 | builder.append("normalText"); 85 | SingleClickUtil.onSingleClick(builder, v -> { 86 | LogUtil.i("clickText clicked"); 87 | }, builder1 -> { 88 | SpannableStringBuilderKt.color(builder1, Color.GREEN, builder2 -> { 89 | builder.append("clickText"); 90 | return null; 91 | }); 92 | return null; 93 | }); 94 | return null; 95 | }); 96 | mBinding.tvText.setText(spannedString); 97 | } 98 | } -------------------------------------------------------------------------------- /sample/src/main/java/cc/taylorzhang/singleclick/sample/JavaSampleViewModel.java: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick.sample; 2 | 3 | import androidx.lifecycle.ViewModel; 4 | 5 | /** 6 | * Created by ZhangKun on 2021/9/9. 7 | */ 8 | public class JavaSampleViewModel extends ViewModel { 9 | 10 | public void handleBtn3Click() { 11 | LogUtil.i("btn3 clicked"); 12 | } 13 | 14 | public void handleBtn4Click() { 15 | LogUtil.i("btn4 clicked"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sample/src/main/java/cc/taylorzhang/singleclick/sample/LogUtil.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick.sample 2 | 3 | import android.util.Log 4 | 5 | /** 6 | *
 7 |  *     author : Taylor Zhang
 8 |  *     time   : 2021/03/20
 9 |  *     desc   : Log util.
10 |  *     version: 1.0.0
11 |  * 
12 | */ 13 | object LogUtil { 14 | 15 | private var mLogFlag = false 16 | 17 | @JvmStatic 18 | fun i(any: Any) { 19 | if (mLogFlag) { 20 | Log.i(". SingleClick", any.toString()) 21 | } else { 22 | Log.i(" .SingleClick", any.toString()) 23 | } 24 | mLogFlag = !mLogFlag 25 | } 26 | } -------------------------------------------------------------------------------- /sample/src/main/java/cc/taylorzhang/singleclick/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick.sample 2 | 3 | import android.content.Intent 4 | import android.graphics.Color 5 | import android.os.Bundle 6 | import android.text.method.LinkMovementMethod 7 | import android.view.Gravity 8 | import android.widget.TextView 9 | import androidx.activity.viewModels 10 | import androidx.appcompat.app.AppCompatActivity 11 | import androidx.core.text.buildSpannedString 12 | import androidx.core.text.color 13 | import androidx.databinding.DataBindingUtil 14 | import androidx.recyclerview.widget.LinearLayoutManager 15 | import cc.taylorzhang.singleclick.determineTriggerSingleClick 16 | import cc.taylorzhang.singleclick.onSingleClick 17 | import cc.taylorzhang.singleclick.sample.databinding.ActivityMainBinding 18 | import com.chad.library.adapter.base.BaseQuickAdapter 19 | import com.chad.library.adapter.base.viewholder.BaseViewHolder 20 | 21 | /** 22 | *
23 |  *     author : Taylor Zhang
24 |  *     time   : 2021/03/20
25 |  *     desc   : Main activity.
26 |  *     version: 1.0.0
27 |  * 
28 | */ 29 | class MainActivity : AppCompatActivity() { 30 | 31 | private lateinit var mBinding: ActivityMainBinding 32 | 33 | private val mViewModel: MainViewModel by viewModels() 34 | 35 | override fun onCreate(savedInstanceState: Bundle?) { 36 | super.onCreate(savedInstanceState) 37 | 38 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) 39 | mBinding.viewModel = mViewModel 40 | 41 | initButton() 42 | initRecyclerView() 43 | initTextView() 44 | } 45 | 46 | private fun initButton() { 47 | mBinding.launchJavaSample.onSingleClick { 48 | startActivity(Intent(this, JavaSampleActivity::class.java)) 49 | } 50 | 51 | mBinding.btn1.onSingleClick { 52 | LogUtil.i("btn1 clicked") 53 | } 54 | 55 | mBinding.btn2.onSingleClick(interval = 2000, isShareSingleClick = false) { 56 | LogUtil.i("btn2 clicked") 57 | } 58 | } 59 | 60 | private fun initRecyclerView() { 61 | val list = resources.getStringArray(R.array.list).toMutableList() 62 | val adapter = object : BaseQuickAdapter( 63 | android.R.layout.simple_list_item_1, list 64 | ) { 65 | override fun convert(holder: BaseViewHolder, item: String) { 66 | holder.getView(android.R.id.text1).apply { 67 | textSize = 20f 68 | text = item 69 | gravity = Gravity.CENTER 70 | } 71 | } 72 | } 73 | adapter.setOnItemClickListener { _, view, position -> 74 | view.determineTriggerSingleClick { 75 | LogUtil.i("${adapter.getItem(position)} clicked") 76 | } 77 | } 78 | mBinding.recyclerView.layoutManager = LinearLayoutManager(this) 79 | mBinding.recyclerView.adapter = adapter 80 | } 81 | 82 | private fun initTextView() { 83 | mBinding.tvText.movementMethod = LinkMovementMethod.getInstance() 84 | mBinding.tvText.highlightColor = Color.TRANSPARENT 85 | mBinding.tvText.text = buildSpannedString { 86 | append("normalText") 87 | onSingleClick({ 88 | LogUtil.i("clickText clicked") 89 | }) { 90 | color(Color.GREEN) { append("clickText") } 91 | } 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /sample/src/main/java/cc/taylorzhang/singleclick/sample/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick.sample 2 | 3 | import androidx.lifecycle.ViewModel 4 | 5 | /** 6 | *
 7 |  *     author : Taylor Zhang
 8 |  *     time   : 2021/03/20
 9 |  *     desc   : Main Activity View Model
10 |  *     version: 1.0.0
11 |  * 
12 | */ 13 | class MainViewModel : ViewModel() { 14 | 15 | fun handleBtn3Click() { 16 | LogUtil.i("btn3 clicked") 17 | } 18 | 19 | fun handleBtn4Click() { 20 | LogUtil.i("btn4 clicked") 21 | } 22 | } -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_java_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 20 | 21 | 28 | 29 | 36 | 37 | 45 | 46 | 56 | 57 | 62 | 63 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 20 | 21 | 28 | 29 | 36 | 37 | 44 | 45 | 53 | 54 | 64 | 65 | 70 | 71 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #12C194 6 | -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20sp 4 | 16dp 5 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SingleClickSample 3 | 4 | launchJavaSample 5 | btn1 6 | btn2 7 | btn3 8 | btn4 9 | 10 | 11 | Android 12 | Kotlin 13 | Java 14 | 15 | -------------------------------------------------------------------------------- /sample/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "single-click" 2 | include ':single-click' 3 | include ':sample' -------------------------------------------------------------------------------- /single-click/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /single-click/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'kotlin-android' 4 | id 'kotlin-kapt' 5 | id 'com.vanniktech.maven.publish' 6 | } 7 | 8 | android { 9 | compileSdkVersion 30 10 | buildToolsVersion "30.0.3" 11 | 12 | defaultConfig { 13 | minSdkVersion 14 14 | targetSdkVersion 30 15 | versionCode 1 16 | versionName "1.0" 17 | 18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 | consumerProguardFiles "consumer-rules.pro" 20 | } 21 | 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | } 26 | } 27 | 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | 33 | kotlinOptions { 34 | jvmTarget = '1.8' 35 | } 36 | 37 | buildFeatures { 38 | dataBinding = true 39 | } 40 | } 41 | 42 | dependencies { 43 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 44 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 45 | androidTestImplementation 'androidx.test:rules:1.3.0' 46 | androidTestImplementation 'androidx.core:core-ktx:1.3.2' 47 | 48 | compileOnly 'androidx.core:core-ktx:1.3.2' 49 | } -------------------------------------------------------------------------------- /single-click/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TaylorKunZhang/single-click/eeb208cd99e28ab6afc30862b42caa0abeac589f/single-click/consumer-rules.pro -------------------------------------------------------------------------------- /single-click/gradle.properties: -------------------------------------------------------------------------------- 1 | GROUP=cc.taylorzhang 2 | POM_ARTIFACT_ID=single-click 3 | VERSION_NAME=1.1.0 4 | 5 | POM_NAME=single-click 6 | POM_DESCRIPTION=A library that gracefully handles repeated clicks in Android. 7 | POM_INCEPTION_YEAR=2021 8 | POM_URL=https://github.com/TaylorKunZhang/single-click 9 | 10 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 11 | POM_LICENCE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt 12 | POM_LICENCE_DIST=repo 13 | 14 | POM_SCM_URL=https://github.com/TaylorKunZhang/single-click/ 15 | POM_SCM_CONNECTION=scm:git:git://github.com/TaylorKunZhang/single-click.git 16 | POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/TaylorKunZhang/single-click.git 17 | 18 | POM_DEVELOPER_ID=TaylorZhang 19 | POM_DEVELOPER_NAME=TaylorZhang 20 | POM_DEVELOPER_URL=https://github.com/TaylorKunZhang -------------------------------------------------------------------------------- /single-click/src/androidTest/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /single-click/src/androidTest/java/cc/taylorzhang/singleclick/ClickableSpanSingleClickTest.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick 2 | 3 | import android.text.SpannedString 4 | import android.text.style.ClickableSpan 5 | import android.view.View 6 | import android.widget.TextView 7 | import androidx.core.text.buildSpannedString 8 | import androidx.test.core.app.ActivityScenario 9 | import androidx.test.espresso.Espresso 10 | import androidx.test.espresso.NoMatchingViewException 11 | import androidx.test.espresso.UiController 12 | import androidx.test.espresso.ViewAction 13 | import androidx.test.espresso.matcher.ViewMatchers 14 | import androidx.test.ext.junit.rules.ActivityScenarioRule 15 | import androidx.test.ext.junit.runners.AndroidJUnit4 16 | import org.hamcrest.Matcher 17 | import org.hamcrest.Matchers 18 | import org.junit.Assert.assertEquals 19 | import org.junit.Before 20 | import org.junit.Rule 21 | import org.junit.Test 22 | import org.junit.runner.RunWith 23 | 24 | /** 25 | *
 26 |  *     author : Taylor Zhang
 27 |  *     time   : 2021/03/21
 28 |  *     desc   : ClickableSpan single click test.
 29 |  *     version: 1.0.0
 30 |  * 
31 | */ 32 | @RunWith(AndroidJUnit4::class) 33 | class ClickableSpanSingleClickTest { 34 | 35 | @get:Rule 36 | val rule = ActivityScenarioRule(SingleClickTestActivity::class.java) 37 | 38 | private lateinit var scenario: ActivityScenario 39 | 40 | @Before 41 | fun setup() { 42 | scenario = rule.scenario 43 | } 44 | 45 | @Test 46 | fun testOneClickableSpanSingleClickInterval() { 47 | var text1SingleClickedCount = 0 48 | val interval = 1500 49 | 50 | scenario.onActivity { 51 | it.tvText.text = buildSpannedString { 52 | onSingleClick( 53 | { 54 | text1SingleClickedCount++ 55 | }, interval = interval 56 | ) { 57 | append("text1") 58 | } 59 | } 60 | } 61 | 62 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 63 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 64 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 65 | assertEquals(1, text1SingleClickedCount) 66 | 67 | Thread.sleep(interval.toLong()) 68 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 69 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 70 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 71 | assertEquals(2, text1SingleClickedCount) 72 | } 73 | 74 | @Test 75 | fun testTwoClickableSpanSingleClickInterval() { 76 | var text1SingleClickedCount = 0 77 | var text2SingleClickedCount = 0 78 | val interval = 1500 79 | 80 | scenario.onActivity { 81 | it.tvText.text = buildSpannedString { 82 | onSingleClick( 83 | { 84 | text1SingleClickedCount++ 85 | }, interval = interval 86 | ) { 87 | append("text1") 88 | } 89 | onSingleClick( 90 | { 91 | text2SingleClickedCount++ 92 | }, interval = interval 93 | ) { 94 | append("text2") 95 | } 96 | } 97 | } 98 | 99 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 100 | Espresso.onView(ViewMatchers.withSubstring("text2")).perform(clickClickableSpan("text2")) 101 | assertEquals(1, text1SingleClickedCount) 102 | assertEquals(0, text2SingleClickedCount) 103 | 104 | Thread.sleep(interval.toLong()) 105 | Espresso.onView(ViewMatchers.withSubstring("text2")).perform(clickClickableSpan("text2")) 106 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 107 | assertEquals(1, text1SingleClickedCount) 108 | assertEquals(1, text2SingleClickedCount) 109 | } 110 | 111 | @Test 112 | fun testOneClickableSpanNotShareSingleClick() { 113 | var text1SingleClickedCount = 0 114 | var text2SingleClickedCount = 0 115 | 116 | scenario.onActivity { 117 | it.tvText.text = buildSpannedString { 118 | onSingleClick( 119 | { 120 | text1SingleClickedCount++ 121 | }, isShareSingleClick = false 122 | ) { 123 | append("text1") 124 | } 125 | onSingleClick({ text2SingleClickedCount++ }) { append("text2") } 126 | } 127 | } 128 | 129 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 130 | Espresso.onView(ViewMatchers.withSubstring("text2")).perform(clickClickableSpan("text2")) 131 | assertEquals(1, text1SingleClickedCount) 132 | assertEquals(1, text2SingleClickedCount) 133 | } 134 | 135 | @Test 136 | fun testTwoClickableSpanNotShareSingleClick() { 137 | var text1SingleClickedCount = 0 138 | var text2SingleClickedCount = 0 139 | 140 | scenario.onActivity { 141 | it.tvText.text = buildSpannedString { 142 | onSingleClick( 143 | { 144 | text1SingleClickedCount++ 145 | }, isShareSingleClick = false 146 | ) { 147 | append("text1") 148 | } 149 | onSingleClick( 150 | { 151 | text2SingleClickedCount++ 152 | }, 153 | isShareSingleClick = false 154 | ) { 155 | append("text2") 156 | } 157 | } 158 | } 159 | 160 | Espresso.onView(ViewMatchers.withSubstring("text1")).perform(clickClickableSpan("text1")) 161 | Espresso.onView(ViewMatchers.withSubstring("text2")).perform(clickClickableSpan("text2")) 162 | assertEquals(1, text1SingleClickedCount) 163 | assertEquals(1, text2SingleClickedCount) 164 | } 165 | 166 | private fun clickClickableSpan(textToClick: CharSequence): ViewAction { 167 | return object : ViewAction { 168 | override fun getConstraints(): Matcher { 169 | return Matchers.instanceOf(TextView::class.java) 170 | } 171 | 172 | override fun getDescription(): String { 173 | return "clicking on a ClickableSpan" 174 | } 175 | 176 | override fun perform(uiController: UiController?, view: View) { 177 | val textView = view as TextView 178 | val spannedString = textView.text as SpannedString 179 | if (spannedString.isEmpty()) { 180 | // TextView is empty, nothing to do 181 | throw NoMatchingViewException.Builder() 182 | .includeViewHierarchy(true) 183 | .withRootView(textView) 184 | .build() 185 | } 186 | 187 | // Get the links inside the TextView and check if we find textToClick 188 | val spans = spannedString.getSpans( 189 | 0, spannedString.length, 190 | ClickableSpan::class.java 191 | ) 192 | if (spans.isNotEmpty()) { 193 | var spanCandidate: ClickableSpan? 194 | for (span in spans) { 195 | spanCandidate = span 196 | val start = spannedString.getSpanStart(spanCandidate) 197 | val end = spannedString.getSpanEnd(spanCandidate) 198 | val sequence = spannedString.subSequence(start, end) 199 | if (textToClick.toString() == sequence.toString()) { 200 | span.onClick(textView) 201 | return 202 | } 203 | } 204 | } 205 | throw NoMatchingViewException.Builder() 206 | .includeViewHierarchy(true) 207 | .withRootView(textView) 208 | .build() 209 | } 210 | } 211 | } 212 | } -------------------------------------------------------------------------------- /single-click/src/androidTest/java/cc/taylorzhang/singleclick/SingleClickTestActivity.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick 2 | 3 | import android.app.Activity 4 | import android.os.Bundle 5 | import android.widget.Button 6 | import android.widget.LinearLayout 7 | import android.widget.TextView 8 | 9 | /** 10 | *
11 |  *     author : Taylor Zhang
12 |  *     time   : 2021/03/21
13 |  *     desc   : Single click test Activity.
14 |  *     version: 1.0.0
15 |  * 
16 | */ 17 | class SingleClickTestActivity : Activity() { 18 | 19 | lateinit var btn1: Button 20 | 21 | lateinit var btn2: Button 22 | 23 | lateinit var tvText: TextView 24 | 25 | override fun onCreate(savedInstanceState: Bundle?) { 26 | super.onCreate(savedInstanceState) 27 | 28 | val group = LinearLayout(this) 29 | group.orientation = LinearLayout.VERTICAL 30 | 31 | btn1 = Button(this) 32 | btn1.text = "btn1" 33 | group.addView(btn1) 34 | 35 | btn2 = Button(this) 36 | btn2.text = "btn2" 37 | group.addView(btn2) 38 | 39 | tvText = TextView(this) 40 | group.addView(tvText) 41 | 42 | setContentView(group) 43 | } 44 | } -------------------------------------------------------------------------------- /single-click/src/androidTest/java/cc/taylorzhang/singleclick/ViewSingleClickTest.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick 2 | 3 | import androidx.test.core.app.ActivityScenario 4 | import androidx.test.espresso.Espresso.onView 5 | import androidx.test.espresso.action.ViewActions.click 6 | import androidx.test.espresso.matcher.ViewMatchers.withText 7 | import androidx.test.ext.junit.rules.ActivityScenarioRule 8 | import androidx.test.ext.junit.runners.AndroidJUnit4 9 | import org.junit.Assert.assertEquals 10 | import org.junit.Before 11 | import org.junit.Rule 12 | import org.junit.Test 13 | import org.junit.runner.RunWith 14 | 15 | /** 16 | *
 17 |  *     author : Taylor Zhang
 18 |  *     time   : 2021/03/21
 19 |  *     desc   : View single click test.
 20 |  *     version: 1.0.0
 21 |  * 
22 | */ 23 | @RunWith(AndroidJUnit4::class) 24 | class ViewSingleClickTest { 25 | 26 | @get:Rule 27 | val rule = ActivityScenarioRule(SingleClickTestActivity::class.java) 28 | 29 | private lateinit var scenario: ActivityScenario 30 | 31 | @Before 32 | fun setup() { 33 | scenario = rule.scenario 34 | } 35 | 36 | @Test 37 | fun testOneViewSingleClick() { 38 | var btn1SingleClickedCount = 0 39 | val interval = 1500 40 | 41 | scenario.onActivity { 42 | it.btn1.onSingleClick(interval = 1500) { btn1SingleClickedCount++ } 43 | } 44 | 45 | onView(withText("btn1")).perform(click()) 46 | onView(withText("btn1")).perform(click()) 47 | onView(withText("btn1")).perform(click()) 48 | assertEquals(1, btn1SingleClickedCount) 49 | 50 | Thread.sleep(interval.toLong()) 51 | onView(withText("btn1")).perform(click()) 52 | onView(withText("btn1")).perform(click()) 53 | onView(withText("btn1")).perform(click()) 54 | assertEquals(2, btn1SingleClickedCount) 55 | } 56 | 57 | @Test 58 | fun testTwoViewSingleClick() { 59 | var btn1SingleClickedCount = 0 60 | var btn2SingleClickedCount = 0 61 | val interval = 1500 62 | 63 | scenario.onActivity { 64 | it.btn1.onSingleClick(interval = interval) { btn1SingleClickedCount++ } 65 | it.btn2.onSingleClick(interval = interval) { btn2SingleClickedCount++ } 66 | } 67 | 68 | onView(withText("btn1")).perform(click()) 69 | onView(withText("btn2")).perform(click()) 70 | assertEquals(1, btn1SingleClickedCount) 71 | assertEquals(0, btn2SingleClickedCount) 72 | 73 | Thread.sleep(interval.toLong()) 74 | onView(withText("btn2")).perform(click()) 75 | onView(withText("btn1")).perform(click()) 76 | assertEquals(1, btn1SingleClickedCount) 77 | assertEquals(1, btn2SingleClickedCount) 78 | } 79 | 80 | @Test 81 | fun testOneViewNotShareSingleClick() { 82 | var btn1SingleClickedCount = 0 83 | var btn2SingleClickedCount = 0 84 | 85 | scenario.onActivity { 86 | it.btn1.onSingleClick(isShareSingleClick = false) { btn1SingleClickedCount++ } 87 | it.btn2.onSingleClick { btn2SingleClickedCount++ } 88 | } 89 | 90 | onView(withText("btn1")).perform(click()) 91 | onView(withText("btn2")).perform(click()) 92 | assertEquals(1, btn1SingleClickedCount) 93 | assertEquals(1, btn2SingleClickedCount) 94 | } 95 | 96 | @Test 97 | fun testTwoViewNotShareSingleClick() { 98 | var btn1SingleClickedCount = 0 99 | var btn2SingleClickedCount = 0 100 | 101 | scenario.onActivity { 102 | it.btn1.onSingleClick(isShareSingleClick = false) { btn1SingleClickedCount++ } 103 | it.btn2.onSingleClick(isShareSingleClick = false) { btn2SingleClickedCount++ } 104 | } 105 | 106 | onView(withText("btn1")).perform(click()) 107 | onView(withText("btn2")).perform(click()) 108 | assertEquals(1, btn1SingleClickedCount) 109 | assertEquals(1, btn2SingleClickedCount) 110 | } 111 | } -------------------------------------------------------------------------------- /single-click/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /single-click/src/main/java/cc/taylorzhang/singleclick/SingleClickUtil.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick 2 | 3 | import android.text.SpannableStringBuilder 4 | import android.text.TextPaint 5 | import android.text.style.ClickableSpan 6 | import android.view.View 7 | import androidx.core.text.inSpans 8 | import java.util.concurrent.TimeUnit 9 | 10 | /** 11 | *
 12 |  *     author : Taylor Zhang
 13 |  *     time   : 2021/03/19
 14 |  *     desc   : Single click util.
 15 |  *     version: 1.0.0
 16 |  * 
17 | */ 18 | object SingleClickUtil { 19 | 20 | /** 21 | * Global single click interval. 22 | */ 23 | var singleClickInterval: Int = 1000 24 | set(value) { 25 | if (value <= 0) { 26 | throw IllegalArgumentException("Single click interval must be greater than 0.") 27 | } 28 | field = value 29 | } 30 | 31 | /** 32 | * Register a callback to be invoked when this view is single clicked. 33 | * 34 | * @param listener Single click listener. 35 | */ 36 | @JvmStatic 37 | fun onSingleClick(view: View, listener: View.OnClickListener) { 38 | view.onSingleClick(listener = listener) 39 | } 40 | 41 | /** 42 | * Register a callback to be invoked when this view is single clicked. 43 | * 44 | * @param interval Single click interval. Unit is [TimeUnit.MILLISECONDS]. 45 | * @param listener Single click listener. 46 | */ 47 | @JvmStatic 48 | fun onSingleClick(view: View, interval: Int, listener: View.OnClickListener) { 49 | view.onSingleClick(interval, listener = listener) 50 | } 51 | 52 | /** 53 | * Register a callback to be invoked when this view is single clicked. 54 | * 55 | * @param isShareSingleClick True if this view is share single click interval whit other view 56 | * in same Activity, false otherwise. 57 | * @param listener Single click listener. 58 | */ 59 | @JvmStatic 60 | fun onSingleClick(view: View, isShareSingleClick: Boolean, listener: View.OnClickListener) { 61 | view.onSingleClick(isShareSingleClick = isShareSingleClick, listener = listener) 62 | } 63 | 64 | /** 65 | * Register a callback to be invoked when this view is single clicked. 66 | * 67 | * @param interval Single click interval. Unit is [TimeUnit.MILLISECONDS]. 68 | * @param isShareSingleClick True if this view is share single click interval whit other view 69 | * in same Activity, false otherwise. 70 | * @param listener Single click listener. 71 | */ 72 | @JvmStatic 73 | fun onSingleClick( 74 | view: View, 75 | interval: Int, 76 | isShareSingleClick: Boolean, 77 | listener: View.OnClickListener 78 | ) { 79 | view.onSingleClick(interval, isShareSingleClick, listener) 80 | } 81 | 82 | /** 83 | * Wrap appended text in [builderAction] in a [ClickableSpan]. 84 | * If selected and single clicked, the [listener] will be invoked. 85 | * 86 | * @param listener Single click listener. 87 | * @see SpannableStringBuilder.inSpans 88 | */ 89 | @JvmStatic 90 | fun onSingleClick( 91 | builder: SpannableStringBuilder, 92 | listener: View.OnClickListener, 93 | builderAction: SpannableStringBuilder .() -> Unit 94 | ) { 95 | builder.onSingleClick(listener, builderAction = builderAction) 96 | } 97 | 98 | /** 99 | * Wrap appended text in [builderAction] in a [ClickableSpan]. 100 | * If selected and single clicked, the [listener] will be invoked. 101 | * 102 | * @param listener Single click listener. 103 | * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS]. 104 | * @see SpannableStringBuilder.inSpans 105 | */ 106 | @JvmStatic 107 | fun onSingleClick( 108 | builder: SpannableStringBuilder, 109 | interval: Int, 110 | listener: View.OnClickListener, 111 | builderAction: SpannableStringBuilder .() -> Unit 112 | ) { 113 | builder.onSingleClick(listener, interval, builderAction = builderAction) 114 | } 115 | 116 | /** 117 | * Wrap appended text in [builderAction] in a [ClickableSpan]. 118 | * If selected and single clicked, the [listener] will be invoked. 119 | * 120 | * @param listener Single click listener. 121 | * @param isShareSingleClick True if this view is share single click interval whit other view 122 | * in same Activity, false otherwise. 123 | * @see SpannableStringBuilder.inSpans 124 | */ 125 | @JvmStatic 126 | fun onSingleClick( 127 | builder: SpannableStringBuilder, 128 | isShareSingleClick: Boolean, 129 | listener: View.OnClickListener, 130 | builderAction: SpannableStringBuilder .() -> Unit 131 | ) { 132 | builder.onSingleClick( 133 | listener, isShareSingleClick = isShareSingleClick, builderAction = builderAction 134 | ) 135 | } 136 | 137 | /** 138 | * Wrap appended text in [builderAction] in a [ClickableSpan]. 139 | * If selected and single clicked, the [listener] will be invoked. 140 | * 141 | * @param listener Single click listener. 142 | * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS]. 143 | * @param isShareSingleClick True if this view is share single click interval whit other view 144 | * in same Activity, false otherwise. 145 | * @param updateDrawStateAction Update draw state action. 146 | * @see SpannableStringBuilder.inSpans 147 | */ 148 | @JvmStatic 149 | fun onSingleClick( 150 | builder: SpannableStringBuilder, 151 | interval: Int, 152 | isShareSingleClick: Boolean, 153 | listener: View.OnClickListener, 154 | updateDrawStateAction: ((TextPaint) -> Unit), 155 | builderAction: SpannableStringBuilder .() -> Unit 156 | ) { 157 | builder.onSingleClick( 158 | listener, interval, isShareSingleClick, updateDrawStateAction, builderAction 159 | ) 160 | } 161 | 162 | /** 163 | * Determine whether to trigger a single click. 164 | * 165 | * @param listener Single click listener. 166 | */ 167 | @JvmStatic 168 | fun determineTriggerSingleClick(view: View, listener: View.OnClickListener) { 169 | view.determineTriggerSingleClick(listener = listener) 170 | } 171 | 172 | /** 173 | * Determine whether to trigger a single click. 174 | * 175 | * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS]. 176 | * @param listener Single click listener. 177 | */ 178 | @JvmStatic 179 | fun determineTriggerSingleClick(view: View, interval: Int, listener: View.OnClickListener) { 180 | view.determineTriggerSingleClick(interval = interval, listener = listener) 181 | } 182 | 183 | /** 184 | * Determine whether to trigger a single click. 185 | * 186 | * @param isShareSingleClick True if this view is share single click interval whit other view 187 | * in same Activity, false otherwise. 188 | * @param listener Single click listener. 189 | */ 190 | @JvmStatic 191 | fun determineTriggerSingleClick( 192 | view: View, 193 | isShareSingleClick: Boolean, 194 | listener: View.OnClickListener 195 | ) { 196 | view.determineTriggerSingleClick( 197 | isShareSingleClick = isShareSingleClick, listener = listener 198 | ) 199 | } 200 | 201 | /** 202 | * Determine whether to trigger a single click. 203 | * 204 | * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS]. 205 | * @param isShareSingleClick True if this view is share single click interval whit other view 206 | * in same Activity, false otherwise. 207 | * @param listener Single click listener. 208 | */ 209 | @JvmStatic 210 | fun determineTriggerSingleClick( 211 | view: View, 212 | interval: Int, 213 | isShareSingleClick: Boolean, 214 | listener: View.OnClickListener 215 | ) { 216 | view.determineTriggerSingleClick(interval, isShareSingleClick, listener) 217 | } 218 | } -------------------------------------------------------------------------------- /single-click/src/main/java/cc/taylorzhang/singleclick/SpannableStringBuilder.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick 2 | 3 | import android.text.SpannableStringBuilder 4 | import android.text.TextPaint 5 | import android.text.style.ClickableSpan 6 | import android.view.View 7 | import androidx.core.text.inSpans 8 | import java.util.concurrent.TimeUnit 9 | 10 | /** 11 | *
12 |  *     author : Taylor Zhang
13 |  *     time   : 2021/03/20
14 |  *     desc   : SpannableStringBuilder extensions.
15 |  *     version: 1.0.0
16 |  * 
17 | */ 18 | 19 | /** 20 | * Wrap appended text in [builderAction] in a [ClickableSpan]. 21 | * If selected and single clicked, the [listener] will be invoked. 22 | * 23 | * @param listener Single click listener. 24 | * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS]. 25 | * @param isShareSingleClick True if this view is share single click interval whit other view 26 | * in same Activity, false otherwise. 27 | * @param updateDrawStateAction Update draw state action. 28 | * @see SpannableStringBuilder.inSpans 29 | */ 30 | inline fun SpannableStringBuilder.onSingleClick( 31 | listener: View.OnClickListener, 32 | interval: Int = SingleClickUtil.singleClickInterval, 33 | isShareSingleClick: Boolean = true, 34 | noinline updateDrawStateAction: ((TextPaint) -> Unit)? = null, 35 | builderAction: SpannableStringBuilder .() -> Unit 36 | ): SpannableStringBuilder = inSpans( 37 | SingleClickableSpan( 38 | listener, interval, isShareSingleClick, updateDrawStateAction 39 | ), 40 | builderAction = builderAction 41 | ) 42 | 43 | /** 44 | * Single clickable span. 45 | */ 46 | class SingleClickableSpan( 47 | private val listener: View.OnClickListener, 48 | private val interval: Int = SingleClickUtil.singleClickInterval, 49 | private val isShareSingleClick: Boolean = true, 50 | private val updateDrawStateAction: ((TextPaint) -> Unit)? = null, 51 | ) : ClickableSpan() { 52 | 53 | private var mFakeView: View? = null 54 | 55 | override fun onClick(widget: View) { 56 | if (isShareSingleClick) { 57 | widget 58 | } else { 59 | if (mFakeView == null) { 60 | mFakeView = View(widget.context) 61 | } 62 | mFakeView!! 63 | }.determineTriggerSingleClick(interval, isShareSingleClick, listener) 64 | } 65 | 66 | override fun updateDrawState(ds: TextPaint) { 67 | updateDrawStateAction?.invoke(ds) 68 | } 69 | } -------------------------------------------------------------------------------- /single-click/src/main/java/cc/taylorzhang/singleclick/View.kt: -------------------------------------------------------------------------------- 1 | package cc.taylorzhang.singleclick 2 | 3 | import android.app.Activity 4 | import android.content.ContextWrapper 5 | import android.os.SystemClock 6 | import android.view.View 7 | import androidx.databinding.BindingAdapter 8 | import java.util.concurrent.TimeUnit 9 | 10 | /** 11 | *
12 |  *     author : Taylor Zhang
13 |  *     time   : 2021/03/19
14 |  *     desc   : View extensions.
15 |  *     version: 1.0.0
16 |  * 
17 | */ 18 | 19 | /** 20 | * Register a callback to be invoked when this view is single clicked. 21 | * 22 | * @param interval Single click interval. Unit is [TimeUnit.MILLISECONDS]. 23 | * @param isShareSingleClick True if this view is share single click interval whit other view 24 | * in same Activity, false otherwise. 25 | * @param listener Single click listener. 26 | */ 27 | @BindingAdapter( 28 | *["singleClickInterval", "isShareSingleClick", "onSingleClick"], 29 | requireAll = false 30 | ) 31 | fun View.onSingleClick( 32 | interval: Int? = SingleClickUtil.singleClickInterval, 33 | isShareSingleClick: Boolean? = true, 34 | listener: View.OnClickListener? = null 35 | ) { 36 | if (listener == null) { 37 | return 38 | } 39 | 40 | setOnClickListener { 41 | determineTriggerSingleClick( 42 | interval ?: SingleClickUtil.singleClickInterval, isShareSingleClick ?: true, listener 43 | ) 44 | } 45 | } 46 | 47 | /** 48 | * Determine whether to trigger a single click. 49 | * 50 | * @param interval Single click interval.Unit is [TimeUnit.MILLISECONDS]. 51 | * @param isShareSingleClick True if this view is share single click interval whit other view 52 | * in same Activity, false otherwise. 53 | * @param listener Single click listener. 54 | */ 55 | fun View.determineTriggerSingleClick( 56 | interval: Int = SingleClickUtil.singleClickInterval, 57 | isShareSingleClick: Boolean = true, 58 | listener: View.OnClickListener 59 | ) { 60 | val target = if (isShareSingleClick) getActivity(this)?.window?.decorView ?: this else this 61 | val millis = target.getTag(R.id.single_click_tag_last_single_click_millis) as? Long ?: 0 62 | if (SystemClock.uptimeMillis() - millis >= interval) { 63 | target.setTag(R.id.single_click_tag_last_single_click_millis, SystemClock.uptimeMillis()) 64 | listener.onClick(this) 65 | } 66 | } 67 | 68 | private fun getActivity(view: View): Activity? { 69 | var context = view.context 70 | while (context is ContextWrapper) { 71 | if (context is Activity) { 72 | return context 73 | } 74 | context = context.baseContext 75 | } 76 | return null 77 | } -------------------------------------------------------------------------------- /single-click/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | --------------------------------------------------------------------------------