Kotlin Notes: Difference between revisions
From James's Wiki
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
===== Use the listener in another class ===== | ===== Use the listener in another class ===== | ||
<syntaxhighlight lang="kotlin"> | |||
dialogRapidShortTimePicker.onDismissListener = { | dialogRapidShortTimePicker.onDismissListener = { | ||
Timber.d("Dialog dismissed from helper") | Timber.d("Dialog dismissed from helper") | ||
dismiss() | dismiss() | ||
} | } | ||
</syntaxhighlight> | |||
=== Interfaces for callbacks === | === Interfaces for callbacks === | ||
Line 18: | Line 20: | ||
==== Code in HomeFragment ==== | ==== Code in HomeFragment ==== | ||
At the type we define the Interface used for the listener | At the type we define the Interface used for the listener | ||
<syntaxhighlight lang="kotlin"> | |||
enum class DialogTypes { | enum class DialogTypes { | ||
RAPID_SHORT_TIME, | RAPID_SHORT_TIME, | ||
Line 46: | Line 48: | ||
} | } | ||
} | } | ||
</syntaxhighlight> | |||
==== In the class that we want to call the listener from ==== | ==== In the class that we want to call the listener from ==== | ||
<syntaxhighlight lang="kotlin"> | |||
class SomeClass { | class SomeClass { | ||
var listener: DialogListener?=null | var listener: DialogListener?=null | ||
Line 63: | Line 67: | ||
listener?.openDialog(DialogTypes.RAPID_SHORT_TIME) | listener?.openDialog(DialogTypes.RAPID_SHORT_TIME) | ||
} | } | ||
</syntaxhighlight> |
Revision as of 11:14, 2 March 2025
Custom Listeners
Define the listener
var onDismissListener: (() -> Unit)? = null
Invoke the listener
onDismissListener?.invoke()
Use the listener in another class
dialogRapidShortTimePicker.onDismissListener = {
Timber.d("Dialog dismissed from helper")
dismiss()
}
Interfaces for callbacks
Here is an interface used to open dialogs from HomeFragment since dialogs must be opened from a fragment or activity
Code in HomeFragment
At the type we define the Interface used for the listener
enum class DialogTypes {
RAPID_SHORT_TIME,
TIME_INPUT_HOUR
}
interface DialogListener {
fun openDialog(type: DialogTypes)
}
class HomeFragment : Fragment(), LocationProvider, DialogListener {
//other code
...
override fun openDialog(type: DialogTypes) {
Timber.i("openDialog type: $type")
when (type) {
DialogTypes.RAPID_SHORT_TIME -> {
val dialogRapidShortTimeHelper = DialogRapidShortTimeHelper()
dialogRapidShortTimeHelper.show(childFragmentManager, "dialog")
}
DialogTypes.TIME_INPUT_HOUR -> {
val dialogTimeInputHourHelper = DialogTimeInputHourHelper()
dialogTimeInputHourHelper.show(childFragmentManager, "dialog")
}
}
}
}
In the class that we want to call the listener from
class SomeClass {
var listener: DialogListener?=null
...
other code
...
override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
val x = event.x.toInt()
val y = event.y.toInt()
Timber.v("onSingleTapConfirmed: $x, $y")
...
other code
...
segments[index].preTripSegment.durationClickArea.contains(x, y) -> {
//this is the part that performs the callback
listener?.openDialog(DialogTypes.RAPID_SHORT_TIME)
}