Android 如何在recyclerView中的两个不同项目之间显示不同的项目?

Android 如何在recyclerView中的两个不同项目之间显示不同的项目?,android,android-recyclerview,chat,adapter,Android,Android Recyclerview,Chat,Adapter,我正在开发一个聊天应用程序,有两种类型的视图,一种用于传入消息,另一种用于传出消息。因此,我通过视图类型将它们区分为0和1。因此,它的工作非常棒,但现在我想添加另一个日期视图作为标题,如。为此,我创建了另一个视图类型为2,但它将其替换为消息视图。那么,如何显示两个视图之间的第三个视图 这就是我如何创建ViewHolder override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {

我正在开发一个聊天应用程序,有两种类型的视图,一种用于传入消息,另一种用于传出消息。因此,我通过视图类型将它们区分为0和1。因此,它的工作非常棒,但现在我想添加另一个日期视图作为标题,如。为此,我创建了另一个视图类型为2,但它将其替换为消息视图。那么,如何显示两个视图之间的第三个视图

这就是我如何创建
ViewHolder

override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
        return if (position == 1) {
            val view = LayoutInflater.from(mContext).inflate(R.layout.item_outgoing_message, parent, false)
            ViewHolder(view)
        } else if (position == 0) {
            val view = LayoutInflater.from(mContext).inflate(R.layout.item_incoming_message, parent, false)
            ViewHolder(view)
        } else {
            val view = LayoutInflater.from(mContext).inflate(R.layout.item_show_time_chat, parent, false)
            ViewHolder(view)
        }
    }
if (mChatList[position].getMessageDate() == getTheRecentMessageDate(position)){
            holder.showDateTv?.text = getTheRecentMessageDate(position)
        }
这是我区分
ViewType

 override fun getItemViewType(position: Int): Int {
        return if (mChatList[position].getSender() == currentUserID) {
            1
        }
        else if (mChatList[position].getMessageDate() == getTheRecentMessageDate(position)){
            2
        }
        else {
            0
        }
    }
这是为了得到日期之间的差异

private fun getTheRecentMessageDate(position: Int): String {
        val lastMessagePosition = position - 1
        //return the 1st message if messages chat thread is empty
        val chat = mChatList[if (lastMessagePosition <= 0) 0 else lastMessagePosition]
        //get and return the sender of the last message
        return chat.getMessageDate()
    }

您的适配器逻辑有缺陷,您需要定义从数据模型(
mChatList
)到回收视图的正确映射

例如,如果您的聊天信息列表有两个条目,则需要一个包含三行的回收器

下面是一些粗略的伪代码,很抱歉这是Java,但您应该了解:

    static final int VIEW_TYPE_SENDER = 0;
    static final int VIEW_TYPE_TIMESTAMP = 1;
    static final int VIEW_TYPE_REPLY = 2;

    public int getItemCount()
    {
      // One row for each chat message + one row for each pair of messages.
      // This logic possibly flawed for odd # of messages in mChatList
      return (mChatList.size() + (mChatList.size() / 2);
    } 
    
    public int getItemViewType (int position)
    {
        // modulo 3 to get the view type
        switch (position % 3)
        {
         case 0:
             return (VIEW_TYPE_SENDER);
         case 1:
            return (VIEW_TYPE_TIMESTAMP);
         case 2:
            return (VIEW_TYPE_REPLY);
        }
    }
    
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        switch (viewType)
        {
        case VIEW_TYPE_SENDER:
           // return a Sender viewholder...
        case VIEW_TYPE_REPLY:
           // return a Reply viewholder...
        case VIEW_TYPE_TIMESTAMP:
           // return a timestamp viewholder...
        }
     }
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if (holder instanceof SenderHolder)
        {
            int chatMessage = (position / 3);
            // use mChatList[chatMessage] to get the data 
        }
        else if (holder instanceof ReplyHolder)
        {
            int chatMessage = (position / 3) + 1;
// use mChatList[chatMessage] to get the data
        }
        ...
    }

请注意,这并不能解释奇数的聊天信息,例如,没有回复的发件人,这必须是正常的。这个答案有望为您指明正确的方向。

您可能会发现,将“邮件日期”视图作为发件人邮件布局的一部分更容易。。。这将在您的
mChatList
和回收器之间启用一对一映射rows@CSmith我知道这一点,并且我已经尝试过了,但是当我滑动邮件进行回复时,日期视图也会随之滑动。您的适配器需要表示您的聊天信息(例如2行)到您的回收器(例如3行)的映射。您的
onCreateViewHolder
getItemViewType
onBindViewHolder
逻辑不正确。@请您解释一下,好吗?