constraintlayout icon indicating copy to clipboard operation
constraintlayout copied to clipboard

when setOnClickListener on views in motionlayout it can't be drag.

Open showwiki opened this issue 3 years ago • 10 comments

when setOnClickListener on views in motionlayout it can't be drag. it seems MotionEvent.ACTION_MOVE event being ignore on the views.

showwiki avatar Jan 27 '22 13:01 showwiki

Try the flag in the transition motion:transitionFlags="onInterceptTouchReturnSwipe"

jafu888 avatar Jan 27 '22 18:01 jafu888

which version of motionlayout have this selection? in my motionlayout 2.1.0 , motion:transitionFlags just have "disableIntraAutoTransition" or "beginOnFirstDraw" or "none"

        motion:transitionFlags="beginOnFirstDraw"
        motion:motionInterpolator="linear"

showwiki avatar Jan 28 '22 13:01 showwiki

2.1.3

jafu888 avatar Jan 28 '22 14:01 jafu888

it not works , i tested the official demo , link is here

when i add the underlying codes to DemoActivity.kt , the picture can't drag. Is there any other way to add click event in motionlayout by code ?

if (layout == R.layout.motion_05_imagefilter) {
            findViewById<ImageFilterView>(R.id.image).setOnClickListener {
                Toast.makeText(this, "show toast", Toast.LENGTH_LONG).show()
            }
        }

showwiki avatar Jan 30 '22 16:01 showwiki

hello, Is there any progress?

showwiki avatar Feb 11 '22 07:02 showwiki

finally, when MotionEvent is moving, it shouldn't allow children view to consume the event, so. i intercept the motion event as underlying code in a custom motionlayout . it works now.

 private var touchDownX = 0f
    private var touchDownY = 0f
    private var mScrolling = false
    private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                touchDownX = event.x
                touchDownY = event.y
                mScrolling = false
            }
            MotionEvent.ACTION_MOVE -> mScrolling = abs(touchDownY - event.y) >= touchSlop
            MotionEvent.ACTION_UP -> mScrolling = false
        }
        return if (mScrolling) true else super.onInterceptTouchEvent(event)
    }

showwiki avatar Feb 13 '22 07:02 showwiki

@showwiki Thank you. Worked for me. Here is a full implementation in Kotlin:

class ClickFriendlyMotionLayout @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
): MotionLayout(context, attributeSet, defStyleAttr) {

    private var touchDownX = 0f
    private var touchDownY = 0f
    private var mScrolling = false
    private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                touchDownX = event.x
                touchDownY = event.y
                mScrolling = false
            }
            MotionEvent.ACTION_MOVE -> mScrolling = abs(touchDownY - event.y) >= touchSlop
            MotionEvent.ACTION_UP -> mScrolling = false
        }
        return if (mScrolling) true else super.onInterceptTouchEvent(event)
    }
}

vvinogra avatar Sep 13 '22 12:09 vvinogra

Unfortunately, this fix, brakes scrolling in the recycler view/view pager.

isamotiuc avatar Mar 22 '23 09:03 isamotiuc

Upon further investigation and testing, we have come to the realization that the proposed fix for the issue at hand unfortunately results in an unintended and undesirable consequence. Specifically, it has been observed that the scrolling functionality within the recycler view or view pager is now broken due to the implementation of the fix.

DanieleRigo avatar Mar 23 '23 12:03 DanieleRigo

@isamotiuc @DanieleRigo did you guys find any solution for the recyclerview scrolling? Thanks

shubhamvashisht avatar Dec 13 '23 07:12 shubhamvashisht