android (Kotlin)RecyclerView按钮在ImageView之外更改

xmakbtuz  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(189)

我有一个构造RecyclerView的适配器类。我希望这个RecyclerView有一个按钮,更新RecyclerView外部的ImageView。这段代码更新了RecyclerView内部的图像,但我无法让它更新外部的图像。

  1. class CustomAdapter(private val mList: List<ItemsViewModel>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
  2. // create new views
  3. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  4. // inflates the card_view_design view
  5. // that is used to hold list item
  6. val mainXML = LayoutInflater.from(parent.context)
  7. .inflate(R.layout.activity_main, parent, false)
  8. val view = LayoutInflater.from(parent.context)
  9. .inflate(R.layout.card_view_design, parent, false)
  10. return ViewHolder(view, mainXML)
  11. }
  12. // binds the list items to a view
  13. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  14. val ItemsViewModel = mList[position]
  15. // sets the image to the imageview from our itemHolder class
  16. Picasso.get().load("https://img.pokemondb.net/sprites/x-y/normal/" + ItemsViewModel.text + ".png").into(holder.imageView)
  17. // sets the text to the textview from our itemHolder class
  18. holder.textView.text = ItemsViewModel.text
  19. holder.buttonView.setOnClickListener {
  20. Log.e("GIF selecter", holder.pokemonSelect.toString())
  21. holder.pokemonSelect.setImageResource(R.drawable.ic_launcher_background)
  22. Picasso.get().load("https://img.pokemondb.net/sprites/x-y/normal/golbat.png").into(holder.pokemonSelect) //This is the line of troubles, changing it to "holder.imageView" changes the image inside the recyclerview. I want it to change an image outside the recyclerview
  23. }
  24. }
  25. // return the number of the items in the list
  26. override fun getItemCount(): Int {
  27. return mList.size
  28. }
  29. // Holds the views for adding it to image and text
  30. class ViewHolder(ItemView: View, mainPage: View) : RecyclerView.ViewHolder(ItemView) {
  31. val pokemonSelect: ImageView = mainPage.findViewById(R.id.pokemonSelect)
  32. val imageView: ImageView = itemView.findViewById(R.id.pokemonImg)
  33. val textView: TextView = itemView.findViewById(R.id.pokemon)
  34. val buttonView: Button = itemView.findViewById(R.id.changeGIF)
  35. }
  36. }
2jcobegt

2jcobegt1#

问题是你在createViewHolder内部膨胀了activity_main.xml,它什么也没做,因为它没有指向实际的activity_main示例,而是创建了一个新的View示例,它根本没有渲染/附加到层次结构。即使将inflate的第三个参数设置为true,它仍然是回收器项的子项,而不是主布局,这是在adapter的构造函数中传递所需外部ImageView的理想方法。

  1. class CustomAdapter(
  2. private val mList: List<ItemsViewModel>,
  3. private val pokemonSelect: ImageView) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
  4. ...//in onBind
  5. holder.buttonView.setOnClickListener {
  6. Log.e("GIF selecter", holder.pokemonSelect.toString())
  7. holder.pokemonSelect.setImageResource(R.drawable.ic_launcher_background)
  8. // get rid of holder
  9. Picasso.get().load("https://img.pokemondb.net/sprites/x-y/normal/golbat.png").into(pokemonSelect)
  10. }
  11. ...
  12. // In MainActivity
  13. customAdapter = CustomAdapter(list, findViewById(R.id.pokemonSelect))
展开查看全部

相关问题