android - 点击 Facebook Messenger 聊天项目动画

标签 android android-recyclerview android-animation kotlin facebook-messenger

我正在尝试让我的列表项在 onClick() 之后变为动画,就像 Facebook Messenger。我尝试在我的列表项布局和我的 recyclerView 的父布局中使用 animateLayoutChanges=true 来做到这一点,但是它不流畅并且有一些问题,当我想显示隐藏字段时第一次点击(< strong>setVisibility(VISIBLE)) ,效果不错,但是 setVisibility(GONE) 不能正常工作,这就是它的样子
enter image description here

我想要实现的是这个

enter image description here

有什么建议如何做到这一点? 这是我的自定义 View

class MessageItemView : RelativeLayout {

  constructor(context: Context) : super(context)
  constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

  private var messageTextView: TextView
  private var dateTextView: TextView
  private var itemView: View? = null
  init {
      itemView =  LayoutInflater.from(context)?.inflate(R.layout.live_chat_list_item, this)
      messageTextView = itemView?.findViewById(R.id.live_chat_msg_text_id) as TextView
      dateTextView = itemView?.findViewById(R.id.message_received_date_txt_id) as TextView
  }

 fun setUPViewModelData(message: MessageModel) {
     itemView?.setOnClickListener {
         dateTextView.visibility = if (dateTextView.visibility == View.VISIBLE) View.GONE else View.VISIBLE
         delivered_status_txt_id.visibility = dateTextView.visibility
  }
}

这是这个 View 的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

<TextView
    android:id="@+id/message_received_date_txt_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:visibility="gone" />

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

    <TextView
        android:id="@+id/live_chat_msg_text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

<TextView
    android:id="@+id/delivered_status_txt_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/message_wrapper_layout_id"
    android:visibility="gone" />

</RelativeLayout>

我不明白我的布局层次结构有什么问题,或者什么是好的架构,我也对 Facebook 的 Messenger 聊天项目布局层次结构感兴趣...有什么想法吗?

最佳答案

从布局中删除 animateLayoutChanges,然后使用支持库中的 TransitionManager:

fun setUPViewModelData(message: MessageModel) {
     itemView?.setOnClickListener {
         TransitionManager.beginDelayedTransition(recyclerView)
         dateTextView.visibility = if (dateTextView.visibility == View.VISIBLE) View.GONE else View.VISIBLE
         delivered_status_txt_id.visibility = dateTextView.visibility
    }
}

关于android - 点击 Facebook Messenger 聊天项目动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112739/

相关文章:

android - 使用 Android NFC 写入 Mifare 标签?

java - 调用 Fragment 的空构造函数而不是 newInstance

android - 使用 PagerSnapHelper 的垂直和水平 RecyclerView

android - 如何在 RecyclerView 上排序期间提供自定义动画(notifyDataSetChanged)

java - 我有一个关于动画拖放的问题

java - 如何更改其他 Activity 的文本?

Android Gradle 对 fork 的 GitHub 项目的依赖

android - 根据 RecyclerView 每行中的数据类型膨胀行 View

android - 如何将整个屏幕的 fragment 最小化到屏幕底部

java - 如何在android中滑入和滑出 View