Saturday, 27 February 2021

MotionLayout Group Visibility Issue

I have a Constraint Layout which contains a RecyclerView, a TextInputEditText, a Constraint Group (which handles visibility of the RecyclerView and TextInputEditText, an ImageView and a LottieAnimationView.

It works as this:

  • On startup, only the LottieAnimation is visible until my LiveData fetches data from repo and updates successfully.

  • After data is fetched, the LottieView is hidden and the Group visibility is shown which in turns shows the RecyclerView and TextInputEditText.

  • The ImageView is shown only if there's an error and then all other views are hidden except the ImageView.

Now i want that when i scroll my RecyclerView, the TextInputEditText also scrolls. (I can't use NestedScrollView due to pagination lag when scroll).

I made use of MotionLayout. Except it is not working properly. My visibility is broken. My Lottie Animation is staying on the screen. My layouts are as such:

fragment.xml:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="ViewModel"
            type="...AllServicesViewModel" />

        <import type="android.view.View" />

    </data>

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/Root_All_Services"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/scene_all_services_header">

        <ImageView
            android:id="@+id/Illus_Error"
            android:layout_width="350dp"
            android:layout_height="350dp"
            android:layout_margin="16dp"
            android:contentDescription="@string/icd_error_services_unavailable"
            android:scaleType="centerCrop"
            android:src="@drawable/il_error"
            android:visibility="@{ViewModel.message != null ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@drawable/il_error" />

        <androidx.constraintlayout.widget.Group
            android:id="@+id/Group_Main_Content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{ViewModel.loading || ViewModel.message != null ? View.GONE : View.VISIBLE}"
            app:constraint_referenced_ids="TextField_Search,RecyclerView_Services" />

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/TextField_Search"
            style="@style/Mes.TextField.Search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:drawablePadding="-24dp"
            android:hint="@string/action_search"
            android:text="@={ViewModel.searchQuery}"
            android:imeOptions="actionSearch"
            android:padding="24dp"
            android:singleLine="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/RecyclerView_Services"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:clipToPadding="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/TextField_Search" />

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/Anim_Loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{!ViewModel.loading || ViewModel.message != null? View.GONE : View.VISIBLE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:lottie_autoPlay="true"
            app:lottie_loop="true"
            app:lottie_rawRes="@raw/raw_anim_loading_services" />

    </androidx.constraintlayout.motion.widget.MotionLayout>

</layout>

scene_all_services_header.xml

<?xml version="1.0" encoding="utf-8"?>

<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">
        <OnSwipe
            motion:dragDirection="dragUp"
            motion:onTouchUp="stop"
            motion:touchAnchorId="@+id/TextField_Search" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/TextField_Search"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

        <ConstraintOverride
            android:id="@+id/RecyclerView_Services"
            motion:visibilityMode="ignore" />

        <Constraint
            android:id="@+id/Anim_Loading"
            motion:visibilityMode="ignore" />

        <Constraint
            android:id="@+id/Illus_Error"
            motion:visibilityMode="ignore" />

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/TextField_Search"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            motion:layout_constraintBottom_toTopOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent" />

        <ConstraintOverride
            android:id="@+id/RecyclerView_Services"
            motion:visibilityMode="ignore" />

        <Constraint
            android:id="@+id/Anim_Loading"
            motion:visibilityMode="ignore" />

        <Constraint
            android:id="@+id/Illus_Error"
            motion:visibilityMode="ignore" />

    </ConstraintSet>

</MotionScene>

Below are samples of how it worked before and after adding MotionLayout

Before

before

After

after

Can someone please help me out ?



from MotionLayout Group Visibility Issue

No comments:

Post a Comment