4

I want to implement an expanding CardView, i did this by including the view i want to show/hide and setting the visibility to View.GONE. But for some reason the CardView stutters when expanding and collapsing.
It does this.
I tried to fix it by adding a custom transition that scales op and down, but it gives me the exact same result just a little bit slower.
I cant really find any documentation for this type of stuff, and the things that i find are not relevant, like Activity transition etc...
i did have a look at LayoutTransition, but again not much help.
I have no idea why this is happening, a push in the right direction would be appreciated!

Cheers

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/shoppinglist_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="4dp"
    app:cardCornerRadius="4dp"
    app:cardUseCompatPadding="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/shoppinglist_content_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true">

        <TextView
            android:id="@+id/shoppinglist_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="Shoppinglist Name"
            android:textColor="@color/primary"
            android:textSize="16sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/shoppinglist_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:text="Group Name"
            app:layout_constraintEnd_toEndOf="@+id/shoppinglist_name"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/shoppinglist_name"
            app:layout_constraintTop_toBottomOf="@+id/shoppinglist_name" />

        <View
            android:id="@+id/shoppinglist_category_container"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:animateLayoutChanges="true"
            android:background="@color/primary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/shoppinglist_category_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ic_email"
            android:tint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="@+id/shoppinglist_dropdown"
            app:layout_constraintEnd_toEndOf="@+id/shoppinglist_category_container"
            app:layout_constraintStart_toStartOf="@+id/shoppinglist_category_container"
            app:layout_constraintTop_toTopOf="@+id/shoppinglist_category_container" />

        <ImageView
            android:id="@+id/shoppinglist_unchecked_image"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginBottom="8dp"
            android:layout_marginStart="16dp"
            android:tint="@color/shoppinglist_unchecked"
            app:layout_constraintBottom_toBottomOf="@+id/shoppinglist_dropdown"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/ic_close" />

        <ImageView
            android:id="@+id/shoppinglist_checked_image"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginBottom="2dp"
            android:layout_marginStart="16dp"
            android:tint="@color/shoppinglist_checked"
            app:layout_constraintBottom_toTopOf="@+id/shoppinglist_unchecked_image"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/ic_check" />

        <TextView
            android:id="@+id/shoppinglist_unchecked_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="2dp"
            android:text="12"
            android:textSize="11sp"
            app:layout_constraintBottom_toBottomOf="@+id/shoppinglist_unchecked_image"
            app:layout_constraintStart_toEndOf="@+id/shoppinglist_unchecked_image"
            app:layout_constraintTop_toTopOf="@+id/shoppinglist_unchecked_image" />

        <TextView
            android:id="@+id/shoppinglist_checked_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="2dp"
            android:text="12"
            android:textSize="11sp"
            app:layout_constraintBottom_toBottomOf="@+id/shoppinglist_checked_image"
            app:layout_constraintStart_toEndOf="@+id/shoppinglist_checked_image"
            app:layout_constraintTop_toTopOf="@+id/shoppinglist_checked_image" />

        <TextView
            android:id="@+id/shoppinglist_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            android:text="31/12/2018"
            android:textColor="@color/accent"
            app:layout_constraintEnd_toStartOf="@+id/shoppinglist_category_container"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/shoppinglist_dropdown"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginBottom="2dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="115dp"
            android:tint="@color/secondary_text"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@drawable/ic_dropdown" />

        <TextView
            android:id="@+id/shoppinglist_item_list"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/shoppinglist_category_container"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/shoppinglist_dropdown" />


    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

And my Adapter:

