【发布时间】:2018-03-12 05:00:09
【问题描述】:
我关注了Nick Butcher's Material Improvements I/O 2016 talk,大约在 6:00,他开始谈论动画列表项。我已经完全按照他的做法实现了该功能,并且绑定的变化动画正确,但颜色变化没有动画,尽管他明确表示会这样做:
这就是代码的样子:
这是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>
【问题讨论】:
-
Gif 不起作用:-/
-
@azizbekian 很遗憾听到这个消息,也许它不起作用,因为您正在移动设备上查看...
-
背景可绘制对象与
ConstraintLayout一起工作,无需调用TransitionManager。我想知道您使用的是哪个视图组。你能发布你的布局吗?
标签: android android-layout android-recyclerview material-design android-animation