在 Kotlin RecyclerView 项目中不显示

In Kotlin RecyclerView items not displayed

我是 Kotlin 初学者,正在学习 RecyclerView 示例。我开始编码但没有得到我应该得到的结果,即使在检查和重新检查代码之后也是如此。然后我注意到,即使使用非常基本的代码,它仍然无法正常运行。 我将包括基本代码,当使用 tools:listitem 时应该显示通用列表。我只得到列表中的 1 项。我怀疑代码以外的东西正在影响结果;但是我还没有达到知道的水平。

请参阅Activity_main.xml:

    <?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="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/recyclerview_item"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/recycler_view"/>

</androidx.constraintlayout.widget.ConstraintLayout>

注意它有 tools:listitem 行。

现在是列表 (recyclerview_item.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/titleTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Title"/>

    <TextView
        android:id="@+id/timeTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12:42"/>

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="#C3C3C3"/>

</LinearLayout>

最初它是作为约束布局开始的,但我在某处读到 LinearLayout 可能是治疗方法。

那么MainActivity.kt就是你开始一个项目时的标准。仅此而已:

package com.example.recyclerexample2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

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

这是一张显示 xml 文件设计的截图,仅显示 1 个项目,而不是完全填充项目。

这是在 xml 文件中添加 listitem 行之前。

我正在使用: Android 北极狐工作室 | 2020.3.1 补丁 4 Build #AI-203.7717.56.2031.7935034,建于 2021 年 11 月 21 日 运行时版本:11.0.10+0-b96-7249189 amd64 VM:Oracle Corporation 的 OpenJDK 64 位服务器 VM Windows 10 10.0 GC:G1年轻代,G1老年代 内存:1280M 核心数:12 注册表:external.system.auto.import.disabled=true 非捆绑插件:com.intellij.marketplace、com.thoughtworks.gauge

任何帮助将不胜感激。

您应该为列表项使用“wrap_content”