#android #kotlin #android-recyclerview #android-animation
#Android #kotlin #android-recyclerview #android-анимация
Вопрос:
Я пытаюсь добавить анимацию возвышения к своим элементам в 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? Что я делаю не так?
Ответ №1:
Проблема в вашем 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>
Комментарии:
1. К сожалению, это не сработало. Я попытался добавить animator в сам recycler view, также не сработало. Я добавлю ссылку github на вопрос, можете ли вы проверить, пожалуйста?