Android 未调用RecyclerView onCreateViewHolder
由于某些原因,我的recyclerview适配器的onCreateViewHolder没有被调用,我不知道为什么。 最初,我对我所有的recyclerview列表使用一个适配器,我经历了同样的事情,所以我制作了一个单独的适配器来找出原因。我想相信我的配置是正确的。 这是我的适配器Android 未调用RecyclerView onCreateViewHolder,android,android-recyclerview,Android,Android Recyclerview,由于某些原因,我的recyclerview适配器的onCreateViewHolder没有被调用,我不知道为什么。 最初,我对我所有的recyclerview列表使用一个适配器,我经历了同样的事情,所以我制作了一个单独的适配器来找出原因。我想相信我的配置是正确的。 这是我的适配器 class BrowseMediaRecyclerAdapter( private val clickListener: (Upload) -> Unit, private val moreInf
class BrowseMediaRecyclerAdapter(
private val clickListener: (Upload) -> Unit,
private val moreInfoListener: (Upload) -> Unit) : RecyclerView.Adapter<BrowseMediaRecyclerAdapter.ViewHolder>() {
lateinit var binding: BrowseMediaItemListBinding
/**
*
* Autonotify msee
*
*/
private var uploadList: List<Upload> by Delegates.observable(emptyList()) { _, oldList, newList ->
autoNotify(oldList, newList) {old, new -> old.id == new.id}
}
fun updateList(items: List<Upload>?){
uploadList = items!!
Log.e("Items", "Item received: ${items.size}")
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
Log.e("Bind", "View holder created")
binding = DataBindingUtil.inflate(inflater, R.layout.browse_media_item_list, parent, false)
return ViewHolder(binding.root)
}
override fun getItemViewType(position: Int): Int = position
override fun getItemCount(): Int = uploadList.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = uploadList[position]
Picasso.get()
.load(item.thumbnailLink)
.placeholder(R.drawable.ic_local_movies_black_24dp)
.fit()
.transform(TransformImage(16f))
.into(holder.thumb)
holder.bind(uploadList[position], binding, clickListener, moreInfoListener)
Log.e("Bind", "Item binded: ${item.id}")
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val thumb: ImageView = itemView.media_thumb
val authorThumb: ImageView = itemView.authorImage
private val moreInfo : FrameLayout = itemView.moreDetail
/**
*
* Bind data
*
*
*/
fun bind(item: Upload, binding: BrowseMediaItemListBinding, clickListener: (Upload) -> Unit, moreInfoListener: (Upload) -> Unit) {
binding.uploadItem = item
thumb.setOnClickListener{clickListener(item)}
moreInfo.setOnClickListener{moreInfoListener(item)}
}
}
}
稍后在我的观察者中更新列表
private val loadUploadItemsObserver = Observer<SubjectUploads> { subject ->
binding.selectedSubjectText.text = subject.subject_title
mediaRecyclerAdapter.updateList(subject.uploads)
}
private val loadUploadItemsObserver=Observer{subject->
binding.selectedSubjectText.text=subject.subject\u title
mediaRecyclerAdapter.updateList(subject.uploads)
}
我的xml文件
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/subjectsUploadsRecyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/selectedSubjectTextFrame"
tools:listitem="@layout/media_item_list" />
我可能做错了什么?当CreateViewHolder在两种情况下被调用时,有人能帮忙吗。当您最初从
getItemCount
方法提供项目计数时,2。当您更改列表项(CRUD)并通知适配器时。我认为您的问题是您使用此方法设置了列表。mediaRecyclerAdapter.updateList(subject.uploads)
是正确的,但忘记了通知适配器。@JeelVankhede确实说了什么。用新列表替换uploadList
时,不会调用autoNotify()
,因为它是最初声明的列表的一部分。在updateList(items:List?
)中,接受可为null的参数并使用忽略null也是一种不好的做法代码>。只需删除@JeelVankhede的?
。你是对的。我做了一个手册notifyDataSetChanged(),所有这些看起来都很有效。谢谢
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/subjectsUploadsRecyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/selectedSubjectTextFrame"
tools:listitem="@layout/media_item_list" />