0dp layout_width 对于 recyclerview 项目中的约束布局子项没有按预期工作
0dp layout_width not working as expected for constraint layout child in recyclerview item
精简工作布局代码,
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
</data>
<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:id="@+id/recyclerview_item_layout_root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/recyclerview_item_layout_textview_fact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
以上代码按预期呈现回收器视图。
但根据 docs、
You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).
所以,我替换了这一行,
android:layout_width="match_parent"
有了这个
android:layout_width="0dp"
但是,它不起作用。 recyclerview
不可见。
完整的 repo 是开源的,
https://github.com/Abhimanyu14/cat-fact
如果需要,Recyclerview 适配器代码,
class HomeFragmentRecyclerViewAdapter :
PagingDataAdapter<CatFact, HomeFragmentRecyclerViewAdapter.MainActivityRecyclerViewHolder>(
CatFactDiffCallback
) {
class MainActivityRecyclerViewHolder(private var binding: RecyclerviewItemLayoutBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(catFact: CatFact?) {
catFact?.let {
binding.recyclerviewItemLayoutTextviewFact.text = String.format(
binding.root.context.resources.getString(R.string.recyclerview_item_layout_fact),
catFact.id,
catFact.fact
)
}
}
}
object CatFactDiffCallback : DiffUtil.ItemCallback<CatFact>() {
override fun areItemsTheSame(oldItem: CatFact, newItem: CatFact): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: CatFact, newItem: CatFact): Boolean {
return oldItem == newItem
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MainActivityRecyclerViewHolder {
return MainActivityRecyclerViewHolder(
RecyclerviewItemLayoutBinding.inflate(
LayoutInflater.from(
parent.context
)
)
)
}
override fun onBindViewHolder(holder: MainActivityRecyclerViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
正如您所说,文档说:
You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).
文档过去也说结果是不可预测的。我想“不可预测”意味着“它有效”(有时。)
使用0dp
是正确的。您遇到的问题是您正在膨胀的视图需要对其父视图的引用才能知道它有多大,因此您需要提供父引用(inflate[=21= 中的第二个参数] 下面),但不要将膨胀视图附加到父视图(第三个参数),因为 RecyclerView 会处理它。
return MainActivityRecyclerViewHolder( RecyclerviewItemLayoutBinding
.inflate( LayoutInflater.from( parent.context ), parent, false )
精简工作布局代码,
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
</data>
<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:id="@+id/recyclerview_item_layout_root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/recyclerview_item_layout_textview_fact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
以上代码按预期呈现回收器视图。
但根据 docs、
You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).
所以,我替换了这一行,
android:layout_width="match_parent"
有了这个
android:layout_width="0dp"
但是,它不起作用。 recyclerview
不可见。
完整的 repo 是开源的, https://github.com/Abhimanyu14/cat-fact
如果需要,Recyclerview 适配器代码,
class HomeFragmentRecyclerViewAdapter :
PagingDataAdapter<CatFact, HomeFragmentRecyclerViewAdapter.MainActivityRecyclerViewHolder>(
CatFactDiffCallback
) {
class MainActivityRecyclerViewHolder(private var binding: RecyclerviewItemLayoutBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(catFact: CatFact?) {
catFact?.let {
binding.recyclerviewItemLayoutTextviewFact.text = String.format(
binding.root.context.resources.getString(R.string.recyclerview_item_layout_fact),
catFact.id,
catFact.fact
)
}
}
}
object CatFactDiffCallback : DiffUtil.ItemCallback<CatFact>() {
override fun areItemsTheSame(oldItem: CatFact, newItem: CatFact): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: CatFact, newItem: CatFact): Boolean {
return oldItem == newItem
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MainActivityRecyclerViewHolder {
return MainActivityRecyclerViewHolder(
RecyclerviewItemLayoutBinding.inflate(
LayoutInflater.from(
parent.context
)
)
)
}
override fun onBindViewHolder(holder: MainActivityRecyclerViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
正如您所说,文档说:
You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).
文档过去也说结果是不可预测的。我想“不可预测”意味着“它有效”(有时。)
使用0dp
是正确的。您遇到的问题是您正在膨胀的视图需要对其父视图的引用才能知道它有多大,因此您需要提供父引用(inflate[=21= 中的第二个参数] 下面),但不要将膨胀视图附加到父视图(第三个参数),因为 RecyclerView 会处理它。
return MainActivityRecyclerViewHolder( RecyclerviewItemLayoutBinding
.inflate( LayoutInflater.from( parent.context ), parent, false )