package nl.knapper_development.basketbuddy.ui.main.shoppinglist;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.transition.TransitionManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import nl.knapper_development.basketbuddy.R;

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

    private ArrayList<Shoppinglist> data;
    private ExpansionStateList expansionStateList;

    public ShoppinglistAdapter(ArrayList<Shoppinglist> data) {
        this.data = data;
        this.expansionStateList = new ExpansionStateList(data.size());
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Shoppinglist s = data.get(position);
        holder.name.setText(s.getName());
        holder.group.setText(s.getGroup());
        holder.date.setText(s.getDate());
        holder.checked.setText(s.getCheckedAmount().toString());
        holder.unchecked.setText(s.getUncheckedAmount().toString());
        holder.categoryImage.setImageResource(s.getDrawable());
        holder.categoryContainer.setBackgroundColor(holder.itemView.getResources().getColor(s.getColor()));
        holder.dropDown.setOnClickListener(v -> {
            TransitionManager.beginDelayedTransition(holder.contentContainer);
            holder.list.setVisibility(expansionStateList.get(position) ? View.VISIBLE : View.GONE);
            expansionStateList.flip(position);
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private TextView group;
        private TextView date;
        private TextView checked;
        private TextView unchecked;
        private ImageView categoryImage;
        private View categoryContainer;
        private ImageView dropDown;
        private TextView list;
        private ConstraintLayout contentContainer;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.shoppinglist_name);
            group = itemView.findViewById(R.id.shoppinglist_group);
            date = itemView.findViewById(R.id.shoppinglist_date);
            checked = itemView.findViewById(R.id.shoppinglist_checked_amount);
            unchecked = itemView.findViewById(R.id.shoppinglist_unchecked_amount);
            categoryImage = itemView.findViewById(R.id.shoppinglist_category_image);
            categoryContainer = itemView.findViewById(R.id.shoppinglist_category_container);
            dropDown = itemView.findViewById(R.id.shoppinglist_dropdown);
            list = itemView.findViewById(R.id.shoppinglist_item_list);
            contentContainer = itemView.findViewById(R.id.shoppinglist_content_container);
        }

    }

}

ExpansionStateList: For keeping track of view expansion states, 'true = expanded'.

public class ExpansionStateList extends ArrayList<Boolean> {

    public ExpansionStateList(int size) {
        for (int c = 0; c < size; c++) {
            this.add(false);
        }
    }

    public void flip(int index) {
        Boolean v = this.get(index);
        this.set(index, !v);
    }

}
Harjan
  • 533
  • 1
  • 6
  • 24
  • Are you changing width of `shoppinglist_item_list` programatically inside flip()? That might be the problem. – Andro Apr 15 '18 at 21:29
  • @Andro No i am not, flip sets a boolean value to '!value', this helps me keep track of the expanded states. This is what i use the ExpansionStateList for. I have added this code to my question. – Harjan Apr 15 '18 at 21:35
  • Try setting `shoppinglist_item_list` width to `match_parent` instead of 0dp. I think it's probably because of layout wrapping around the content. Which again should not be the case as the wrapper has `match_parent` attribute. Another way could be to use Animator instead. – Andro Apr 15 '18 at 21:50
  • @Andro good suggestion, sadly it didn't work. I am currently trying a custom animator, but I don't have high hopes. I have found that even if I remove all the views and keep the list as the only view. The card view still bugs out on me. I have no idea why. I have a backup plan that is using a folding cell library, if all else fails. – Harjan Apr 15 '18 at 22:21
  • 1
    Shortest way would be to remove `android:animateLayoutChanges="true"` and `TransitionManager.beginDelayedTransition(holder.contentContainer);`. This will expand the view without animation. For animation use this https://stackoverflow.com/questions/32835397/android-property-animation-how-to-increase-view-height – Andro Apr 15 '18 at 23:06

1 Answers1

1

I experienced the same exact problem. I achieved better results by setting the width of the gone/visible layout to "match_parent" instead of "0dp" with constraints to both sides. This fixed the animation for the expansion, but not the reduction. Due to lack of time I ended up using RelativeLayoutt but I'm sure with some experimenting with the width of views the problem could be fixed. I hope this helps someone, I haven't found any other solutions online.