添加项目后Recyclerview不更新

问题描述 投票:0回答:1

我正在将聊天对话实现为回收器视图,但当我输入新消息时,我没有成功更新列表。这是适配器类:

class ConversationAdapter(messagesList: List<ChatMessageModel>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var m_MessagesList: List<ChatMessageModel> = messagesList

    inner class MyViewHolder(view: View, context: Context) : RecyclerView.ViewHolder(view) {
        var m_Sender: TextView = view.findViewById(R.id.conversation_sender)
        var m_Message: TextView = view.findViewById(R.id.conversation_message)
        var m_Date: TextView = view.findViewById(R.id.conversation_timestamp)
    }

    override fun getItemViewType(position: Int): Int {
        return 0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.conversationlayout, parent, false)
        return MyViewHolder(itemView, parent.context)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message: ChatMessageModel = m_MessagesList[position]
        val holderView = holder as MyViewHolder
        holderView.m_Sender.text = message.getSender()
        holderView.m_Message.text = message.getMessage()
        holderView.m_Date.text = message.getTimestamp()
    }

    override fun getItemCount(): Int {
        return m_MessagesList.size
    }

    fun updateSections(messagesList : List<ChatMessageModel>) {
        m_MessagesList = messagesList
    }
}

这里的问题是 updateSections 方法是否正确。我还是 Kotlin 的新手,但 m_MessagesList 是一个不可变列表。我还能这样写吗?

这是对话片段中的更新函数:

private fun sendMessage() {
        val message = m_EditText.text.toString()
        var formattedDate = DateTimeUtils.getDateTimeNowAsString()
        val chatMessage = ChatMessage(0, m_MultiplayerRound.getUserId().toInt(), m_MultiplayerRound.getUsername(), m_UserId.toInt(), m_Username,  m_MultiplayerRound.getUserId().toInt(), m_MultiplayerRound.getUsername(), message, Date.from(Instant.now()))
        val chatMessageModel = ChatMessageModel(chatMessage, m_MultiplayerRound.getUsername(), m_MultiplayerRound.getUserId())

        m_MessagesList = m_MessagesList + chatMessageModel
        m_ConversationAdapter.updateSections(m_MessagesList)
        m_ConversationAdapter.notifyDataSetChanged()
}

又是关于m_MessagesList更新的问题。可以用 Kotlin 写这样的东西吗? 我使用调试器进行了验证,m_MessagesList 变量似乎已正确更新。最后为什么回收器视图不更新?

这是fragment的onCreateView函数:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
        val rootView = inflater.inflate(R.layout.fragment_conversation1, container, false)

        val sendMessageButton = rootView.findViewById(R.id.send_message) as Button
        sendMessageButton.setOnClickListener {
        sendMessage()
    }
        m_EditText = rootView.findViewById(R.id.message_edittext)

        val recyclerView: RecyclerView = rootView.findViewById(R.id.recycler_conversation)
        val mLayoutManager = LinearLayoutManager(activity)
        recyclerView.layoutManager = mLayoutManager
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.adapter = m_ConversationAdapter
}

非常感谢!

android kotlin android-recyclerview
1个回答
0
投票

看到你的代码,你仍然需要返回充气布局来设置片段。

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
        val rootView = inflater.inflate(R.layout.fragment_conversation1, container, false)
        ...

        return rootView
}
© www.soinside.com 2019 - 2024. All rights reserved.