@kenya
Для реализации плавного перехода даты в RecyclerView в Kotlin можно использовать анимацию смены текста при обновлении элемента RecyclerView. Для этого необходимо выполнить следующие шаги:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
android { ... defaultConfig { ... minSdkVersion 21 targetSdkVersion 30 } ... } dependencies { ... implementation 'androidx.recyclerview:recyclerview:1.2.0' implementation 'androidx.cardview:cardview:1.0.0' } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:padding="16dp"> <TextView android:id="@+id/date_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> </androidx.cardview.widget.CardView> |
1 2 3 4 5 6 7 8 9 |
import android.view.View import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.date_item.view.* class DateItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(date: String) { itemView.date_text_view.text = date } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.animation.ObjectAnimator import android.animation.ValueAnimator import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.RecyclerView class DateItemAnimator : DefaultItemAnimator() { override fun animateChange( oldHolder: RecyclerView.ViewHolder, newHolder: RecyclerView.ViewHolder, fromX: Int, fromY: Int, toX: Int, toY: Int ): Boolean { if (oldHolder.itemViewType == newHolder.itemViewType) { val anim = ObjectAnimator.ofFloat(newHolder.itemView, "alpha", 0f, 1f) anim.duration = 250 anim.addListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator?) { dispatchAnimationFinished(newHolder) } }) anim.start() return true } return super.animateChange(oldHolder, newHolder, fromX, fromY, toX, toY) } override fun canReuseUpdatedViewHolder(viewHolder: RecyclerView.ViewHolder, payloads: MutableList<Any>): Boolean { return true } override fun getChangePayload(oldHolder: RecyclerView.ViewHolder, newHolder: RecyclerView.ViewHolder): Any? { return newHolder.itemView.context.getString(R.string.date_changed) } override fun animateChange( oldHolder: RecyclerView.ViewHolder, newHolder: RecyclerView.ViewHolder, preInfo: ItemHolderInfo, postInfo: ItemHolderInfo ): Boolean { val animators = ArrayList<Animator>() if (preInfo.left != postInfo.left || preInfo.top != postInfo.top) { val xAnimator = ObjectAnimator.ofFloat(newHolder.itemView, "translationX", preInfo.left.toFloat(), |
@kenya
postInfo.left.toFloat()) val yAnimator = ObjectAnimator.ofFloat(newHolder.itemView, "translationY", preInfo.top.toFloat(), postInfo.top.toFloat()) animators.add(xAnimator) animators.add(yAnimator) } if (animators.isEmpty()) { dispatchAnimationFinished(newHolder) } else { val animatorSet = ValueAnimator() animatorSet.playTogether(animators) animatorSet.addListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator?) { dispatchAnimationFinished(newHolder) } }) animatorSet.start() } return true } }
Использовать DateItemAnimator в RecyclerView:
1 2 3 4 5 6 7 8 9
recyclerView.apply { itemAnimator = DateItemAnimator() adapter = adapter }
Это позволит реализовать плавный переход даты при обновлении элемента списка RecyclerView.