【问题标题】:RecyclerView items not showing properlyRecyclerView 项目未正确显示
【发布时间】:2019-11-11 14:45:23
【问题描述】:

在我的项目中,我使用了RecyclerView,正如我预期的那样,它在layout-preview 中显示,但问题是当我在emulator/device 中运行application 时,RecyclerView 的项目没有显示如layout-preview 所示。

这是recyclerview的XML。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".view.LActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/rv_sample" />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:src="@drawable/ic_add"
        android:id="@+id/fabAdd"
        app:tint="@color/white"
        app:backgroundTint="@color/colorPrimary"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_height="wrap_content"/>


</androidx.constraintlayout.widget.ConstraintLayout>

recyclerview 项目的 XML。

<com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        app:cardCornerRadius="10dp"
        android:minHeight="60dp"
        app:cardUseCompatPadding="true"
        app:strokeColor="@color/design_default_color_secondary_variant"
        app:strokeWidth="1dp">

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

            <TextView
                android:id="@+id/tvWord"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginEnd="10dp"
                android:drawableStart="@drawable/arrow_right"
                android:fontFamily="monospace"
                android:text="@{item.word}"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tvType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{item.type}"
                android:layout_marginEnd="10dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@id/tvWord" />
        </androidx.constraintlayout.widget.ConstraintLayout>


    </com.google.android.material.card.MaterialCardView>

适配器类

class MyAdapter(private var itemList: List<Item>, private val listener: ItemClickListener) :
    RecyclerView.Adapter<MyAdapter.VHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = RvSampleBinding.inflate(inflater)
        return VHolder(binding)
    }

    override fun getItemCount(): Int = itemList.size

    override fun onBindViewHolder(holder: VHolder, position: Int) = holder.bind(itemList[position])

    inner class VHolder(private val binding: RvSampleBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Item) {
            binding.item = item
            binding.root.setOnClickListener {
                listener.onItemClick(item)
            }
            binding.executePendingBindings()
        }
    }

布局预览截图

以及实际输出的截图。

我已经在多个模拟器和设备上运行了该应用程序,但不知道问题出在哪里。

【问题讨论】:

  • 适配器代码,请
  • 这很奇怪,因为我已将您的 rv_sample 布局复制粘贴到我的 recyclerview 实现中,并且效果很好。可能是适配器有问题
  • 您的问题似乎是您的项目布局在没有通过父 ViewGroup 的情况下被夸大。这是一个常见问题,并导致宽度换行,即使您可能将match_parent 用于layout_width。由于LayoutInflater 没有父级,因此会生成默认的LayoutParams,并且会在两个方向上环绕。我不使用数据绑定,但是 AFAICT,应该有一个 RvSampleBinding.inflate() 重载创建了 3 个参数:LayoutInflater、父 ViewGroupboolean,所以 RvSampleBinding.inflate(inflater, parent, false)。跨度>
  • 感谢@MikeM 的有用评论。目前,我在办公室,我会在空闲时间尝试并告诉你。
  • 再次感谢@MikeM。我已经根据您的评论解决了我的问题。

标签: android android-layout android-recyclerview androidx


【解决方案1】:

在这里,我将回答我自己的问题。所以这个答案将帮助其他面临同样问题的用户。

感谢 @MikeM 的有益评论。所有功劳归于他。

问题:问题出在我的适配器上,我在没有通过父 viewgroup 的情况下膨胀了 item-layout

原因:如果你没有在你的layout-inflater 中传递parent,那将inflate 你的layout 默认layout-paramswrap 你的content在两个方向(宽度和高度)上,即使您可能将 match_parent 用于 layout_width

就我而言,我已经尝试过这样的

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
     val inflater = LayoutInflater.from(parent.context)
     val binding = RvSampleBinding.inflate(inflater) // here i didn't pass viewgroup and boolean parameters.
     return VHolder(binding)
}

解决方案:我的问题通过如下修改onCreateViewHolder来解决。

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
     val inflater = LayoutInflater.from(parent.context)
     val binding = RvSampleBinding.inflate(inflater,parent,false) //here i have passed viewgroup and boolean parameter and that's solve my problem
     return VHolder(binding)
}

【讨论】:

  • 救了我的命。找不到任何线索。谢谢。
【解决方案2】:

所以基本上你的父布局是约束布局,所以你必须添加与所有视图相关的所有约束,一个建议只是为了检查/验证目的使用另一个父布局而不是约束布局。

【讨论】:

  • 这更像是一个评论,你有足够的声誉来评论
【解决方案3】:
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".RecyclerViewActivity"
        android:id="@+id/recyclerView_ex1">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/recycler_view_item_ex1" />
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            android:src="@drawable/ic_add"
            android:id="@+id/fabAdd"
            app:tint="@color/cardview_light_background"
            app:backgroundTint="@color/colorPrimary"
            android:layout_marginEnd="5dp"
            android:layout_marginBottom="5dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_height="wrap_content"/>
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>

项目视图

    <?xml version="1.0" encoding="utf-8"?>
    <com.google.android.material.card.MaterialCardView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:padding="5dp"
    app:cardCornerRadius="10dp"
    android:minHeight="60dp"
    app:cardUseCompatPadding="true"
    app:strokeColor="@color/colorAccent"
    app:strokeWidth="1dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tool:context=".RecyclerViewActivity">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/tvWord"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="10dp"
            android:drawableStart="@drawable/arrow_right"
            android:fontFamily="monospace"
            android:text="word"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    
        <TextView
            android:id="@+id/tvType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="type"
            android:layout_marginEnd="10dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@id/tvWord"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    
    </com.google.android.material.card.MaterialCardView>

如果可行,请投票。

愉快的编码。谢谢

【讨论】:

  • 你能告诉我我的设计有什么问题吗?我认为它(我的设计)应该可以工作,因为在布局预览中它按预期显示。
  • 我没有看到您的 xml 文件有任何问题。如您所见,我刚刚使用了您的观点。如果您发布绑定/适配器示例代码会更好地理解。
  • Adapter 课程已添加到我更新的问题中。你可以看到我更新的问题。
猜你喜欢
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多