【问题标题】:Recyclerview view not populatingRecyclerview 视图未填充
【发布时间】:2020-11-28 17:10:08
【问题描述】:

我是一名 Kotlin 新手,正在学习如何创建简单的 recyclerview 应用。我的代码应该在垂直堆叠的单元格中列出整数 1..10。但是,它只列出了第一项。我已经查阅了几个教程并多次审查了我的代码(经过长时间的休息),但我看不出我的代码有什么问题。

我今天早些时候有了打印日志语句的好主意。检查它们,我注意到我的 onBindViewHolder 函数只被调用一次。我犯了什么错误?

这是我的日志输出:

D/QuoteAdapter: value is: 1
D/QuoteAdapter: index is: 0
D/QuoteAdapter: Size is: 10

我的活动:

class MainActivity : AppCompatActivity() {
    lateinit var mRecyclerView: RecyclerView

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

        mRecyclerView = findViewById(R.id.recyclerView)
        mRecyclerView.layoutManager = LinearLayoutManager(this)

        mRecyclerView.adapter = QuoteAdapter()
        //mRecyclerView.setHasFixedSize(true)
    }
}

我的适配器:

class QuoteAdapter : RecyclerView.Adapter<QuoteViewHolder>() {
    private val listOfInts = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    private val TAG = "QuoteAdapter"

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

    override fun getItemCount(): Int {
        Log.d(TAG, "Size is: ${listOfInts.size.toString()}")
        return listOfInts.size
    }

    override fun onBindViewHolder(holder: QuoteViewHolder, position: Int) {
        val item = listOfInts[position]
        Log.d(TAG, "value is: ${item.toString()}")
        Log.d(TAG, "index is: ${position.toString()}")
        holder.listTitle.text = item.toString()
    }
}

我的观众:

class QuoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val listTitle = itemView.findViewById(R.id.itemString) as TextView
}

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/itemString"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

我的主要布局如下图:

<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:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 我们能看到你的RecyclerView所在的xml吗?
  • @DavidKroukamp:最后一部分包含我的主要 xml 文件。
  • 很酷,代码对我来说看起来不错尝试我的答案建议我认为这是问题所在?

标签: android kotlin android-recyclerview


【解决方案1】:

在你的“我的布局”中试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/itemString"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

通知layout_heightLinearLayout 已更改为wrap_content

另外怀疑您是否需要 ViewHolders 项目 xml 上的 android:orientation="vertical",除非您将来添加的不仅仅是 1 个 TextView

【讨论】:

  • 是的,wrap_content 不像 match_parent 那样占据整个屏幕。这种方式 onBind 被称为其他时间更多
  • 是的,我自己没有测试它,但认为这似乎并不正确,正如 OP 所说,它只被调用一次是有道理的,因为它占据了整个屏幕而 Recyclerview 不会显示或绑定隐形持有人
  • 我试过了,就是这个。 OP 的代码运行良好,只是每个项目实际上都是屏幕大小,顶部有一个小文本框,您仍然可以滚动它!
  • 很有趣,这么小的东西怎么能有这么大的效果。现在可以了。谢谢!
  • 哈哈,这是对的,我们可以花多长时间寻找它们?。很高兴它正在工作
【解决方案2】:

就像 Zain 所说,您可以在布局文件中单独使用 TextView,这也可以解决问题(只要它的高度是 wrap_content!)

实际上有一些包含在 Android 中 - 输入 android.R.layout.,你会看到一些东西,比如 simple_list_item_1,它只是一个样式 TextView(你可以 ctrl+单击参考或任何查看文件)。如果您只想做一件快速的事情,那就太好了!

android.R.layout.simple_list_item_1TextView 的 ID 是 @android:id/text1 - 请注意 android 前缀,因为它是 android 资源的一部分,而不是您的应用程序的一部分。这意味着您必须以与布局相同的方式引用 ID,android 在前面:android.R.id.text1

【讨论】:

  • 内置物品上不错的一款+1
【解决方案3】:

可以去掉列表项布局中的LinearLayout,只保留TextView

因此,将列表项布局替换为:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemString"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多