首页 > 解决方案 > RecylerView 项目的 maxWidth 因大量元素而变化

问题描述

所以我正在构建一个聊天应用程序。我曾经RecyclerView显示消息并将maxWidth消息列表项的设置为设备宽度的 75%,并且在布局中,宽度是0dp动态ConstraintLayout的。这使得它对于较小的文本更加简洁,当它是一个更长的文本时,它只是在达到 maxWidth 后将其发送到下一行。在项目列表增加之前,一切都运行良好。当有很多数据时,RecyclerView使宽度像上一个列表项(我知道RecyclerView回收视图)。一个解决方案是setIsRecyclable(false);ViewHolderAdapter. 这确实有效,但列表项相互重叠,或者当一个列表项的数据发生更改时,它会重叠自身,在自身上创建另一项作为更改项。由于它不会回收视图,因此它会在列表项发生任何更改后自行创建新视图。我不知道该怎么办。我应该求助于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);
    }

标签: javaandroidandroid-recyclerview

解决方案


我认为您实际上不需要在代码本身中做任何事情。约束布局本身就有能力实现这一点。这是我曾经使用过的聊天设计示例。

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


推荐阅读