首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android循环视图中添加Lottie动画会导致不稳定的结果,包括在错误位置显示视图和scrollToPosition窃听。

在Android循环视图中添加Lottie动画会导致不稳定的结果,包括在错误位置显示视图和scrollToPosition窃听。
EN

Stack Overflow用户
提问于 2021-06-14 19:06:24
回答 1查看 1.2K关注 0票数 3

我创造了一个简单的聊天风格,回收视图,像信使,只有超级简化。但是,当我添加一个lottie动画来显示另一个人正在键入时,它会导致回收视图中的许多不稳定。

当插入新消息时,我使用了scrollToPosition函数,这样它将自动向下滚动到底部,并使消息可见。这是错误的,当我添加lottie文件,它不会正确地滚动到底部,也许只有一半的向下。

此外,我注意到当我向下移动列表时,来自列表索引0的消息将显示为最近的索引,因此它现在被复制了。当我从xml中删除lottie动画时,所有这些异常都会自行修复。但我想有能力显示一个打字动画,就像地球上所有其他聊天应用程序一样。如果有人能帮助我了解出了什么问题,我会非常感激的。

乐蒂动画scrollToPosition失败的视频片段

为了澄清当我从xml中删除lottie动画时,scrollToPosition按预期工作,它们的行为并不少见。

ViewHolder XML

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/_6sdp"
    android:layout_marginTop="@dimen/_12sdp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/_24sdp"
        android:layout_height="@dimen/_24sdp"
        android:layout_marginStart="@dimen/_6sdp"
        android:layout_marginBottom="4dp"
        android:contentDescription="User Icon"
        app:layout_constraintBottom_toBottomOf="@+id/messageCv"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/messageCv"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/feelingfit_app_icon_legacy_circular_v1" />

    <!--<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/typingAnimation"
        android:layout_width="@dimen/_30sdp"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginStart="@dimen/_12sdp"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        app:lottie_repeatCount="3"
        android:visibility="visible"
        app:lottie_rawRes="@raw/typing"
        app:lottie_cacheStrategy="strong"
        app:layout_constraintBottom_toBottomOf="@+id/icon"
        app:layout_constraintStart_toEndOf="@+id/icon"
        app:layout_constraintTop_toTopOf="@+id/icon"
        />-->

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/messageCv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="@dimen/_40sdp"
        android:visibility="invisible"
        app:cardBackgroundColor="@color/light_blue_50"
        app:cardCornerRadius="@dimen/_10sdp"
        app:cardElevation="0dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/icon"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/layout_gchat_container_other"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/_6sdp"
                android:maxWidth="@dimen/_230sdp"
                android:paddingLeft="@dimen/_10sdp"
                android:paddingTop="@dimen/_6sdp"
                android:paddingRight="@dimen/_10sdp"
                android:text="Message"
                android:textColor="#000000"
                android:textSize="@dimen/text_size_normal" />
        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/timestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_4sdp"
        android:layout_marginBottom="@dimen/_4sdp"
        android:text="Today at 110:00pm"
        android:textColor="#C0C0C0"
        android:textSize="@dimen/_8sdp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/messageCv"
        app:layout_constraintTop_toBottomOf="@+id/messageCv" />

</androidx.constraintlayout.widget.ConstraintLayout>

适配器

代码语言:javascript
复制
class ReplaceAndEmbraceAdapter
constructor(
    private val dateUtil: DateUtil,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var messageList: MutableList<ReplaceAppModel> = mutableListOf()
    private val appMessage: Int = 1
    private val userMessage: Int = 2
    
    inner class ChatDiffCallback(
        private val oldList: List<ReplaceAppModel>,
        private val newList: List<ReplaceAppModel>
    ) : DiffUtil.Callback() {
        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList[oldItemPosition] == newList[newItemPosition]
        }
        override fun getOldListSize(): Int = oldList.size
        override fun getNewListSize(): Int = newList.size
        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList[oldItemPosition] == newList[newItemPosition]
        }
    }

    fun addChat(list: List<ReplaceAppModel>) = apply {
        val diffCallback = ChatDiffCallback(messageList, list)
        val diffResult = DiffUtil.calculateDiff(diffCallback)
        messageList.clear()
        messageList.addAll(list)
        diffResult.dispatchUpdatesTo(this)
    }

    /* fun addChat(list: List<ReplaceAppModel>) {
         messageList = list
         notifyItemInserted(messageList.size)
     }*/

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            appMessage -> {
                MessageAppViewHolder(
                    RecyclerViewReplaceAndEmbraceAppBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    ),
                    dateUtil
                )
            }

            else -> MessageUserViewHolder(
                RecyclerViewReplaceAndEmbraceUserBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is MessageAppViewHolder -> {
                holder.bind(messageList?.get(position))
            }
            is MessageUserViewHolder -> {
                holder.bind(messageList?.get(position))
            }
        }
    }

    override fun getItemCount(): Int {
        return if (!messageList.isNullOrEmpty()) {
            messageList!!.size
        } else {
            0
        }
    }

    override fun getItemViewType(position: Int): Int = messageList[position].id

    class MessageAppViewHolder
    constructor(
        private val binding: RecyclerViewReplaceAndEmbraceAppBinding,
        private val dateUtil: DateUtil
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: ReplaceAppModel?) = with(itemView) {
            binding?.apply {


                 typingAnimation.addAnimatorListener(object : Animator.AnimatorListener {
                     override fun onAnimationRepeat(animation: Animator?) {
                     }

                     override fun onAnimationEnd(animation: Animator?) {
                         typingAnimation.visibility = View.INVISIBLE
                         message.text =
                             HtmlCompat.fromHtml(item?.message!!, HtmlCompat.FROM_HTML_MODE_LEGACY)
                         icon.setImageResource(R.drawable.feelingfit_app_icon_legacy_circular_v1)
                         timestamp.text = dateUtil.getCurrentTimestampEDDMMMYYYYHHMM()
                         timestamp.visibility = View.VISIBLE
                         messageCv.visibility = View.VISIBLE
                         messageCv.fadeIn()
                         timestamp.fadeIn()
                     }

                     override fun onAnimationCancel(animation: Animator?) {
                     }

                     override fun onAnimationStart(animation: Animator?) {
                     }

                 })

            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-22 05:40:43

在我看来,在查看了可用的代码之后,问题在于约束,所以首先尝试更改typingAnimation和图标w.r.t父级的约束,而不是messageCv

但是在看完视频之后,我想你不需要把动画放进循环视图/适配器中。把它放在recyclerView下面就行了。我的意思是在recyclerView和EditText之间,当您想要显示/隐藏动画时,更改可见性。

如果您仍然希望在回收器视图中显示动画,那么您应该为动画视图创建一个单独的视图。这意味着您将有三个viewTypes和viewHolders。如果您要在recyclerView中展示它,这是一种更好的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67975909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档