android - Recyclerview溢出ConstraintLayout
问题描述
我正在尝试创建一个 recyclerview,它的大小随着其他项目的增加而增长,直到达到某个最大高度,然后逐渐消失。我知道约束布局是去这里的正确方法,我发誓这已经在一个月左右前工作了,然后 Recyclerview 不再关心它的约束并且在 280dp 之后可见(见图)。这是我的代码。我确定这已经奏效了,我不知道谷歌是否将某些内容更改为实现“androidx.constraintlayout:constraintlayout:2.0.0-beta4”,或者我是否正在慢慢发疯。也许有人知道如何解决这个问题。也许它的实现是测试版。任何帮助都感激不尽。
<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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_tilemap"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
app:layout_constrainedHeight="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:fadingEdge="horizontal"
android:fadingEdgeLength="30dp"
android:fillViewport="true"
android:requiresFadingEdge="vertical"
android:id="@+id/rv_comms"
android:padding="0dp"
android:clipToPadding="false" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
我刚刚找到了有罪的代码。原来我的适配器中的录音按钮是罪魁祸首。他们包括以下方法,使按钮能够溢出其他适配器项目的视图(按下按钮时,按钮会像 whatsapp 一样增长,见下图)
public void setClip(View v) {
if (v.getParent() == null) {
return;
}
if (v instanceof ViewGroup) {
((ViewGroup) v).setClipChildren(false);
((ViewGroup) v).setClipToPadding(false);
}
if (v.getParent() instanceof View) {//this part is to blame
setClip((View) v.getParent());
}
}
该方法的递归性质(已在该方法的最后 4 行中已注释掉)基本上将 clipToPadding 和 clipChildren 设置为 false 以用于从记录按钮向上的整个视图层次结构,这会导致奇怪的溢出。我最终手动将clipToPadding 和clipChildren 设置为false,仅针对那些与适配器有关的视图(这使我能够保留很酷的录制按钮溢出动画而没有褪色边缘问题),现在它看起来很漂亮(见下文)。
推荐阅读
- spring-boot - Postman 中的 HttpServletRequest 请求提交
- android - Android 10 GPS Pings 在后台
- elasticsearch - 每当 logstash 重新启动时,sql_last_value 都会重置
- angular - 如何在 RxJS Observable 上同时调用 next 和 error
- powershell - 如何使用 PowerShell 在启用 FIPS 的服务器上压缩/7z 压缩文件
- r - 如何替换嵌套的 for 循环?
- javascript - 如何总结我在 forEach 循环中生成的变量中的数字列表
- android - 如何更改特定布局组件的颜色?
- python - 合并字典和标签
- windows - 将计算机主机名、操作系统以及是否启用 Bitlocker 导出到 CSV 的 PowerShell 脚本