Jak*_*ain 1 android android-layout android-recyclerview androidx
在我的项目中,我已经按照我的预期使用了RecyclerViewwhich 显示,layout-preview但问题是当我运行applicationin emulator/未显示device的项目时。RecyclerViewlayout-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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
适配器类
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()
}
}
Run Code Online (Sandbox Code Playgroud)
布局预览的屏幕截图
和实际输出的截图。
我已经在多个模拟器和设备中运行了该应用程序,但无法弄清楚问题出在哪里。
在这里,我将回答我自己的问题。因此,此答案将帮助将面临相同问题的其他用户。
谢谢,@MikeM的有用评论。所有的功劳都归功于他。
问题:问题出在我的适配器上,我在item-layout没有父级viewgroup通过的情况下膨胀了。
原因:如果你没有通过parent你layout-inflater,将inflate你的layout默认layout-params和wrap你的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)
}
Run Code Online (Sandbox Code Playgroud)
解决方案:我的问题通过修改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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
920 次 |
| 最近记录: |