I am trying to override View.setRotation() method in Kotlin.
Since AndroidKTX already provided property extension "rotation", caller's can simply call
viewObject.rotation = 90.0f
to rotate the view.
However, I want to add some additional operation when user change the rotation, like
override fun setRotation(newRotation: Float) {
if (rotation == newRotation)
return
rotation = newRotation
doSomethingElse()
}
This will crash because of StackOverflow error.
So, I have to add some additional code to achieve the goal:
private var _rotation: Float = 0.0f
override fun setRotation(newRotation: Float) {
if (_rotation == newRotation) {
return
}
_rotation = newRotation
updateRotationInternally()
}
private fun updateRotationInternally() {
super.setRotation(_rotation)
doSomethingElse()
}
This works, but I wonder if there is some other more elegant way of doing this, like "override the property extension setter"?