【问题标题】:RecyclerView wont display image but displays textRecyclerView 不会显示图像但显示文本
【发布时间】:2019-08-30 19:25:19
【问题描述】:

我正在使用 Kotlin 制作一个 RecyclerView,它将显示图像和文本。到目前为止,文本应该只是“Textview”,图像应该是 android 附带的图像之一。文本显示“Textview”指定次数,但根本不显示图像。

我实际上正在关注 [此视频](https://www.youtube.com/watch?v=jS0buQyfJfs&list=PL0dzCUj1L5JGfHj1lwxOq67zAJV3e1S9S&index=2&t=409s! 并遇到了问题。

我已更改图像的单元格布局以包装内容,但没有帮助。

适配器

class MainAdapter : RecyclerView.Adapter<CustomViewHolder>() {
    val vidTitles = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        //create the view for the rows
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
        return CustomViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        holder?.view?.videoTitleTextView?.text= vidTitles[position]
    }
}

class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {

}

RecyclerView 的 Cell 布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:contentDescription="avatar image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/videoTitleTextView"
        android:layout_width="0dp"
        android:layout_height="31dp"
        android:text="animals"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

主要活动

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //recyclerView_main.setBackgroundColor(Color.CYAN)
        recyclerView_main.layoutManager = LinearLayoutManager(this)
        recyclerView_main.adapter = MainAdapter()
    }
}

我希望它同时显示图像和文本;它显示文本但不显示任何图像,甚至没有为图像留下任何空间。

【问题讨论】:

    标签: android kotlin android-recyclerview


    【解决方案1】:

    如下更改您的图像视图。

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:contentDescription="avatar image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@mipmap/ic_launcher" />
    

    您使用的是错误的 tools:srcCompat

    还将约束布局的高度更改为 wrap_content。否则 Recyclerview 中的每个项目都将占据整个屏幕高度。

    Tools 命名空间用于编译时行为。当您构建应用程序时,此工具功能将被删除。它可以帮助您在编译时预览视图。

    【讨论】:

    • 按照指示工作非常感谢。问题是为什么 tools:srcCompat 不起作用/是错误的?
    【解决方案2】:

    替换这个

    tools:srcCompat="@mipmap/ic_launcher"
    

    用这个:

    android:src="@mipmap/ic_launcher"
    

    注意:这里的tools:srcCompat只会在Android Studio的预览中显示,而不是在真机中。 更多信息请查看here

    【讨论】:

    • 我按照你说的做了,效果很好。为什么会这样呢?
    • 当我添加注释Here tools:srcCompat will show only in the preview of android studio but not in the real device.
    【解决方案3】:

    当我正确阅读您的代码时,您应该只在 TextViews 中看到“动物”:) 这是因为您在单元格的布局文件中设置了 android:text 而不是 tools:text 属性。

    只需实现您的 ViewHolder 并将视图绑定到您的模型。

    如果您只想验证您的视图是否已加载,您可以替换

    tools:srcCompat="@mipmap/ic_launcher"
    

    app:srcCompat="@mipmap/ic_launcher"
    

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2021-04-28
      • 2021-12-11
      • 2021-10-28
      • 1970-01-01
      • 2020-08-23
      相关资源
      最近更新 更多