将 stateListAnimator 添加到 Recyclerview 项目时出现问题

Problem when Adding stateListAnimator to Recyclerview items

我正在尝试为我在 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,
                            R.animator.elevation_on_touch
                        )
                    holder.itemView.stateListAnimator = stateListAnimator
                    val intent = Intent(it.context, CityActivityHawaii::class.java)
                    it.context.startActivity(intent)

                }
                3 -> {
                    val stateListAnimator: StateListAnimator =
                        AnimatorInflater.loadStateListAnimator(
                            it.context,
                            R.animator.elevation_on_touch
                        )
                    holder.itemView.stateListAnimator = stateListAnimator
                    val intent = Intent(it.context, CityActivityIstanbul::class.java)
                    it.context.startActivity(intent)


                }
                4 -> {
                    val stateListAnimator: StateListAnimator =
                        AnimatorInflater.loadStateListAnimator(
                            it.context,
                            R.animator.elevation_on_touch
                        )
                    holder.itemView.stateListAnimator = stateListAnimator
                    val intent = Intent(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 组件本身添加一些东西吗?我做错了什么?

project repo

问题出在您的 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>