Recyclerview notifyDataSetChanged 不起作用?
Recyclerview notifyDataSetChanged is not working?
我在布局中有一个复选框和一个回收视图。在适配器中,我创建了函数:selectAll 和 unSelectAll。只要复选框(在片段布局中)分别为 checked/unchecked,我希望所有项目都为 checked/unchecked。然而似乎什么都没有发生?我不确定是 notifyDataSetChanged() 不起作用还是错误地使用了它。我已经在这里检查了所有可能的解决方案,但我找不到一个。感谢任何帮助。
在适配器中:
class DetailRecyclerAdapter :
RecyclerView.Adapter<DetailRecyclerAdapter.BindingHolder>() {
var details: List<ApiDetail>? = null
var isSelectedAll: Boolean = true
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemDetailBinding.inflate(layoutInflater, parent, false)
return BindingHolder(binding)
}
override fun onBindViewHolder(holder: BindingHolder, position: Int) {
val detail = details?.get(position)
holder.binding.detail = detail
if (isSelectedAll) {
holder.binding.checkBox.isChecked = true
} else if(!isSelectedAll) {
holder.binding.checkBox.isChecked = false
}
}
override fun getItemCount(): Int {
return details?.size ?: 0
}
fun selectAll() {
isSelectedAll = true
notifyDataSetChanged()
}
fun unSelectAll() {
isSelectedAll = false
notifyDataSetChanged()
}
class BindingHolder(var binding: ItemDetailBinding) : RecyclerView.ViewHolder(binding.root)
}
片段中:
val adapter = DetailRecyclerAdapter()
binding?.checkBox?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
adapter.selectAll()
} else {
adapter.unSelectAll()
}
}
binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = DetailRecyclerAdapter()
}
布局中:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/details_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:search_details="@{viewModel.details}"/>
绑定适配器
@BindingAdapter(value = ["search_details"])
@JvmStatic
fun RecyclerView.setSearchConditionDetails(apiDetails: ObservableField<List<ApiDetail>>) {
adapter?.apply {
this as DetailRecyclerAdapter
this.details = apiDetails.get()
notifyDataSetChanged()
}
}
详情列表里有数据吗?因为适配器中的详细信息列表似乎为空。
var details: List<ApiDetail>? = null
但在那种情况下根本不应该显示列表。你可以检查和调试这个如果
onBindViewHolder()方法是否被调用
这是有问题的代码:
binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = DetailRecyclerAdapter()
}
您正在有效地创建 DetailRecyclerAdapter 的第二个实例,即 DetailRecyclerAdapter()
实例化了两次。
代码应为:
val detailRecyclerAdapter = DetailRecyclerAdapter()
binding?.checkBox?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
detailRecyclerAdapter.selectAll()
} else {
detailRecyclerAdapter.unSelectAll()
}
}
binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = detailRecyclerAdapter
}
您想将 detailsRecyclerView.adapter
设置为您之前创建的实例,而不是新实例。我将 var 从 adapter
重命名为 detailRecyclerAdapter
以使其更清楚。
我在布局中有一个复选框和一个回收视图。在适配器中,我创建了函数:selectAll 和 unSelectAll。只要复选框(在片段布局中)分别为 checked/unchecked,我希望所有项目都为 checked/unchecked。然而似乎什么都没有发生?我不确定是 notifyDataSetChanged() 不起作用还是错误地使用了它。我已经在这里检查了所有可能的解决方案,但我找不到一个。感谢任何帮助。
在适配器中:
class DetailRecyclerAdapter :
RecyclerView.Adapter<DetailRecyclerAdapter.BindingHolder>() {
var details: List<ApiDetail>? = null
var isSelectedAll: Boolean = true
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemDetailBinding.inflate(layoutInflater, parent, false)
return BindingHolder(binding)
}
override fun onBindViewHolder(holder: BindingHolder, position: Int) {
val detail = details?.get(position)
holder.binding.detail = detail
if (isSelectedAll) {
holder.binding.checkBox.isChecked = true
} else if(!isSelectedAll) {
holder.binding.checkBox.isChecked = false
}
}
override fun getItemCount(): Int {
return details?.size ?: 0
}
fun selectAll() {
isSelectedAll = true
notifyDataSetChanged()
}
fun unSelectAll() {
isSelectedAll = false
notifyDataSetChanged()
}
class BindingHolder(var binding: ItemDetailBinding) : RecyclerView.ViewHolder(binding.root)
}
片段中:
val adapter = DetailRecyclerAdapter()
binding?.checkBox?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
adapter.selectAll()
} else {
adapter.unSelectAll()
}
}
binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = DetailRecyclerAdapter()
}
布局中:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/details_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:search_details="@{viewModel.details}"/>
绑定适配器
@BindingAdapter(value = ["search_details"])
@JvmStatic
fun RecyclerView.setSearchConditionDetails(apiDetails: ObservableField<List<ApiDetail>>) {
adapter?.apply {
this as DetailRecyclerAdapter
this.details = apiDetails.get()
notifyDataSetChanged()
}
}
详情列表里有数据吗?因为适配器中的详细信息列表似乎为空。
var details: List<ApiDetail>? = null
但在那种情况下根本不应该显示列表。你可以检查和调试这个如果 onBindViewHolder()方法是否被调用
这是有问题的代码:
binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = DetailRecyclerAdapter()
}
您正在有效地创建 DetailRecyclerAdapter 的第二个实例,即 DetailRecyclerAdapter()
实例化了两次。
代码应为:
val detailRecyclerAdapter = DetailRecyclerAdapter()
binding?.checkBox?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
detailRecyclerAdapter.selectAll()
} else {
detailRecyclerAdapter.unSelectAll()
}
}
binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = detailRecyclerAdapter
}
您想将 detailsRecyclerView.adapter
设置为您之前创建的实例,而不是新实例。我将 var 从 adapter
重命名为 detailRecyclerAdapter
以使其更清楚。