[英]notifyDataSetChanged() doesn't call's onBindViewHolder() method
I have a RecyclerView's Adapter, where I am adding items dynamically, when I am calling the my adapter's updateMessages function old data list were changing correctly but, recycler items stays the same. 我有一个RecyclerView的适配器,我在动态添加项目,当我调用我的适配器的updateMessages函数时,旧数据列表正在正确更改但是,回收器项目保持不变。
this is my updateMessages method in my adapter: 这是我的适配器中的updateMessages方法:
fun updateMessages(messages: List<MessageReceivedResponseModel>?){
messages?.let {
this.messages.clear()
this.messages.addAll(messages)
}
notifyDataSetChanged()
}
also here is complete adapter class, I don't understand what's the problem 这里也是完整的适配器类,我不明白是什么问题
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()
}
}
}
this is where my adapter and recycler initialization goes 这是我的适配器和回收器初始化的地方
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())
}
I realized my problem where out of all this classes, The problem was in my interactor class where the messages retrieving requests started 我意识到我的问题在所有这些类中,问题出在我的交互器类中,其中检索请求的消息已经开始
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)
}
}
I removed doAsync and works correctly, here callback.onResponse() is being called from Non-UI thread and it caused the problem Only the original thread that created a view hierarchy can touch its views.
我删除了doAsync并正常工作,这里从非UI线程调用callback.onResponse()并导致问题
Only the original thread that created a view hierarchy can touch its views.
, but not always. , 但不总是。 Also app weren't crashed and I missed the log
应用程序也没有崩溃,我错过了日志
Try using notifyItemRangeChanged(0, messages.size - 1);
尝试使用
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)
}
}
I think notifyDataSetChanged()
isn't working because your dataset ( messages
field) is still the same object. 我认为
notifyDataSetChanged()
不起作用,因为您的数据集( messages
字段)仍然是同一个对象。
You can try to replace object of messages: 您可以尝试替换消息对象:
fun updateMessages(messages: List<MessageReceivedResponseModel>?){
messages?.let {
this.messages = messages
notifyDataSetChanged()
}
}
You should use notifyDataSetChanged();
你应该使用
notifyDataSetChanged();
on your adapter, like this: adapter.notifyDataSetChanged();
在您的适配器上,如下所示:
adapter.notifyDataSetChanged();
. 。 If you wanna use a class you can, you just need to declare the adapter variable as a member variable of the class and call the method like I said above.
如果你想使用一个类,你只需要将适配器变量声明为类的成员变量,并像上面所说的那样调用方法。 Where
adapter
is the name of your adapter. 其中
adapter
是adapter
的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.