0dp layout_width 未按预期为回收视图项中的约束布局子项工作 [英] 0dp layout_width not working as expected for constraint layout child in recyclerview item

查看:43
本文介绍了0dp layout_width 未按预期为回收视图项中的约束布局子项工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缩小工作布局代码,

<?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,

The above code renders the recycler view as expected.
But as per docs,

您不能对 ConstraintLayout 中的任何视图使用 match_parent.而是使用匹配约束"(0dp).

You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).

所以,我替换了这一行,

So, I replaced this line,

android:layout_width="match_parent"

有了这个

android:layout_width="0dp"

但是,它不起作用.recyclerview 不可见.

But, it is not working. The recyclerview is not visible.

完整的 repo 是开源的,https://github.com/Abhimanyu14/cat-fact

Complete repo is open-source, https://github.com/Abhimanyu14/cat-fact

Recyclerview 适配器代码(如果需要),

Recyclerview adapter code if required,

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))
    }
}

推荐答案

正如你所说,文档说:

您不能对 ConstraintLayout 中的任何视图使用 match_parent.而是使用匹配约束"(0dp).

You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).

文档过去还说结果是不可预测的.我猜不可预测"意思是它有效"(有时.)

The docs used to also say that the result would be unpredictable. I guess "unpredictable" means "it works" (sometimes.)

使用 0dp 是正确的.您遇到的问题是您要膨胀的视图需要对其父视图的引用才能知道它可以有多大,因此您需要提供父引用(下面 inflate 中的第二个参数)但不要将膨胀的视图附加到父视图(第三个参数),因为 RecyclerView 会处理这个.

Using 0dp is correct. The problem you are having is that the view you're inflating needs a reference to its parent view to know how big it can be, so you need to give a parent reference (2nd argument in inflate below) but not attach the inflated view to the parent view (3rd argument) since the RecyclerView will take care of that.

return MainActivityRecyclerViewHolder( RecyclerviewItemLayoutBinding
    .inflate( LayoutInflater.from( parent.context ), parent, false )

这篇关于0dp layout_width 未按预期为回收视图项中的约束布局子项工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