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

垂直RecyclerView嵌套水平RecyclerView,动态元素尺寸计算异常

解决嵌套RecyclerView子项宽度适配异常问题

问题背景

垂直分组列表中嵌套水平RecyclerView,子项宽度需适配动态文本内容,但10%的子项出现宽度异常(过大/过小导致文本截断),其余90%显示正常。

核心解决方案

1. 修复子项布局的测量逻辑

原布局中CardView直接包裹TextView的方式,会导致CardView无法正确计算内部最宽文本的宽度。修改为用LinearLayout作为容器,让CardView基于内部最宽的TextView自动适配宽度:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="73dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="4dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="29dp"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:singleLine="true"
            android:layout_marginTop="7dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:singleLine="true"
            android:layout_marginTop="7dp" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

2. 优化嵌套RecyclerView的初始化逻辑

避免在onBindViewHolder中重复创建LinearLayoutManager和Adapter,改为在ViewHolder初始化时一次性配置,仅更新数据:

// 父RecyclerView的ViewHolder类
public class ParentViewHolder extends RecyclerView.ViewHolder {
    private final RecyclerView nestedRecyclerView;
    private final RecyclerViewAdapterContentBoxes nestedAdapter;

    public ParentViewHolder(View itemView) {
        super(itemView);
        nestedRecyclerView = itemView.findViewById(R.id.tasks_expansion).findViewById(R.id.rv_expansion_tasks);
        
        // 仅初始化一次LayoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(
            nestedRecyclerView.getContext(),
            LinearLayoutManager.HORIZONTAL,
            false
        );
        nestedRecyclerView.setLayoutManager(layoutManager);
        nestedRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        nestedRecyclerView.setHasFixedSize(false); // 允许宽度动态变化
        
        // 初始化Adapter,后续仅更新数据
        nestedAdapter = new RecyclerViewAdapterContentBoxes(nestedRecyclerView.getContext());
        nestedRecyclerView.setAdapter(nestedAdapter);
    }

    // 绑定数据时仅更新Adapter内容
    public void bind(MenuItem menuItem) {
        nestedAdapter.setData(menuItem.getContentBoxes());
        nestedAdapter.notifyDataSetChanged();
        nestedRecyclerView.requestLayout();
    }
}

3. 修复子Adapter的文本绑定逻辑

在子Adapter的onBindViewHolder中,设置文本后强制View重新计算尺寸,避免复用旧的错误尺寸:

@Override
public void onBindViewHolder(ContentBoxViewHolder holder, int position) {
    ContentBox item = mData.get(position);
    holder.text1.setText(item.getText1());
    holder.text2.setText(item.getText2());
    holder.text3.setText(item.getText3());
    
    // 强制View重新测量宽度
    holder.itemView.requestLayout();
}

4. 关闭预取优化(可选)

若仍存在异常,可尝试关闭嵌套RecyclerView的预取功能,避免预取时测量错误:

nestedRecyclerView.setItemViewCacheSize(0);
layoutManager.setInitialPrefetchItemCount(0);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 02:20:36