Skip to content

Instantly share code, notes, and snippets.

@hkusu
Last active September 17, 2019 14:53
Show Gist options
  • Save hkusu/c24b491c33d75e19dc37b76705cae4a4 to your computer and use it in GitHub Desktop.
Save hkusu/c24b491c33d75e19dc37b76705cae4a4 to your computer and use it in GitHub Desktop.
Androidの連続クリック対策
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン"
android:onClick="@{(view) -> someViewModel.onButtonClicked(view)}"
/>
※ Data Binding でなくても普通にコードでリスナーをセットしてもOK(その場合でもViewは渡す)
fun onButtonClicked(view: View?) { // unitテスト時はnullを渡す
view?.disableContinuousClicks()
// do something..
}
fun View.disableContinuousClicks(interval: Long = 300) {
this.isClickable = false
this.postDelayed(interval) {
this.isClickable = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment