我正在尝试使用适配器显示回收视图列表数据。到目前为止没有错误,我认为我的代码逻辑是正确的,但当我运行应用程序时,它没有显示列表。
下面是我的主要活动代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityMainBinding.root)
setSupportActionBar(activityMainBinding.mainToolbar)
binding = activityMainBinding.contentMain
val rv = binding.recyclerView
val clickListener = { view: View, bandName:String ->
//Deal with click feature later, show the list first
}
adapter = RVAdapter(clickListener)
rv.adapter = adapter
rv.layoutManager = LinearLayoutManager(this)
adapter.submitlist(bandResources.values.toMutableList())
下面是我的RVAdapter代码
class RVAdapter(private val clickListener: (view:View, bandName: String)->Unit)
: ListAdapter<BandInfo,RVAdapter.ViewHolder>(Diff())
{
inner class ViewHolder(val bandRowBinding : BandRowBinding)
: RecyclerView.ViewHolder(bandRowBinding.root) {
init {
bandRowBinding.root.setOnClickListener{
//Deal with click feature later, show the list first
}
}
}
private var bands = mutableListOf<BandInfo>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val bandRowBinding = BandRowBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
return ViewHolder(bandRowBinding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val binding = holder.bandRowBinding
bands[position].let{
binding.bandTitle.text = it.name
binding.bandCreated.text = it.time
}
}
fun submitlist(items: MutableList<BandInfo>){
bands = items
}
我哪里错了?
您已经向ListAdapter
添加了一个submitlist
方法,而ListAdapter
有自己的submitList
方法。
我认为我的代码逻辑是正确的,但它没有显示列表
您的submitlist
正在为bands
分配一个新列表,但您从未通知适配器。您可以在将列表分配给bands
后调用notifyDataSetChanged()
,它将开始在回收视图中显示您的列表。
fun submitlist(items: MutableList<BandInfo>){
bands = items
notifyDataSetChanged()
}
使用
notifyDataSetChanged()
不是有效的
notifyDataSetChanged()
不会告诉reyclerView
更改了哪些数据,而是强制重新创建整个列表,这是无效的。我们应该始终使用特定的通知方法,如notifyItemChanged
、notifyItemInserted
和notifyItemRemoved
。因此,recyclerView
只需要绘制所需的更改。
CCD_ 16具有其自己的CCD_ 17方法,该方法获取新列表并使用所提供的diff
实现将其显示在CCD_ 19中。它负责通知过程,并且只通过使用所提供的diff
实现比较新旧列表来调用所需的方法。
为了使adapter
工作,您必须在RVAdapter
中对进行一些更改。
class RVAdapter(private val clickListener: (view:View, bandName: String)->Unit)
: ListAdapter<BandInfo, RVAdapter.ViewHolder>(Diff())
{
inner class ViewHolder(val bandRowBinding : BandRowBinding)
: RecyclerView.ViewHolder(bandRowBinding.root) {
init {
bandRowBinding.root.setOnClickListener{
//Deal with click feature later, show the list first
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val bandRowBinding = BandRowBinding.inflate(LayoutInflater.from(parent.context),
parent, false)
return ViewHolder(bandRowBinding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val binding = holder.bandRowBinding
getItem(position).let{ //use `getItem` method of ListAdapter to get current item
binding.bandTitle.text = it.name
binding.bandCreated.text = it.time
}
}
}
使用ListAdapter
时,不需要将Band列表存储在适配器中。
您不需要声明另一个submitlist
函数,因为ListAdapter已经有了它,只需从ListAdapter
本身调用submitList()
函数即可。
您的RecyclerView适配器正在实现一个ListAdapter
,它已经为您处理了内部列表及其submitList
方法。通过在适配器内添加bands
列表并添加submitList
方法,您没有使用ListAdapter
提供的内容,因此您的RecyclerView将不会显示任何内容,因为适配器的内部状态始终为空列表。
从适配器和submitList方法中删除bands
。如果您想访问适配器的内部列表,可以使用currentList
来代替bands
。