【发布时间】: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、父ViewGroup和boolean,所以RvSampleBinding.inflate(inflater, parent, false)。跨度> -
感谢@MikeM 的有用评论。目前,我在办公室,我会在空闲时间尝试并告诉你。
-
再次感谢@MikeM。我已经根据您的评论解决了我的问题。
标签: android android-layout android-recyclerview androidx