在ConstraintLayout上使用animateLayoutChanges时出现毛刺运动

时间:2018-04-15 21:13:31

标签: java android animation android-constraintlayout

我想实现扩展的CardView,我通过包含我想要显示/隐藏的视图并将可见性设置为View.GONE来实现。 但由于某些原因,CardView在扩展和折叠时会断断续续 It does this
我尝试通过添加自定义转换来修复它,可以缩放操作和向下,但它给我的结果完全相同,只是稍慢一点。
我真的找不到这类东西的任何文档,我找到的东西是不相关的,比如活动转换等...... 我确实看过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);
        }

    }

}

ExpansionStateList:为了跟踪视图扩展状态,&#39; true =展开&#39;。

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

}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。通过将经过/可见的布局的宽度设置为“ match_parent”而不是“ 0dp”并带有约束,我获得了更好的结果。这样可以固定动画以进行扩展,但不能缩小。由于时间不足,我最终使用了RelativeLayoutt,但是我敢肯定,通过对视图宽度进行一些试验,问题可以得到解决。我希望这对某人有帮助,但我还没有在线找到其他解决方案。