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

针对含可展开内容的Activity布局,用单个还是两个RecyclerView更优?

实现带可折叠正文的Activity布局方案

嘿,我来给你搞定这个布局需求!其实你完全没必要用Expandable RecyclerView——毕竟你只是单个页面里的正文折叠,用TextView自带的属性+简单的点击逻辑就能完美实现,比搞复杂的列表组件省心多了。下面是完整的实现步骤:

1. 布局文件(activity_main.xml)

先把整体结构搭好:标题TextView、可折叠的正文区域、底部操作栏LinearLayout,代码里我加了详细注释:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- 顶部标题TextView -->
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你的Activity标题"
        android:textSize="24sp"
        android:textStyle="bold"
        android:marginBottom="16dp"/>

    <!-- 可折叠正文区域:TextView + Read More按钮 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="这里放你的长正文内容,比如:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 再加点内容凑够超过5行的长度..."
            android:textSize="16sp"
            android:maxLines="5" <!-- 初始只显示5行 -->
            android:ellipsize="end"/> <!-- 末尾显示省略号,提示还有更多内容 -->

        <Button
            android:id="@+id/btn_read_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Read More"
            android:layout_gravity="end"/> <!-- 按钮靠右对齐 -->
    </LinearLayout>

    <!-- 底部操作栏LinearLayout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="24dp"
        android:paddingVertical="12dp"
        android:background="#f5f5f5">

        <Button
            android:id="@+id/btn_action1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="操作按钮1"
            android:layout_marginHorizontal="8dp"/>

        <Button
            android:id="@+id/btn_action2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="操作按钮2"
            android:layout_marginHorizontal="8dp"/>
    </LinearLayout>

</LinearLayout>

2. Java代码实现折叠/展开逻辑(MainActivity.java)

接下来在Activity里处理按钮点击,控制TextView的显示行数,逻辑超简单:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvContent;
    private Button btnReadMore;
    private boolean isContentExpanded = false; // 标记正文是否展开

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 绑定控件
        tvContent = findViewById(R.id.tv_content);
        btnReadMore = findViewById(R.id.btn_read_more);

        // 设置按钮点击事件
        btnReadMore.setOnClickListener(v -> {
            if (!isContentExpanded) {
                // 展开正文:设置行数不限,按钮文字改为"Read Less"
                tvContent.setMaxLines(Integer.MAX_VALUE);
                btnReadMore.setText("Read Less");
                isContentExpanded = true;
            } else {
                // 折叠正文:恢复为5行,按钮文字改回"Read More"
                tvContent.setMaxLines(5);
                btnReadMore.setText("Read More");
                isContentExpanded = false;
            }
        });
    }
}

3. 额外说明(如果需要多个TextView作为正文)

如果你的正文必须拆分成多个TextView(比如不同样式的段落),那可以把这些TextView放在一个LinearLayout里作为正文容器,然后通过ViewTreeObserver获取单个TextView的行高,计算出5行的总高度,给容器设置android:maxHeight为该高度。点击按钮时,切换容器的maxHeight为wrap_content或者计算好的固定高度,同时配合android:overflow="scroll"来处理超出部分的滚动。不过单个TextView的方案是最简洁的,能合并的话优先用单个TextView。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:25:12