将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组件本身中添加一些内容吗?我在做什么错了?

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>