gpt4 book ai didi

android - TransitionManager 不动画颜色变化

转载 作者:搜寻专家 更新时间:2023-11-01 07:45:02 24 4
gpt4 key购买 nike

我关注了Nick Butcher's Material Improvements I/O 2016 talk ,大约在 6:00,他开始谈论动画列表项。我已经完全按照他的方式实现了该功能,并且绑定(bind)更改正确地设置了动画,但是颜色更改没有设置动画,尽管他明确表示他们会:

animation

代码如下:

这是 RecyclerView.Adapter 类的相关部分:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item: Pair<String, String> = items[position]

holder.title.text = item.first
holder.subtitle.text = item.second

val isExpanded = position == expandedPosition
holder.subtitle.visibility = if (isExpanded) View.VISIBLE else View.GONE
holder.itemView.isActivated = isExpanded
holder.itemView.setOnClickListener {
expandedPosition = if (isExpanded) -1 else position
TransitionManager.beginDelayedTransition(recyclerView)
notifyDataSetChanged()
}
}

这是我为项目使用的布局。 ConstraintLayout 对于当前的布局设置有点矫枉过正,但我​​减少了布局以创建一个最小的示例。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_background"
android:stateListAnimator="@animator/item_elevation"
android:padding="8dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
tools:text="Title 1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Subtitle 1"
app:layout_constraintTop_toBottomOf="@id/title"
app:layout_constraintStart_toStartOf="parent"
/>

</android.support.constraint.ConstraintLayout>

这是我用于项目布局的背景:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_mediumAnimTime"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">

<item android:state_activated="true"
android:drawable="@color/colorAccent" />

<item android:drawable="@color/colorPrimaryDark" />

</selector>

最佳答案

确保您的数据集具有稳定的 ID 并调用 Recycler.Adapter#setHasStableIds(true)。看 setHasStableIds() .

您还需要覆盖 getItemId()在适配器中返回一个稳定的 id。另见 notifyDataSetChanged有关稳定 ID 的简短讨论。

RecyclerView will attempt to synthesize visible structural change events for adapters that report that they have stable IDs when this method is used. This can help for the purposes of animation and visual object persistence but individual item views will still need to be rebound and relaid out.

这是一个将稳定 ID 设置为 false 的演示:

enter image description here

并将稳定的 ID 设置为 true。我在颜色变化上设置了很长的过渡时间,所以它会很明显。

enter image description here

RecyclerViewAdapter.java

这是演示中使用的适配器。 TransitionManager.beginDelayedTransition(mRecyclerView) 已被删除,让 RecyclerView 更好地处理动画。

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

RecyclerViewAdapter() {
}

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

private int mExpandedPosition = RecyclerView.NO_POSITION;

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final ItemViewHolder vh = (ItemViewHolder) holder;
final TextView subTitle = vh.mSubTitle;

vh.mTitle.setText(titleForPosition(position));
subTitle.setText(subTitleForPosition(position));

final boolean isExpanded = position == mExpandedPosition;

subTitle.setVisibility((isExpanded) ? View.VISIBLE : View.GONE);
holder.itemView.setActivated(isExpanded);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? RecyclerView.NO_POSITION : vh.getAdapterPosition();
notifyDataSetChanged();
}
});
}

private String titleForPosition(int position) {
return "Title " + position;
}

private String subTitleForPosition(int position) {
return "Subtitle " + position;
}

@Override
public int getItemCount() {
return 5;
}

@Override
public long getItemId(int position) {
return titleForPosition(position).hashCode();
}

static class ItemViewHolder extends RecyclerView.ViewHolder {
private final TextView mTitle;
private final TextView mSubTitle;

ItemViewHolder(View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.title);
mSubTitle = itemView.findViewById(R.id.subtitle);
}
}

@SuppressWarnings("unused")
private final static String TAG = "RecyclerViewAdapter";
}

关于android - TransitionManager 不动画颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46503216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com