在我的项目中,我按预期使用了RecyclerView
中显示的layout-preview
,但是问题是我在application
/ {{1 }} emulator
的项目未显示为device
。
这是recyclerview的XML。
RecyclerView
recyclerview项目的XML。
layout-preview
适配器类
<?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>
布局预览的屏幕截图
以及实际输出的屏幕截图。
我已经在多个仿真器和设备中运行了该应用程序,但不知道问题出在哪里。
答案 0 :(得分:0)
因此,基本上,您的父布局是约束布局,因此您必须添加与所有视图相关的所有约束。一个建议只是使用另一父布局而不是约束布局,仅用于检查/验证目的。
答案 1 :(得分:0)
<?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>
itemview
<?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>
如果可行,请投票。
编码愉快。谢谢
答案 2 :(得分:0)
在这里,我将回答我自己的问题。这样,该答案将有助于将面临相同问题的其他用户。
感谢 @MikeM 的有用评论。一切归功于他。
问题:问题出在我的适配器上,我在夸张item-layout
而未通过父viewgroup
的情况下。
原因:如果您没有在parent
中传递layout-inflater
,则inflate
将layout
设置为默认{{1} },并且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)
}
来解决的,如下所示。
onCreateViewHolder