MotionLayout with recyclerview make onBindViewHolder get called many many times

225 views Asked by At

I have MotionLayout as

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/layout_background"
    android:clickable="true"
    android:focusable="true"
    app:layoutDescription="@xml/collapsing_toolbar_match_profile
    android:transitionGroup="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/small_header_padding_top"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <FrameLayout
            android:id="@+id/frame_back"
            android:layout_width="60.33dp"
            android:layout_height="40.33dp"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="20dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">


            <ImageButton
                android:id="@+id/btn_back"
                style="@style/Base.Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/back_left_space"
                android:src="@drawable/ic_back" />
        </FrameLayout>

    
        </FrameLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle_tabs"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:transitionGroup="true"
        app:layout_constraintBottom_toBottomOf="@+id/imageView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />




    <ImageView
        android:id="@+id/imgview_team1_match_profile"
        android:layout_width="@dimen/_42sdp"
        android:layout_height="@dimen/_39sdp"
        android:layout_marginStart="@dimen/_33sdp"
        android:layout_marginTop="10dp"
        android:scaleType="fitCenter"
        android:transitionGroup="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/constraintLayout" />


    <TextView
        android:id="@+id/txv_title_match_profile1"
        android:layout_width="@dimen/_68sdp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="@dimen/_20sdp"
        android:layout_marginTop="@dimen/_3sdp"
             app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imgview_team1_match_profile" />

    <!--</FrameLayout>-->


    <ImageView
        android:id="@+id/imgview_team2_match_profile"
        android:layout_width="@dimen/_42sdp"
        android:layout_height="@dimen/_39sdp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="@dimen/_33sdp"
        android:scaleType="fitCenter"
        android:transitionGroup="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/constraintLayout" />

    <TextView
        android:id="@+id/txv_title_match_profile2"
        android:layout_width="@dimen/_68sdp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/_3sdp"
        android:layout_marginEnd="@dimen/_20sdp"
        android:ellipsize="end"
        android:gravity="center_horizontal"
             app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imgview_team2_match_profile" />

    <!--current-->
    <LinearLayout
        android:id="@+id/linear_match_profile_father"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
        app:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        app:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        app:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile">


        <TextView
            android:id="@+id/txv_time_match_profile"
            android:layout_width="37dp"
            android:layout_height="37dp"
            android:background="@drawable/time_match_profile_background"
            android:gravity="center"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/_9sdp"
            android:visibility="gone" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/txv_time_match_current_state_profile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:textColor="@color/white"
                android:textSize="@dimen/_9sdp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/txv_time_match_current_now_profile"
                android:layout_width="10dp"
                android:layout_height="10dp"
                android:layout_gravity="center"
                android:layout_marginStart="5dp"
                android:layout_marginTop="@dimen/_1sdp"
                android:layout_marginEnd="5dp"
                android:background="@drawable/green_frame_background"
                android:gravity="center"
                android:text=""
                android:textColor="@color/green"
                android:textSize="@dimen/_9sdp"
                android:visibility="gone" />


        </LinearLayout>

    </LinearLayout>

    <!--next-->
    <LinearLayout
        android:id="@+id/linear_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
        app:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        app:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        app:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile">



        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingStart="@dimen/_1sdp"
            android:paddingEnd="@dimen/_1sdp">

            <TextView
                android:id="@+id/txv_time_match_profile2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center|bottom"
                android:textColor="#FFF"
                android:textSize="@dimen/_12sdp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/txv_time_am"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center|bottom"
                android:paddingStart="@dimen/_1sdp"
                android:textColor="#FFF"
                android:textSize="@dimen/_9sdp"
                android:visibility="gone" />

        </LinearLayout>


        <TextView
            android:id="@+id/txv_remine_match_profile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:lineSpacingExtra="4dp"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/_12sdp"
            android:textStyle="bold"
            android:visibility="gone" />
    </LinearLayout>

    <!--done-->

    <FrameLayout

        android:id="@+id/frame_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
        app:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        app:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        app:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile">

        <TextView
            android:id="@+id/txv_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/finished"
            android:textColor="#FFF"
            android:textSize="@dimen/_9sdp"
            android:visibility="gone" />
    </FrameLayout>


    <TextView
        android:id="@+id/txv_goals_team2_match_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_45sdp"
        android:textColor="#FFF"
        android:textSize="@dimen/_14sdp"
        app:layout_constraintBottom_toBottomOf="@+id/view_center"
        app:layout_constraintStart_toEndOf="@+id/view_center"
        app:layout_constraintTop_toTopOf="@+id/view_center" />

    <TextView
        android:id="@+id/txv_goals_team1_match_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/_45sdp"
        android:textColor="#FFF"
        android:textSize="@dimen/_14sdp"
        app:layout_constraintBottom_toBottomOf="@+id/view_center"
        app:layout_constraintEnd_toStartOf="@+id/view_center"
        app:layout_constraintTop_toTopOf="@+id/view_center" />


    <View
        android:id="@+id/view_center"
        android:layout_width="1dp"
        android:layout_height="1dp"
        app:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
        app:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        app:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        app:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />



    <com.duolingo.open.rtlviewpager.RtlViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:transitionGroup="true"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintTop_toBottomOf="@+id/imageView4" />


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

