回收器视图持有人始终是wrap_content



我有这个带有回收器视图的布局:

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

出于某种原因,当我使用布局检查器时,每个 holder 项目都具有 wrap_content 属性,并且单击仅在我单击文本而不是整行时有效。我具有此回收器视图的片段不会对大小进行任何更改,适配器和视图持有人不会更改。

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

我找到了解决方案...问题在于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)
}
}

我只是通过在布局参数的第一个参数中ViewGroup.LayoutParams.MATCH_PARENT来覆盖ViewGroup.LayoutParams.WRAP_CONTENT

最新更新