Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MotionLayout setGuidelinePercent not working on alpha4 release

I have a MotionLayout for a swipe transition in my app. Currently I just updated from 2.0.0-alpha3 to 2.0.0-alpha4 release and some things are not working as before. There is "myView" which is a layout that should expand from the bottom of the screen to a header view. It has a constraint top to a Guideline which I need to control programmatically (based on some user actions).

There were no other changes in the code, so downgrading to alpha3 makes everything work as expected.

The code is pretty basic:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000"
        motion:interpolator="linear">
        <!-- I used motion:motionInterpolator="linear" for alpha4-->

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/myView"
            motion:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="@id/myGuideline" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/someHeaderView" />
    </ConstraintSet>
</MotionScene>

The problem is that in release alpha3, I could use something like this(and it worked):

myMotionLayout.getConstraintSet(R.id.start)?.setGuidelinePercent(R.id.myGuideline, guidelinePercentage)

Now that I upgraded to 2.0.0-alpha4, this is no longer working and I can't understand why. It always takes the default one, from the xml and is never changed whenever I try to do it dynamically.

What I'm trying to do is to change the starting point of the transition based on some dynamic data (available at the beginning of the Fragment or when the user is returning to it). If you have a better idea of how I can implement this using MotionLayout, please give me a hint.

Gradle implementation:

implementation "com.android.support.constraint:constraint-layout:2.0.0-alpha4"
like image 470
MihaiV Avatar asked Apr 17 '19 07:04

MihaiV


1 Answers

I noticed a similar regression introduced in the alpha4 release. Through trial and error, and the following bug report, I was able to get my ConstraintSet changes to be applied programatically in a MotionLayout (beta1 release). It appears the clone method does not work for MotionLayout, and getConstraintSet should be used instead (as you've done). Looks like the step you're missing is constraintSet.applyTo(motion_layout). I also needed to re-apply the MotionLayout description xml at the end.

implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta1"

Steps:

val constraintSet = motion_layout.getConstraintSet(R.id.motion_layout)
constraintSet.clear(R.id.txt_view, ConstraintSet.BOTTOM)
constraintSet.applyTo(motion_layout)
// If using a MotionLayout description xml file, re-apply it
motion_explore_article.loadLayoutDescription(R.xml.slide_over_image)

Bug report: https://issuetracker.google.com/issues/131874827

like image 103
Luke Simpson Avatar answered Nov 20 '22 00:11

Luke Simpson