With identical viewpager items that contain a recyclerview;

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swip"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:transitionGroup="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraint_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/layout_background"
        android:transitionGroup="true">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingStart="@dimen/margin"
            android:paddingTop="@dimen/margin"

            android:paddingEnd="@dimen/margin"
            android:transitionGroup="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/txv_note"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@drawable/item_background_drawable_accent"
                android:gravity="center"
                android:lineSpacingExtra="@dimen/_4sdp"
                android:padding="12dp"
                android:textColor="@color/white"
                android:textSize="@dimen/_10sdp"
                android:textStyle="bold"
                android:visibility="gone" />


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycle_news"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:alpha="0.1"
                android:transitionGroup="true"
                android:visibility="visible" />
        </LinearLayout>


      
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

I have a motion scene that collapses the image when scrolled up (the configurations for this are correct).

<?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:duration="1000"
        motion:motionInterpolator="linear"
        motion:constraintSetStart="@id/start">

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:onTouchUp="autoComplete"
            motion:touchAnchorId="@id/viewpager"
            motion:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="@dimen/big_header"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@+id/imgview_team1_match_profile"
            android:layout_width="@dimen/_42sdp"
            android:layout_height="@dimen/_39sdp"
            android:layout_marginStart="@dimen/_33sdp"
            android:layout_marginTop="10dp"
            motion:layout_constraintStart_toStartOf="parent"


            motion:layout_constraintTop_toBottomOf="@id/constraintLayout" />
        <Constraint
            android:id="@+id/txv_title_match_profile1"
            android:layout_width="@dimen/_68sdp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="@dimen/_20sdp"
            android:layout_marginTop="@dimen/_3sdp"
            android:paddingStart="4dp"
            android:paddingTop="1dp"
            android:paddingEnd="1dp"
            android:paddingBottom="1dp"
            android:scaleX="1"
            android:scaleY="1"

            android:textSize="@dimen/_10sdp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/imgview_team1_match_profile" />

        <Constraint
            android:id="@+id/imgview_team2_match_profile"
            android:layout_width="@dimen/_42sdp"
            android:layout_height="@dimen/_39sdp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="@dimen/_33sdp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/constraintLayout" />
        <Constraint
            android:id="@+id/txv_title_match_profile2"
            android:layout_width="@dimen/_68sdp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/_3sdp"
            android:layout_marginEnd="@dimen/_20sdp"
            android:paddingStart="4dp"
            android:paddingTop="1dp"
            android:paddingEnd="1dp"
            android:paddingBottom="1dp"
            android:scaleX="1"
            android:scaleY="1"
            android:textSize="@dimen/_10sdp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/imgview_team2_match_profile" />


        <Constraint
            android:id="@+id/linear_match_profile_father"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            motion:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
            motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
            motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
            motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />

        <Constraint
            android:id="@+id/linear_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            motion:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
            motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
            motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
            motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />

        <Constraint
            android:id="@+id/frame_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            motion:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
            motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
            motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
            motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />

        <Constraint
            android:id="@+id/view_center"
            android:layout_width="1dp"
            android:layout_height="1dp"
            motion:layout_constraintBottom_toBottomOf="@+id/txv_title_match_profile1"
            motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
            motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
            motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />

        <Constraint
            android:id="@+id/shimmer_view_container"
            android:layout_width="match_parent"
            android:layout_height="75.67dp"
            android:layout_marginStart="@dimen/_33sdp"
            android:layout_marginEnd="@dimen/_33sdp"
            android:layout_marginBottom="60dp"
            motion:layout_constraintBottom_toTopOf="@+id/viewpager"
            motion:layout_constraintEnd_toEndOf="@+id/imageView4"
            motion:layout_constraintStart_toStartOf="@+id/imageView4" />


    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="177dp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@+id/imgview_team1_match_profile"
            android:layout_width="@dimen/_25sdp"
            android:layout_height="@dimen/_21sdp"
            android:layout_marginStart="@dimen/_58sdp"
            android:layout_marginTop="5dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/constraintLayout" />
        <Constraint
            android:id="@+id/txv_title_match_profile1"
            android:layout_width="@dimen/_68sdp"
            android:layout_height="15dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="@dimen/_36sdp"
            android:layout_marginTop="0dp"
            android:padding="2dp"
            android:scaleX="0"
            android:scaleY="0"


            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/imgview_team1_match_profile"

            />


        <Constraint
            android:id="@+id/imgview_team2_match_profile"
            android:layout_width="@dimen/_25sdp"
            android:layout_height="@dimen/_21sdp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="@dimen/_58sdp"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/constraintLayout" />


        <Constraint
            android:id="@+id/txv_title_match_profile2"
            android:layout_width="@dimen/_68sdp"
            android:layout_height="15dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="0dp"
            android:layout_marginEnd="@dimen/_36sdp"
            android:padding="2dp"
            android:scaleX="0"

            android:scaleY="0"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/imgview_team2_match_profile" />


        <Constraint
            android:id="@+id/linear_match_profile_father"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            motion:layout_constraintBottom_toBottomOf="@+id/imgview_team1_match_profile"
            motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
            motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
            motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />

        <Constraint
            android:id="@+id/linear_next"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        motion:layout_constraintBottom_toBottomOf="@+id/imgview_team1_match_profile"
        motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />
    <Constraint
        android:id="@+id/frame_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintBottom_toBottomOf="@+id/imgview_team1_match_profile"
        motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />
    <Constraint
        android:id="@+id/view_center"
        android:layout_width="1dp"
        android:layout_height="1dp"
        motion:layout_constraintBottom_toBottomOf="@+id/imgview_team1_match_profile"
        motion:layout_constraintEnd_toStartOf="@+id/imgview_team2_match_profile"
        motion:layout_constraintStart_toEndOf="@+id/imgview_team1_match_profile"
        motion:layout_constraintTop_toTopOf="@+id/imgview_team1_match_profile" />


    <Constraint
        android:id="@+id/shimmer_view_container"
        android:layout_width="match_parent"
        android:layout_height="75.67dp"
        android:layout_marginStart="@dimen/_33sdp"
        android:layout_marginEnd="@dimen/_33sdp"
        android:layout_marginBottom="20dp"
        motion:layout_constraintBottom_toTopOf="@+id/viewpager"
        motion:layout_constraintEnd_toEndOf="@+id/imageView4"
        motion:layout_constraintStart_toStartOf="@+id/imageView4" />
</ConstraintSet>

Works just fine, and transitions as I'd expect. But the problem is: When I use normal constraintLayout or MotionLayout without

app:layoutDescription="@xml/collapsing_toolbar_match_profile

the adapter onBindViewHolder got called about 9 times and it's good, but when I use MotionLayout it got called about 300 times!

Thank you in advance

EDIT:

I found the problem. It caused by nested ConstraintLayout. My code had too many nested ConstraintLayout and that caused the bug.

0

There are 0 answers