微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

这就是我创建 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,false)
            ViewHolder(view)
        } else {
            val view = LayoutInflater.from(mContext).inflate(R.layout.item_show_time_chat,false)
            ViewHolder(view)
        }
    }

这是我区分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()
    }

最后将其应用到 BindViewHolder

if (mChatList[position].getMessageDate() == getTheRecentMessageDate(position)){
            holder.showDateTv?.text = getTheRecentMessageDate(position)
        }

解决方法

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

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

这是一些粗略的伪代码,抱歉这是 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
        }
        ...
    }

请注意,这不包括奇数个聊天消息,例如没有回复的发件人,这一定是正常的。这个答案有望为您指明正确的方向。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。