【问题标题】:Adding Drawable Images to RecyclerView将可绘制图像添加到 RecyclerView
【发布时间】:2022-01-04 18:17:07
【问题描述】:

我正在尝试将可绘制图像添加到我的菜单RecyclerView,但是当我运行应用程序时,图像没有显示出来。我是否需要在我的模型类中将 menuImage 设为 Int 以便它知道图像需要进入的位置?还是在将其添加到数组时需要更改语法?或者我需要在我的menuImage 在我的MenuAdaptersetImageDrawable?谢谢!

我的最终结果

我有什么

menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/iconImageButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginStart="16dp"
            android:background="@drawable/circle_shape"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />

        <ImageView
            android:id="@+id/iconImageView"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:elevation="10dp"
            app:layout_constraintBottom_toBottomOf="@+id/iconImageButton"
            app:layout_constraintEnd_toEndOf="@+id/iconImageButton"
            app:layout_constraintStart_toStartOf="@+id/iconImageButton"
            app:layout_constraintTop_toTopOf="@+id/iconImageButton"
            app:srcCompat="@drawable/profile_photo_"
            android:contentDescription="@string/string_menu_icon_image" />

        <TextView
            android:id="@+id/menuLabelTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="@string/menu_label"
            android:textAllCaps="false"
            android:textColor="@color/black"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@+id/iconImageButton"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/menuArrowImageView"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/right_chevron_"
            android:contentDescription="@string/menu_arrow" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

MenuItem.kt

class MenuItem(val menuName: String = "", val menuImage: String = "")

MenuAdapter.kt

class MenuAdapter(var context: Context, var list: ArrayList<MenuItem>) : RecyclerView.Adapter<MenuAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val v = LayoutInflater.from(parent?.context).inflate(R.layout.menu_item, parent, false)
        return MyViewHolder(v)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder?.bindItems(list[position])
    }

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

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view){

        fun bindItems(items: MenuItem) {
            val menuName = itemView.findViewById<TextView>(R.id.menuLabelTextView)
            val menuImage = itemView.findViewById<ImageView>(R.id.iconImageView)

            menuName.text = items.menuName
            Picasso.get().load(items.menuImage).into(menuImage)
        }
    }
}

MenuActivity.kt

lateinit var menuAdapter: MenuAdapter
val menuList = ArrayList<MenuItem>()

private fun createMenu() {

   menuAdapter = MenuAdapter(this, menuList)

  val menuRecycleView = findViewById<RecyclerView>(R.id.menuRecyclerView)
  menuRecycleView.apply {
       layoutManager = LinearLayoutManager(this@MenuActivity, LinearLayoutManager.VERTICAL, false)
       adapter = menuAdapter
       setHasFixedSize(true)
  }

  menuList.add(MenuItem("My Profile", "${R.drawable.outline_person_}"))
  menuList.add(MenuItem("My Bookings", "${R.drawable.bookings_}"))
  menuList.add(MenuItem("Payments", "${R.drawable.payment_}"))
  menuList.add(MenuItem("On Going Jobs", "${R.drawable.on_going_jobs_}"))
  menuList.add(MenuItem("Settings", "${R.drawable.settings_}"))
}

【问题讨论】:

  • 在 xml 文件中尝试将 "app:srcCompat=" 更改为 "app:src="
  • @PRANAVSINGH,如果我输入app:src=,我会得到error: attribute src (aka com.zwdalpha.skedaddle:src) not found.

标签: android kotlin android-recyclerview android-drawable


【解决方案1】:

您使用String 调用load(String),即this method

使用指定路径启动图像请求。这是调用load(Uri) 的便捷方法。 该路径可以是远程 URL、文件资源(以file: 为前缀)、内容资源(以content: 为前缀)、或android 资源(以android.resource: 为前缀)

您只是将裸资源 ID int 作为字符串传递:

MenuItem("My Profile", "${R.drawable.outline_person_}")

我不知道它具体想要什么,它可能是 "android.resource:${R.drawable.outline_person_}"(整数)或者它可能是 "android.resource:R.drawable.outline_person_"(限定 ID 名称) - 我猜是第一个,但如果它不起作用。

您的另一个选择是使用load(int),它直接采用实际的资源ID int,因此您可以将项目定义为MenuItem("My Profile", R.drawable.outline_person_) - 这是更好的IMO,除非您出于某种原因确实需要它作为字符串(并且你不能只在需要时将其转换为字符串)。

我不知道使用app:srcCompat 是否会出现问题,但如果您仍然遇到问题,您可以尝试使用android:src

(顺便说一句,您的日志中应该有一些关于此的错误信息,来自 Picasso 的消息说“无法加载 URL 1234567 或其他内容”,请务必检查日志以获取提示!)

【讨论】:

  • 谢谢,将我的menuImage 更改为Int 并在我的MenuAdapter 中更改为load(int) 效果很好!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
相关资源
最近更新 更多