Skip to content

Commit

Permalink
加上了评论区大家提到的扩展函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujiang521 committed Apr 29, 2020
1 parent 34ff307 commit aaef14c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 23 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ implementation 'com.github.zhujiang521:AndroidAOP:1.0.1'
```

OK。

新增了Kotlin的判断重复的方法,大家可以按照下面的方法直接进行调用,比写注解更加灵活方便:
```
btnKuoZhan.setSafeListener {
ToastUtils.showShort("222")
Log.e("kuozhan","wwww")
}
```
2 changes: 2 additions & 0 deletions app/src/main/java/zj/it/bhne/androidaop/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ToastUtils
import com.zj.singclick.SingleClick
import com.zj.singclick.click
import com.zj.singclick.setSafeListener
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() ,View.OnClickListener{
Expand Down
46 changes: 23 additions & 23 deletions app/src/main/java/zj/it/bhne/androidaop/OnClick.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import android.view.View
*
*/

inline fun View.setSafeListener(crossinline action:()->Unit){
var lastClick=0L
setOnClickListener {
val gap = System.currentTimeMillis() - lastClick
lastClick=System.currentTimeMillis()
if(gap<1500) return@setOnClickListener
action.invoke()
}
}


var _viewClickFlag = false
var _clickRunnable = Runnable { _viewClickFlag = false }
fun View.click(action: (view: View) -> Unit) {
setOnClickListener {
if (!_viewClickFlag) {
_viewClickFlag = true
action(it)
}
removeCallbacks(_clickRunnable)
postDelayed(_clickRunnable, 1000)
}
}
//inline fun View.setSafeListener(crossinline action:()->Unit){
// var lastClick=0L
// setOnClickListener {
// val gap = System.currentTimeMillis() - lastClick
// lastClick=System.currentTimeMillis()
// if(gap<1500) return@setOnClickListener
// action.invoke()
// }
//}
//
//
//var _viewClickFlag = false
//var _clickRunnable = Runnable { _viewClickFlag = false }
//fun View.click(action: (view: View) -> Unit) {
// setOnClickListener {
// if (!_viewClickFlag) {
// _viewClickFlag = true
// action(it)
// }
// removeCallbacks(_clickRunnable)
// postDelayed(_clickRunnable, 1000)
// }
//}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.72'
ext.kotlin_version = '1.3.10'
repositories {
google()
Expand Down
7 changes: 7 additions & 0 deletions singclick/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'android-aspectjx'
android {
compileSdkVersion 28
Expand Down Expand Up @@ -31,4 +33,9 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
37 changes: 37 additions & 0 deletions singclick/src/main/java/com/zj/singclick/OnClick.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.zj.singclick

import android.view.View

/**
* 版权:渤海新能 版权所有
* @author zhujiang
* 版本:1.5
* 创建日期:2020/4/29
* 描述:AndroidAOP
*
*/


inline fun View.setSafeListener(crossinline action:()->Unit){
var lastClick=0L
setOnClickListener {
val gap = System.currentTimeMillis() - lastClick
lastClick=System.currentTimeMillis()
if(gap<1500) return@setOnClickListener
action.invoke()
}
}


var _viewClickFlag = false
var _clickRunnable = Runnable { _viewClickFlag = false }
fun View.click(action: (view: View) -> Unit) {
setOnClickListener {
if (!_viewClickFlag) {
_viewClickFlag = true
action(it)
}
removeCallbacks(_clickRunnable)
postDelayed(_clickRunnable, 1000)
}
}

0 comments on commit aaef14c

Please sign in to comment.