【问题标题】:RecyclerView holder always is wrap_contentRecyclerView 持有者始终是 wrap_content
【发布时间】:2019-10-19 21:44:58
【问题描述】:

我有这个布局和一个 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 不会对大小做任何改变,更靠近适配器和视图。

谁能告诉我怎么了?

【问题讨论】:

  • 我在下面看到了您的答案,但这不是必需的。我怀疑你没有正确地膨胀你的视图持有者的项目视图。它应该看起来像 inflate(R.layout.foo, parent, false) 而不是 inflate(R.layout.foo, null)。是这个问题吗?

标签: android android-recyclerview


【解决方案1】:

我找到了解决方案...问题与 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.MATCH_PARENT覆盖了ViewGroup.LayoutParams.WRAP_CONTENT

【讨论】:

    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 2023-03-11
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多