我正在尝试理解为什么适配器方法notifyDataSetChanged()无法刷新我的recyclerview。我在适配器这样的创建方法时找到了解决方案:
fun setData(list: List){ resumeList = list notifyDataSetChanged() }
此解决方案有效,但是,我想知道为什么我不能做这样的事情:
private lateinit var downloadAdapter: DownloadRecyclerAdapter private fun setupAdapter() { downloadAdapter = DownloadRecyclerAdapter( this@DownloadActivity, downloadList, { id -> onViewClick(id) }, { id -> onEditClick(id) }, { id, position -> onDeleteClick(id, position) } ) savedResumeRv.apply { layoutManager = LinearLayoutManager(context) layoutAnimation = AnimationUtils.loadLayoutAnimation( this@DownloadActivity, R.anim.layout_animation_down_to_up ) adapter = downloadAdapter } } private fun observers() { downloadViewModel.getDownloadList().observe(this, Observer { list -> downloadList = list list?.let { downloadAdapter.notifyDataSetChanged() } }) }
downloadAdapter持有列表downloadList的相同实例,我不知道为什么当我在活动中通知适配器时无法正常工作。
downloadList = list
由于这一行,不再是下载列表的参考
尝试
private fun observers() { downloadViewModel.getDownloadList().observe(this, Observer { list -> with(downloadList){ clear() addAll(list) } downloadList?.let { downloadAdapter.notifyDataSetChanged() } }) }