Item not updated in recyclerView when using motionLayout

I want to implement collapse/expand inside an item from my recycler view. An item of recycler view look like this:
enter image description here

When I click on expandCollapseImageView, textView should be collapsed or expanded depending on its state.
When I click on the showImageButton, imageView should be visible, otherwise hidden.
To implement this feature, expand / collapse, I am using motionLayout (I know there is another solution for this without using motionLayout, I just want to play around with motionLayout).
When I add motionLayout the collapse / expand functionality works fine, but when I click on showImageButton, imageView does not appear! before adding motionLayout it was working fine. I don’t know why when I add motionLayout the button click doesn’t show the image. I’m trying to remove motionLayout and it works! the problem is therefore related to motionLayout.

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/training_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:listitem="@layout/item"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

item.xml

<androidx.constraintlayout.motion.widget.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutDescription="@xml/item_scene">


    <Button
        android:id="@+id/show_image_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?colorPrimary"
        android:minWidth="40dp"
        android:minHeight="40dp"
        android:padding="4dp"
        android:text="Show Image"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="36dp"
        android:minHeight="36dp"
        android:padding="6dp"
        android:src="@drawable/ic_solved"
        app:layout_constraintStart_toEndOf="@id/show_image_button"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/collapse_expand_image_view"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:minWidth="40dp"
        android:minHeight="40dp"
        android:padding="4dp"
        android:src="@drawable/ic_arrow_down"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:typeface="monospace"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/show_image_button" />

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

Model.java

public class Model {

    private String text;
    private Boolean show;

    public Model(String text, Boolean show) {
        this.text = text;
        this.show = show;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setShow(Boolean show) {
        this.show = show;
    }

    public String getText() {
        return text;
    }

    public Boolean getShow() {
        return show;
    }
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<Model> mModels;
    private OnClickListener mOnClickListener;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Model model = mModels.get(position);
        holder.textView.setText(model.getText());
        holder.imageView.setVisibility(model.getShow()? View.VISIBLE: View.INVISIBLE);
    }

    @Override
    public int getItemCount() {
        return mModels == null ? 0 : mModels.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView textView;
        private final Button button;
        private final ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.text_view);
            button = itemView.findViewById(R.id.show_image_button);
            imageView = itemView.findViewById(R.id.image_view);

            button.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (mOnClickListener != null && position != RecyclerView.NO_POSITION){
                    mOnClickListener.onClick(position);
                }
            });
        }
    }

    public interface OnClickListener {
        void onClick(int position);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public void setModels(List<Model> models) {
        mModels = models;
        notifyDataSetChanged();
    }
}

item_scene.xml

<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="250">
       <KeyFrameSet>
           <KeyAttribute
               motion:motionTarget="@+id/collapse_expand_image_view"
               motion:framePosition="100"
               android:rotation="90" />
       </KeyFrameSet>
        <OnClick motion:targetId="@+id/collapse_expand_image_view" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:typeface="monospace"
            android:visibility="gone"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/show_image_button"/>
    </ConstraintSet>
</MotionScene>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private List<Model> mModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        MyAdapter myAdapter = new MyAdapter();
        RecyclerView recyclerView = findViewById(R.id.training_recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(
                new DividerItemDecoration(recyclerView.getContext(),
                        layoutManager.getOrientation()));

        recyclerView.setHasFixedSize(true);

        recyclerView.setAdapter(myAdapter);

        mModels = getData();
        myAdapter.setModels(mModels);

        myAdapter.setOnClickListener((position) -> {
            Model model = mModels.get(position);
            model.setShow(true);
            myAdapter.setModels(mModels);
        });
    }

    private List<Model> getData(){
        List<Model> models = new ArrayList<>();
        for (int i = 0; i< 10; i++){
            models.add(new Model("text" + i, false));
        }
        return models;
    }
}

Source: Android Questions

One Reply to “Item not updated in recyclerView when using motionLayout”

  • try adding
    motion:visibilityMode=”ignore”
    to any motion layout children where you don’t want the motion layout to control the visibility (first create the start and end constraints for that view in the scene xml)

LEAVE A COMMENT