You need to enable JavaScript to run this app. RecyclerView无法完整展示API获取内容,高度被首项固定如何修复
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView无法完整展示API获取内容,高度被首项固定如何修复

修复方案

1. 修复列表项布局文本截断问题

修改列表项row.xml的代码,调整布局逻辑和文本属性:

<RelativeLayout
    android:layout_weight="3.3"
    <!-- 把写死的宽度改成0dp,让weight属性生效 -->
    android:layout_width="0dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name of the file"
        android:padding="8dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_marginLeft="80dp"
        android:textColor="#ffffff"/>
    <TextView
        android:id="@+id/txtQuoteid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="rs_299"
        android:padding="8dp"
        android:layout_toRightOf="@id/txtTitle"
        <!-- 让id始终靠右侧对齐 -->
        android:layout_alignParentEnd="true"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="#ffffff"/>
    <TextView
        android:id="@+id/txtQuote"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtTitle"
        android:text="name_of_the_author"
        android:padding="8dp"
        <!-- 让文本不与右侧的id重叠 -->
        android:layout_toLeftOf="@id/txtQuoteid"
        <!-- 支持多行显示,避免截断 -->
        android:maxLines="99"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textSize="19sp"/>
</RelativeLayout>

2. 修复RecyclerView高度被第一项固定问题

2.1 先修正RecyclerView的约束错误

原布局中RecyclerView和Toolbar重叠,修改activity_descactivity.xml中RecyclerView的约束:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    <!-- 把约束从parent顶部改成Toolbar底部,避免重叠 -->
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintBottom_toBottomOf="parent"/>

2.2 若需要RecyclerView高度自适应最高子项

如果不要RecyclerView占满剩余高度,要自适应内容高度,就自定义横向LinearLayoutManager,替换原有的布局管理器初始化代码:

layoutmanager = object : LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) {
    override fun onMeasure(recycler: RecyclerView.Recycler, state: RecyclerView.State, widthSpec: Int, heightSpec: Int) {
        var maxHeight = 0
        // 遍历所有子项,取最高的作为RecyclerView的高度
        for (i in 0 until itemCount) {
            val itemView = recycler.getViewForPosition(i)
            measureChild(itemView, widthSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
            val itemHeight = itemView.measuredHeight
            if (itemHeight > maxHeight) maxHeight = itemHeight
            recycler.recycleView(itemView)
        }
        val newHeightSpec = View.MeasureSpec.makeMeasureSpec(maxHeight, View.MeasureSpec.EXACTLY)
        super.onMeasure(recycler, state, widthSpec, newHeightSpec)
    }
}

3. 修复Activity业务逻辑冗余问题

原代码在循环中每次新增数据都重新设置适配器,性能损耗严重,把适配器初始化和绑定逻辑移到循环外:

try {
    if (success) {
        progressLayout.visibility = View.GONE
        val data = it.getJSONArray("data")
        for (i in 0 until data.length()) {
            val bookJsonObject = data.getJSONObject(i)
            val BookObject = Book(
                bookJsonObject.getString("_id"),
                bookJsonObject.getString("title"),
                bookJsonObject.getString("quoteid"),
                bookJsonObject.getString("quote")
            )
            bookInfoList.add(BookObject)
        }
        // 所有数据加载完成后再设置适配器和布局管理器
        adapter = Horizontal_RecyclerView(this@descactivity, bookInfoList)
        recyclerview.adapter = adapter
        recyclerview.layoutManager = layoutmanager
    }
} catch (e: JSONException) {
    Toast.makeText(this, "some unknown error occurred", Toast.LENGTH_SHORT).show()
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 20:45:02