java - RecylerView 项目的 maxWidth 因大量元素而变化
问题描述
所以我正在构建一个聊天应用程序。我曾经RecyclerView
显示消息并将maxWidth
消息列表项的设置为设备宽度的 75%,并且在布局中,宽度是0dp
动态ConstraintLayout
的。这使得它对于较小的文本更加简洁,当它是一个更长的文本时,它只是在达到 maxWidth 后将其发送到下一行。在项目列表增加之前,一切都运行良好。当有很多数据时,RecyclerView
使宽度像上一个列表项(我知道RecyclerView
回收视图)。一个解决方案是setIsRecyclable(false);
在ViewHolder
Adapter
. 这确实有效,但列表项相互重叠,或者当一个列表项的数据发生更改时,它会重叠自身,在自身上创建另一项作为更改项。由于它不会回收视图,因此它会在列表项发生任何更改后自行创建新视图。我不知道该怎么办。我应该求助于ListView
吗?或者还有其他解决方案吗?为了上下文,这是我的代码。
ViewHolder
环境maxWidth
public ChatViewHolder(@NonNull @NotNull View itemView, int width) {
super(itemView);
...
...
sendContainer.setMaxWidth(width);
comeContainer.setMaxWidth(width);
}
onCreateViewHolder
计算maxWidth
@Override
public ChatViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.message_list_item, parent, false);
int width = (int) (parent.getWidth() * 0.75);
return new ChatViewHolder(view, width);
}
解决方案
我认为您实际上不需要在代码本身中做任何事情。约束布局本身就有能力实现这一点。这是我曾经使用过的聊天设计示例。
<?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="wrap_content"
android:background="@color/colorPrimaryBackground">
<TextView
android:id="@+id/tvMsgSelf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:background="@drawable/drawable_chat_self"
android:paddingStart="12dp"
android:paddingTop="16dp"
android:paddingEnd="12dp"
android:paddingBottom="16dp"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="wrap"
app:layout_constraintWidth_percent="0.72"
tools:text="dgdgdgdgdfddg dfgdf gdf gdf gdf gdf gdfd g dg dfg dfg df gd gdf gdf g dfg dfg dfg dfg df g dfg df g dfgdfg" />
<TextView
android:id="@+id/tvTimeStampSelf"
style="@style/StyleRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textColor="@color/colorSecondaryText"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="@+id/tvMsgSelf"
app:layout_constraintTop_toBottomOf="@+id/tvMsgSelf"
tools:text="2 Jan,20, 10:50PM" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里的主要属性是app:layout_constraintWidth_percent="0.72"
和app:layout_constraintWidth_max="wrap"
。这将其宽度限制为从那里开始的任何给定屏幕的 72%。
推荐阅读
- ios - 如何防止多次调用异步函数但为每个函数调用完成?
- python - 在 pyspark 中获取不同的行数
- spring - JPA 和 QueryDSL 动态查询很重要
- angular - Angular matDialog 应该显示两个带有样式的空 div
- python - 如何使 Django-select2 样式起作用
- admob - 我已经实现了 app-ads.txt 但我也说“你还没有设置 app-ads.txt”,
- c# - 如何在谷歌云功能 .NET Core 3.1 上启用 CORS
- ios - Firebase [Swift Package Manager]:没有这样的模块 FirebaseRemoteConfig
- python - 从输入到 Django url 参数的参数 ID - 可能吗?
- rxjs - Rxjs,组合多个组的最后一个查询结果(组合switchMap与groupBy)