notifyDataSetChanged()不调用onBindViewHolder()方法

时间:2017-03-23 06:53:15

标签: java android listview android-recyclerview kotlin

我有一个RecyclerView的适配器,我在这里动态添加项目,当我调用我的适配器的updateMessages函数时,旧数据列表正在正确更改但是,回收项目保持不变。

这是我的适配器中的updateMessages方法:

fun updateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
       this.messages.clear()
       this.messages.addAll(messages)
    }
    notifyDataSetChanged()
}

这里也是完整的适配器类,我不明白是什么问题

class MessagesRecyclerAdapter(private val itemActionListener: IOnMessageItemActionListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val messages = ArrayList<MessageReceivedResponseModel>()

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

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    (holder as MyHolder).updateUI(messages[position])
}

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

fun updateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
        this.messages.clear()
        this.messages.addAll(messages)
    }
    notifyDataSetChanged()
}

private inner class MyHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val messageTextView: TextView = itemView.findViewById(R.id.chat_from_text_id) as TextView
    private val msgQuantityTextView: TextView = itemView.findViewById(R.id.msg_quantity_txt_id) as TextView

    internal fun updateUI(msg: MessageReceivedResponseModel) {
        messageTextView.text = msg.from
        msgQuantityTextView.text = msg.quantity.toString()
    }
}

}

这是我的适配器和回收器初始化的地方

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater?.inflate(R.layout.activity_awesome_chat, container, false)
    recycler = view?.findViewById(R.id.awesome_chat_list_view_id) as RecyclerView
    val layoutManager = LinearLayoutManager(activity)
    recycler?.layoutManager = layoutManager
.....
}

override fun onMessageReceived(messages: List<MessageReceivedResponseModel>){
        adapter?.updateMessages(messages)
        Log.e(TAG, messages.size.toString())
  }
// called in oncreateView of fragment
private fun initRecycler(items: List<MessageReceivedResponseModel>?) {
    adapter = MessagesRecyclerAdapter(this)
    adapter?.updateMessages(items)
    recycler?.adapter = adapter
    Log.e(TAG, items?.size.toString())
}

3 个答案:

答案 0 :(得分:2)

我意识到我的问题在所有这些类中,问题出在我的交互器类中,其中检索请求的消息已经开始

fun startMessageRetrieveRequest(callback: OnRequestFinishedListener<List<MessageReceivedResponseModel>>){
    doAsync {
        Thread.sleep(1000)
        val idx = Random().nextInt(2)
        val its: List<MessageReceivedResponseModel>
        when(idx){
            0 -> its = REs
            1 -> its = RES_1
            else -> its = RES_1
        }
        callback.onResponse(its)
    }
}

我删除了doAsync并且工作正常,这里从非UI线程调用callback.onResponse()并导致问题Only the original thread that created a view hierarchy can touch its views.,但并非总是如此。应用程序也没有崩溃,我错过了日志

答案 1 :(得分:0)

尝试使用notifyItemRangeChanged(0, messages.size - 1);

fun updateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
        this.messages.clear()
        this.messages.addAll(messages)
        this.notifyItemRangeChanged(0, messages.size - 1)
    }
}

我认为notifyDataSetChanged()无效,因为您的数据集(messages字段)仍然是同一个对象。

您可以尝试替换消息对象:

fun updateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
        this.messages = messages
        notifyDataSetChanged()
    }
}

答案 2 :(得分:-1)

您应该在适配器上使用notifyDataSetChanged();,如下所示: adapter.notifyDataSetChanged();。 如果你想使用一个类,你只需要将适配器变量声明为类的成员变量,并像上面所说的那样调用方法。其中adapter是适配器的名称。