如何解决将stateListAnimator添加到Recyclerview项时出现问题
我正在尝试在recyclerview上的项目中添加高程动画。我在网上看到了示例,但它们是用于硬卡的。
我向res/animator
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<set>
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="translationZ"
android:valueTo="56dp"
android:valueType="floatType" />
</set>
</item>
<item
android:state_enabled="true"
android:state_pressed="true">
<set>
<objectAnimator
android:duration="@android:integer/config_longAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"
android:valueType="floatType" />
</set>
</item>
</selector>
之后,我处理了每个recyclerview物品,然后单击适配器。
class RecyclerViewAdapterMainDashboard(
private val features: List<MainDashboardModel>
) : RecyclerView.Adapter<RecyclerViewAdapterMainDashboard.DashboardViewHolder>() {
inner class DashboardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageUrlMain: ImageView = itemView.imageCity
val textTitle: TextView = itemView.textTitleCity
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): DashboardViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.main_dashboard_cards,parent,false)
return DashboardViewHolder(itemView)
}
override fun getItemCount(): Int {
return features.size
}
override fun onBindViewHolder(holder: DashboardViewHolder,position: Int) {
val currentItem = features[position]
holder.textTitle.text = currentItem.cityMain
Glide.with(holder.itemView.context)
.load(currentItem.imageUrlMain)
.fitCenter()
.into(holder.imageUrlMain)
holder.itemView.setOnClickListener {
when (currentItem.cityId) {
1 -> {
val stateListAnimator: StateListAnimator =
AnimatorInflater.loadStateListAnimator(
it.context,R.animator.elevation_on_touch
)
holder.itemView.stateListAnimator = stateListAnimator
val intent = Intent(it.context,CityActivityNewYork::class.java)
it.context.startActivity(intent)
}
2 -> {
val stateListAnimator: StateListAnimator =
AnimatorInflater.loadStateListAnimator(
it.context,CityActivityHawaii::class.java)
it.context.startActivity(intent)
}
3 -> {
val stateListAnimator: StateListAnimator =
AnimatorInflater.loadStateListAnimator(
it.context,CityActivityIstanbul::class.java)
it.context.startActivity(intent)
}
4 -> {
val stateListAnimator: StateListAnimator =
AnimatorInflater.loadStateListAnimator(
it.context,CityActivityBolivia::class.java)
it.context.startActivity(intent)
}
}
}
}
}
我也将这些属性添加到card.xml
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:stateListAnimator="@animator/elevation_on_touch"
问题是当我按下卡时卡没有升高。甚至我写了54dp来查看它们是否在移动,但是什么也没发生。我应该在recyclerview组件本身中添加一些内容吗?我在做什么错了?
解决方法
问题出在您的selector
中,您已经为两个选择器状态分别设置了state_enabled="true"
和state_pressed="true"
。但是state_pressed
仅对于按下状态应为true
。正确的选择器应如下所示:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true">
...
</item>
<item
android:state_pressed="false">
.....
</item>
</selector>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。