android - 通过 Adapter 更改 RecyclerView Item 图标

android - Change RecyclerView Item icon through Adapter

所以,我正在创建一个应用程序,它在设置中有一个 RecyclerView。 RecyclerView items,包含一个cardview,包含一个ImageView,用来表示RecyclerView的item。我需要做的是用更新的图标替换占位符 ImageView,通过加载 RecyclerView 的片段提供槽。我在 this tutorial.

之后在 Kotlin 中创建了我的 RecyclerView

我尝试在数据 class 中创建类型为 ImageView、Image 和 Bitmap 的值,但是建议的函数不起作用。

如何在 class 中创建 ImageView 值?如何更改一个 RecyclerView ImageView 的内容?

dataListIcon.kt

import android.widget.ImageView

data class dataListIcons (
    val stringTitle: String,
    val stringDescription: String,
    val imageIcon: ImageView     //Here i tried creating the ImageView value
)

fragmentSettings.kt

class frgSettingsMain : Fragment() {
    val listsettings = listOf(
        dataListIcon("Option", "Description of option", R.drawable.ic_outline_color_lens_24),
        dataListIcon("Option", "Description of option", R.drawable.ic_outline_dashboard_24),
        dataListIcon("Option", "Description of option", R.drawable.ic_outline_image_24),
        dataListIcon("Option", "Description of option", R.drawable.ic_outline_volume_up_24),
        dataListIcon("Option", "Description of option", R.drawable.ic_outline_library_music_24)
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        retainInstance = true
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.fragment_settingsmain, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        rvSettingsMain.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = adapterSettingsMain(listsettings)
        }
    }

    companion object {
        fun newInstance(): frgSettingsMain = frgSettingsMain()
    }
}

adapterSettings.kt

class adapterSettingsMain(
    var listsettings: List<dataItems>
) : RecyclerView.Adapter<adapterSettingsMain.SettingsViewHolder>() {

    inner class SettingsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SettingsViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_settingsicon, parent, false)
        return SettingsViewHolder(view)
    }

    override fun getItemCount(): Int {
        return listsettings.size
    }

    override fun onBindViewHolder(holder: SettingsViewHolder, position: Int) {
        holder.itemView.apply {
            rvSettingsTitle.text = listsettings[position].stringTitle
            rvSettingsDescription.text = listsettings[position].stringDescription
            rvSettingsIcon //TODO
        }
    }
}

解决方案一:

只需使用 Glide 在 Imageviews 中进行图像渲染。

它会更灵活,可以使用更多功能。

val imageView : ImageView = ..initialise it..
Glide.with(imageView).placeholder(R.drawable.your_placeholder).into(imageView)

依赖关系:

implementation 'com.github.bumptech.glide:glide:4.11.0'

方案二:

val imageView : ImageView = ..initialise it..
imageView.setImageResource(R.drawable.your_resource)

val imageIcon: ImageView //Here i tried creating the ImageView value

ImageView 类型更改为 int,因为 R.drawable.ic_outline_color_lens_24Integer 类型

data class dataListIcons (
    val stringTitle: String,
    val stringDescription: String,
    val imageIcon Int    //Here i tried creating the ImageView value
)

将此添加到您的 onBindViewHolder(...) 方法中

 override fun onBindViewHolder(holder: SettingsViewHolder, position: Int) {
        holder.itemView.apply {
            rvSettingsTitle.text = listsettings[position].stringTitle
            rvSettingsDescription.text = listsettings[position].stringDescription
            rvIcon.setImageResource(listsettings[position].imageIcon);//rvIcon is id of ImageView from R.layout.item_settingsicon
        }
    }

在你的 R.layout.item_settingsicon

中添加