首页 > 解决方案 > RecyclerView 持有者始终是 wrap_content

问题描述

我有一个带有recyclerview的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.ContactFragment">

<data>

    <variable
        name="vm"
        type="corp.br.UserInfoViewModel" />


</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:padding="@dimen/default_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="@string/account_destination_label"
        android:id="@+id/accountDestinationTv"
        style="@style/DefaulAppearance.Title"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/contactsRv"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_marginTop="@dimen/default_margin"
        app:layout_constraintTop_toBottomOf="@+id/accountDestinationTv"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我有这个持有人:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".adapter.holder.BaseHolder">

<data>
    <variable
        name="name"
        type="String" />
</data>

<TextView
    android:id="@+id/item_contact_name"
    android:text="@{name}"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/default_margin"
    style="@style/DefaulAppearance"
    tools:text="teste"
    />

</layout>

由于某种原因,当我使用 Layout Inspector 时,每个持有者项目都具有 wrap_content 属性,并且单击仅在我单击文本而不是整行时才有效。我的片段有这个 recyclerview 不会对大小做任何改变,更接近适配器和视图。

有人可以告诉我有什么问题吗?

标签: androidandroid-recyclerview

解决方案


我找到了一个解决方案......问题出在 LinearLayoutManager 上。

导致我的问题的默认 LinearLayoutManager 代码:

@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}

我在片段中应用的解决方案:

bind.contactsRv.layoutManager = object : LinearLayoutManager(this@ContactFragment.context) {
        override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
            return RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

我只是覆盖了LayoutParams 的第一个参数中的ViewGroup.LayoutParams.WRAP_CONTENTbyViewGroup.LayoutParams.MATCH_PARENT


推荐阅读