androidx / constraintlayout

ConstraintLayout is an Android layout component which allows you to position and size widgets in a flexible way
Apache License 2.0
1.06k stars 177 forks source link

MotionLayout OnSwipe without TouchAnchorId (support nested scroll) #856

Open vkomysh-vicman opened 4 weeks ago

vkomysh-vicman commented 4 weeks ago

Case: MotionLayout with RecyclerView child. Transition has OnSwipe TouchResponse without TouchAnchorId (whole MotionLayout area):

   <Transition
        android:id="@+id/transition"
        motion:constraintSetStart="@id/start"
        motion:constraintSetEnd="@id/end">
        <OnSwipe />
    </Transition>

All works fine after any MOVE touch event (non-nested scrollable): mAnchorDpDt set by minSize = Math.min(mMotionLayout.getWidth(), mMotionLayout.getHeight())

But, before processTouchEvent MOVE, mAnchorDpDt has incorrect value [0.01, 0.01] and strange behavior in case nested scroll TouchResponse.scrollMove

Temporary fix:

fun MotionScene.Transition.fixAnchorDpDt(minSize: Float) {
    val mAnchorDpDt: FloatArray = touchResponse.getPrivateProperty("mAnchorDpDt") as FloatArray
    mAnchorDpDt[1] = -minSize
}

fun <T : Any> T.getPrivateProperty(variableName: String): Any? {
    return javaClass.getDeclaredField(variableName).let { field ->
        field.isAccessible = true
        return@let field.get(this)
    }
}