我有一个问题,那就是 onBindViewHolder 永远不会被执行。当我调试时,它确实进入 setPostList 并设置帖子列表与它应该的 15 个项目,问题是 onBindViewHolder 永远不会达到,我不知道为什么会这样。
这是我的适配器
class PostAdapter: RecyclerView.Adapter<PostViewHolder>(){
var posts = mutableListOf<PostModel>()
fun setPostList(postResponseList: List<PostModel>){
Log.i("PostAdapter", "setPostList")
this.posts.clear()
this.posts.addAll(postResponseList.toMutableList())
this.notifyDataSetChanged()
//imprimir la lista de post con un forEach
posts.forEach { post -> Log.i("PostAdapter", post.toString()) }
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = PostItemBinding.inflate(inflater, parent, false)
return PostViewHolder(binding)
}
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
try {
Log.i("PostAdapter", "onBindViewHolder")
val post = posts[position]
holder.binding.petNameLabel.text = post.pet.name
//imprimir el nombre del pet
Log.i("PostAdapter", post.pet.name)
holder.binding.aboutLabel.text = post.pet.about
//imprimir el about del pet
Log.i("PostAdapter", post.pet.about)
// holder.binding.animalLabel.text = post.pet.animalType.name
// //imprimir el tipo de animal
// Log.i("PostAdapter", post.pet.animalType.name)
//
// holder.binding.addresLabel.text = post.pet.address
// //imprimir la dirección
// Log.i("PostAdapter", post.pet.address)
//
// holder.binding.breedLabel.text = post.pet.animalType.breed.name
// //imprimir la raza
// Log.i("PostAdapter", post.pet.animalType.breed.name)
//
// holder.binding.ageLabel.text = post.pet.age.toString()
// //imprimir la edad
// Log.i("PostAdapter", post.pet.age.toString())
} catch (e: Exception) {
Log.i("PostAdapter", "Error en onBindViewHolder: ${e.message}")
}
}
override fun getItemCount(): Int {
return posts.size
}
companion object {
const val POST_ID = "post_id"
}
}
class PostViewHolder (
val binding: PostItemBinding
) : RecyclerView.ViewHolder(binding.root)
我尝试将
notifyDataSetChanged()
放在适配器之外,并在加载回收视图的片段中调用它,但它不起作用。我还注释掉了其余标签,仅使用名称和即将进行测试,看看是否是 recycleview 渲染的问题,但事实并非如此。
这例如是我的应用程序中使用的代码
/**
* <h1>Game2RecyclerViewAdapter1</h1>
*
* **Game2RecyclerViewAdapter1** adapter for game2
*
* Refer to [androidauthority](https://www.androidauthority.com/how-to-build-an-image-gallery-app-718976/)
* by [Adam Sinicki](https://www.androidauthority.com/author/adamsinicki/)
*
* @version 5.0, 01/04/2024
* @see RecyclerView.Adapter<RecyclerView.ViewHolder>
*/
class Game2RecyclerViewAdapter(
private val context: Context,
private val galleryList: ArrayList<Game1ArrayList>
) : RecyclerView.Adapter<Game2RecyclerViewAdapter.ViewHolder>() {
var sharedPref: SharedPreferences
var preference_PrintPermissions: String?
/**
* used for TTS
*/
var tTS1: TextToSpeech? = null
/**
* Game2RecyclerViewAdapter1 constructor.
* set game2 list and context annotation, get print permissions
*
*/
init {
// if is print permitted then preference_PrintPermissions = Y
sharedPref = context.getSharedPreferences(
context.getString(R.string.preference_file_key), Context.MODE_PRIVATE
)
preference_PrintPermissions = sharedPref.getString(
context.getString(R.string.preference_print_permissions),
"DEFAULT"
)
}
/**
* Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.
*
* @param viewGroup viewGroup into which the new View will be added after it is bound to an adapter position
* @param i int with the view type of the new View.
* @return RecyclerView.ViewHolder new View created inflating it from an XML layout file
* @see RecyclerView.Adapter.onCreateViewHolder
*/
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.activity_game_2_cell_layout_1, viewGroup, false)
return ViewHolder(view)
}
/**
* Called by RecyclerView to display the data at the specified position.
* set on click listeners
*
*
* Refer to [stackoverflow](https://stackoverflow.com/questions/49969278/recyclerview-item-click-listener-the-right-way)
* answer of [Reaz Murshed](https://stackoverflow.com/users/3145960/reaz-murshed)
*
* @param viewHolder RecyclerView.ViewHolder ViewHolder which should be updated to represent the contents of the item at the given position in the data set
* @param i int with the position of the item within the adapter's data set.
* @see RecyclerView.Adapter.onBindViewHolder
*
* @see addImage
*
*/
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
viewHolder.title.text = galleryList[i].image_title
viewHolder.img.scaleType = ImageView.ScaleType.CENTER_CROP
viewHolder.img.contentDescription = galleryList[i].image_title
//
addImage(
galleryList[i].urlType,
galleryList[i].url, viewHolder.img,
200,200
)
//
viewHolder.img.setOnClickListener {
// TTS
tTS1 = TextToSpeech(context) { status ->
if (status != TextToSpeech.ERROR) {
tTS1!!.speak(
galleryList[i].image_title,
TextToSpeech.QUEUE_FLUSH,
null,
"prova tts"
)
} else {
Toast.makeText(context, status, Toast.LENGTH_SHORT).show()
}
}
//
if (preference_PrintPermissions == "Y") {
printImage(context, galleryList[i].urlType, galleryList[i].url, 200, 200 )
}
}
}
/**
* returns number of item within the adapter's data set.
*
* @return int with number of item within the adapter's data set
* @see RecyclerView.Adapter.getItemCount
*/
override fun getItemCount(): Int {
return galleryList.size
}
/**
* viewholder for game2 recyclerview.
*
*
* @see RecyclerView.ViewHolder
*/
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val title: TextView
val img: ImageView
init {
//
title = view.findViewById<View>(R.id.title) as TextView
img = view.findViewById<View>(R.id.img1) as ImageView
}
}
}