Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Split up OnTouchListener #382

Open
romtsn opened this issue Mar 1, 2018 · 6 comments
Open

Split up OnTouchListener #382

romtsn opened this issue Mar 1, 2018 · 6 comments

Comments

@romtsn
Copy link
Contributor

romtsn commented Mar 1, 2018

Would it make sense to have something like

inline fun View.doOnActionUp(crossinline action: (event: MotionEvent) -> Unit)
inline fun View.doOnActionMove(crossinline action: (event: MotionEvent) -> Unit)
inline fun View.doOnActionCancel(crossinline action: (event: MotionEvent) -> Unit)
...

instead of wall of code in a single OnTouchListener ?

@JakeWharton
Copy link
Contributor

We need to figure out a general multi-callback listener strategy... I'm going to coalesce these into a mega issue.

@dovahkiin98
Copy link

maybe something like this

view.onTouchListener (
    onActionUp = {

    },
    onActionDown = {

    }
)

@Edward608
Copy link

I think the similar to what @dovahkiin98 suggest, the same can be done to OnGestureListener

@xiaofeidev
Copy link

@dovahkiin98 I like this,this‘ s named DSL

@guxiaonian
Copy link

I just do this:
fun View.doOnTouchListener(
onTouch: Boolean,
onActionUp: ((motionEvent: MotionEvent) -> Unit)? = null,
onActionDown: ((motionEvent: MotionEvent) -> Unit)? = null,
onActionMove: ((motionEvent: MotionEvent) -> Unit)? = null
): View.OnTouchListener {
val listener = View.OnTouchListener { _, p1 ->
when (p1!!.action) {
MotionEvent.ACTION_UP -> onActionUp?.invoke(p1)
MotionEvent.ACTION_DOWN -> onActionDown?.invoke(p1)
MotionEvent.ACTION_MOVE -> onActionMove?.invoke(p1)
}
onTouch
}
setOnTouchListener(listener)
return listener
}

also:
tv.doOnTouchListener(true,
onActionDown = {
},
onActionMove = {
},
onActionUp = {
}
)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@JakeWharton @romtsn @dovahkiin98 @Edward608 @xiaofeidev @guxiaonian and others