【问题标题】:Glitchy movement when using animateLayoutChanges on a ConstraintLayout在 ConstraintLayout 上使用 animateLayoutChanges 时出现故障移动
【发布时间】:2019-09-23 20:28:28
【问题描述】:

我想实现一个扩展的 CardView,我通过包含我想显示/隐藏的视图并将可见性设置为 View.GONE 来做到这一点。 但是由于某种原因,CardView 在展开和折叠时会卡顿。
It does this.
我尝试通过添加一个自定义转换来解决它,该转换可以缩放 op 和 down,但它给了我完全相同的结果,只是慢了一点。
我真的找不到这类东西的任何文档,而且我发现的东西不相关,比如活动转换等......
我确实看过 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:用于跟踪视图扩展状态,“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);
    }

}

【问题讨论】:

  • 您是否在 Flip() 中以编程方式更改 shoppinglist_item_list 的宽度?这可能是问题所在。
  • @Andro 不,我不是,flip 将布尔值设置为“!value”,这有助于我跟踪扩展状态。这就是我使用 ExpansionStateList 的目的。我已将此代码添加到我的问题中。
  • 尝试将 shoppinglist_item_list 宽度设置为 match_parent 而不是 0dp。我认为这可能是因为围绕内容的布局。由于包装器具有match_parent 属性,因此不应该再次出现这种情况。另一种方法可能是使用 Animator。
  • @Andro 好建议,可惜没用。我目前正在尝试自定义动画师,但我并没有寄予厚望。我发现即使我删除了所有视图并将列表保留为唯一视图。卡片视图仍然困扰着我。我不知道为什么。如果一切都失败了,我有一个使用折叠单元库的备份计划。
  • 最短的方法是删除android:animateLayoutChanges="true"TransitionManager.beginDelayedTransition(holder.contentContainer);。这将在没有动画的情况下扩展视图。对于动画,请使用 stackoverflow.com/questions/32835397/…

标签: java android animation android-constraintlayout


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多