You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

水平RecyclerView中TextView文本省略失效问题求助

解决水平RecyclerView中文本省略失效的问题

问题描述

在水平方向的RecyclerView中,Item内的AlohaTextView无法正常触发文本末尾省略效果,单独预览Item布局时该功能正常。推测原因是水平RecyclerView未正确约束Item宽度,导致TextView可以无限制延伸显示全部文本。

相关布局代码

Item布局(service_info_slot.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/service_info_slot_parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:gravity="center_vertical"
    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:orientation="horizontal">

    <com.gojek.asphalt.aloha.text.AlohaTextView
        android:id="@+id/slot_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:maxLines="1"
        tools:text="I went to buy a chicken but there was no chicken to buy so I bought fish"
        app:aloha_typographyStyle="caption_moderate_book_default" />
</LinearLayout>

RecyclerView所在布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/service_info_slot_parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:gravity="center_vertical"
    android:layout_marginBottom="4dp"
    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">

       <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/service_info_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:listitem="@layout/service_info_slot"
        tools:itemCount="3"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>

解决方案

1. 约束RecyclerView的宽度

将RecyclerView的android:layout_width从wrap_content改为match_parent(或设置固定宽度,如300dp),确保RecyclerView有明确的宽度限制,这样Item才能获得有效的宽度约束:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/service_info_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:listitem="@layout/service_info_slot"
    tools:itemCount="3"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

2. 调整Item内TextView的宽度约束

修改Item布局中AlohaTextView的android:layout_width为match_parent,同时将根LinearLayout的android:layout_width设为match_parent,让TextView在Item的可用宽度内自动触发省略:

<!-- 修改后的Item布局 -->
<LinearLayout android:id="@+id/service_info_slot_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:gravity="center_vertical"
    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:orientation="horizontal">

    <com.gojek.asphalt.aloha.text.AlohaTextView
        android:id="@+id/slot_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:maxLines="1"
        tools:text="I went to buy a chicken but there was no chicken to buy so I bought fish"
        app:aloha_typographyStyle="caption_moderate_book_default" />
</LinearLayout>

3. 可选:手动触发文本测量(极端情况)

如果上述布局调整后仍不生效,可在Adapter的onBindViewHolder方法中手动触发TextView的重新测量,强制启用省略逻辑:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    String text = mData.get(position);
    holder.slotText.setText(text);
    // 强制重新测量并应用省略规则
    holder.slotText.post(() -> {
        holder.slotText.setEllipsize(TextUtils.TruncateAt.END);
        holder.slotText.setSingleLine(true);
        holder.slotText.requestLayout();
    });
}

内容的提问来源于stack exchange,提问作者Vidya Mohanraj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:44:53