使用动画布局时出现故障移动约束布局更改



我想实现一个扩展的 CardView,我通过包含我要显示/隐藏的视图并将可见性设置为 View.GONE 来做到这一点。但由于某种原因,CardView 在扩展和折叠时会卡顿。
它这样做。
我试图通过添加一个缩放和缩小操作的自定义过渡来修复它,但它给了我完全相同的结果,只是慢了一点。
我真的找不到这类东西的任何文档,而且我发现的东西不相关,比如活动转换等......
我确实看过LayoutTransition,但同样没有太大帮助。
我不知道为什么会发生这种情况,朝着正确的方向推动将不胜感激!

干杯

这是我的布局:

<?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>

和我的适配器:

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);
        }
    }
}
展开状态

列表:为了跟踪视图展开状态,"true = 展开"。

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);
    }
}

我遇到了同样的问题。通过将消失/可见布局的宽度设置为"match_parent"而不是"0dp",并限制两侧,我获得了更好的结果。这修复了扩展的动画,但未修复缩减。由于时间不够,我最终使用了RelativeLayoutt,但我确信通过对视图宽度进行一些实验,可以解决问题。我希望这对某人有所帮助,我还没有在网上找到任何其他解决方案。

最新更新