Zorgan
Zorgan

Reputation: 9123

View.OnLongClick() doesn't work when implementing onTouch() ACTION_DOWN

I'm trying to enable drag and dropping imageviews after the imageview has been held for a certain duration (long click).

So the dragging and dropping works fine (onTouch ACTION_DOWN/ACTION_MOVE/ACTION_UP), however it prevents me from detecting View.OnLongClick. When I remove the onTouch, the OnLongClick is successfully detected.

How can I get these 2 listeners to be working side by side?

onCreate:

{
    editPhoto1.setOnTouchListener(this)
    editPhoto2.setOnTouchListener(this)
    editPhoto3.setOnTouchListener(this)
    editPhoto1.isLongClickable = true
    editPhoto2.isLongClickable = true
    editPhoto3.isLongClickable = true
    editPhoto1.setOnLongClickListener(this)
    editPhoto2.setOnLongClickListener(this)
    editPhoto3.setOnLongClickListener(this)
}

onLongClick

override fun onLongClick(v: View?): Boolean {
    Log.d(TAG, "LongClick") // doesn't fire
    when (v){
        is CircleImageView -> {
            v.elevation = 20f
            v.borderColor = ContextCompat.getColor(this, R.color.colorPrimary)
            ready = true
        }
    }
    return true
}

onTouch

override fun onTouch(v: View?, event: MotionEvent?): Boolean {

    if (v is CircleImageView){
        when (event?.action){
            ACTION_DOWN -> {
                v.alpha = 0.7f
            }
            ACTION_MOVE -> {
                if (!ready) return false

                ...
            }
    return true
}

Any idea?

Upvotes: 0

Views: 136

Answers (1)

Taranjit Kaur
Taranjit Kaur

Reputation: 216

Because the event is already being consumed by onTouch .Theoratically, If you can return false from touch(not consumed for long click), it should work

Upvotes: 2

Related Questions