android - 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 不会对大小做任何改变,更接近适配器和视图。
有人可以告诉我有什么问题吗?
解决方案
我找到了一个解决方案......问题出在 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_CONTENT
byViewGroup.LayoutParams.MATCH_PARENT
推荐阅读
- android - 恢复 Runnable() 导致应用程序错误
- javascript - 将序列号动态添加到标签
- oracle - Oracle Reports-如何将字段对象添加到重复帧
- android - 科尔多瓦 android 应用程序所有 REST 请求都处于待处理状态
- python - 从子列表列表中删除索引以返回它
- php - 注意:未定义索引:PHP 会话中的电子邮件错误
- locking - 资源预留模式的锁定和隔离
- flask - 使用烧瓶cli时烧瓶应用程序未在浏览器中打开
- javascript - 'animate()' 函数我的代码不起作用,但 'css()' 起作用。怎么了?
- python - 将熊猫多索引系列转换为 Json python