Pro Android with Kotlin


CHAPTER 9: User Interface


Download 5.42 Mb.
Pdf ko'rish
bet193/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   189   190   191   192   193   194   195   196   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile



242
CHAPTER 9: User Interface
when(event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
Log.e("LOG","Action: ACTION_DOWN " +
event.toString())
}
MotionEvent.ACTION_UP -> {
Log.e("LOG","Action: ACTION_UP " +
event.toString())
}
MotionEvent.ACTION_MOVE -> {
Log.e("LOG","Action: MOVE " +
event.toString())
}
else -> handled = false
}
return handled || super.onTouchEvent(event)
}
You can see that we return true when our listener handles events. If we receive an ACTION_
DOWN event, returning true is important; otherwise, both move and up actions will be ignored.
UI Design with Movable Items
If your app needs movable UI elements, you should use the FrameLayout class or a subclass 
of it. We want to take care of UI element positions ourselves and don’t want a layout class 
to interfere with that. The FrameLayout class does not position its elements dynamically, so 
using it is a good choice for that kind of positioning.
If you want your view to be movable, you create a subclass and overwrite its onTouchEvent() 
method. For example, say you have an ImageView and want to have it movable. For that aim
create a subclass as follows:
class MyImageView : ImageView {
constructor(context: Context)
: super(context)
constructor(context: Context, attrs: AttributeSet)
: super(context, attrs)
var dx : Float = 0.0f
var dy : Float = 0.0f
override
fun onTouchEvent(event: MotionEvent): Boolean {
var handled = true
Note We check the actionMasked accessor of the event object, not the action accessor as is 
suggested often. The reason for this is that the action accessor might contain additional bits if a 
multitouch event happens. The masked variant is just more reliable.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   189   190   191   192   193   194   195   196   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling