Android 回收器查看项目宽度与父项不匹配,预览与模拟器不匹配



这是我的回收器视图项xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recyclerItem"
app:cardElevation="10dp"
app:cardUseCompatPadding="false"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/recyclerItemImageView"
android:padding="8dp"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="@+id/recyclerItemTextView"
android:layout_marginStart="4dp"
android:textSize="18sp"
android:textColor="@color/black"
android:text="item"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>

预览显示如下:

预览

但模拟器的结果是:

模拟器结果

所以回收器视图中的项目和模拟器上的父宽度不匹配。其他xml有:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:layout_margin="16dp"
android:name="com.example.databinding_practice.ListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/activityTextView"/>
<TextView
android:id="@+id/activityTextView"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="@color/black"
android:textSize="20sp" />
</RelativeLayout>

和片段:

<?xml version="1.0" encoding="utf-8"?>
<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=".ListFragment">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

适配器类和ListFragment如下:

再循环适配器.kt:

class RecyclerAdapter(var recyclerItems: ArrayList<RecyclerItemModel>) : RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>() {
class RecyclerViewHolder(val binding: RecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val binding = RecyclerItemBinding.inflate(LayoutInflater.from(parent.context))
return RecyclerViewHolder(binding)
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
holder.binding.recyclerItemTextView.text = recyclerItems[position].text
recyclerItems[position].imageMipmapSource?.let { holder.binding.recyclerItemImageView.setImageResource(it) }
}
override fun getItemCount(): Int {
return recyclerItems.size
}
}

ListFragment.kt:

class ListFragment : Fragment() {
private var itemList : ArrayList<RecyclerItemModel> = arrayListOf()
private var recyclerAdapter: RecyclerAdapter = RecyclerAdapter(arrayListOf())
private lateinit var fragmentListBinding: FragmentListBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
fragmentListBinding = FragmentListBinding.inflate(layoutInflater)
return fragmentListBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

for (i in 0..3){
itemList.add(RecyclerItemModel(R.mipmap.ic_launcher,"Item $i"))
}

fragmentListBinding.recyclerView.layoutManager = LinearLayoutManager(context)
fragmentListBinding.recyclerView.adapter = recyclerAdapter
recyclerAdapter.recyclerItems = itemList
recyclerAdapter.notifyDataSetChanged()
}

我哪里做错了?如果我将matchparent替换为固定大小,如200dp-300dp,这是正确的,但matchparents不正确。为什么预览和模拟器不匹配?非常感谢。

在片段中,您应该将layout_widthlayout_height更改为"0dp">在您的RecyclerView中。

<?xml version="1.0" encoding="utf-8"?>
<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=".ListFragment">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

最新更新