ConstraintLayout下设置RecyclerView动态高度仅显示第一项问题求助
问题原因
- 点击添加按钮的逻辑错误:每次新增条目都重新实例化Adapter并重新赋值给RecyclerView,没有复用原有Adapter,导致高度计算异常
- 父布局控件设置了硬编码的margin,约束逻辑冲突:比如FloatingActionButton设置了
android:layout_marginBottom="492dp",底部按钮也设置了android:layout_marginTop="444dp",这些硬编码边距限制了控件的动态移动,新增条目后 RecyclerView 高度扩展的空间被锁死 - RecyclerView的布局配置有冗余冲突:同时设置了
wrap_content高度和app:layout_constrainedHeight="true",还有layout_constraintVertical_bias属性,约束逻辑冲突导致高度计算异常 - 条目布局高度冲突:TextInputLayout和内部的TextInputEditText设置了不一致的固定高度,导致条目测量异常
修复步骤
1. 修改Fragment中添加条目的逻辑
不要每次点击都新建Adapter,复用原有Adapter实例即可,修改对应代码:
// 把adapter声明改成具体的RecyclerAdapter类型,不要用抽象的RecyclerView.Adapter private var adapter: RecyclerAdapter? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.recyclerView.apply { layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false) // 初始化一次Adapter即可 val recyclerAdapter = RecyclerAdapter(items) adapter = recyclerAdapter // 把adapter实例存到全局变量 this@SecondFragment.adapter = recyclerAdapter } binding.cancelButton.setOnClickListener { findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment) } binding.addItemActionButton.setOnClickListener { items.add("Testing..") // 直接调用原有adapter的通知方法,不要新建adapter也不要重新绑定 adapter?.notifyItemInserted(items.size - 1) } }
2. 修复父布局的约束冲突,删掉所有硬编码边距
父Fragment的布局修改后如下:
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondFragment"> <Button android:id="@+id/cancel_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancel" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/cancel_button2" app:layout_constraintStart_toStartOf="parent" android:layout_marginBottom="16dp"/> <Button android:id="@+id/cancel_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ok" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintLeft_toRightOf="@id/cancel_button" android:layout_marginBottom="16dp"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginHorizontal="16dp" android:layout_marginTop="16dp" app:layout_constrainedHeight="true" app:layout_constraintBottom_toTopOf="@id/addItemActionButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/addItemActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:src="@android:drawable/ic_input_add" app:layout_constraintBottom_toTopOf="@id/cancel_button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginVertical="16dp" tools:ignore="SpeakableTextPresentCheck" /> </androidx.constraintlayout.widget.ConstraintLayout>
3. 修复条目布局的高度冲突
修改条目布局的高度参数,避免固定高度导致的测量异常:
<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:padding="8dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/item" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/item" /> </com.google.android.material.textfield.TextInputLayout> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者user3362334
相关产品推荐
相关产品推荐

