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

RecyclerView按钮文本短暂显示后消失问题求助

问题分析与解决方案

核心问题与修复步骤

1. RecyclerView嵌套ScrollView引发布局异常

你的RecyclerView被包裹在ScrollView中,这会破坏RecyclerView自身的布局计算逻辑,导致列表项显示错乱(比如文本短暂显示后消失、高度计算异常)。RecyclerView自带滚动功能,完全不需要嵌套ScrollView。

修复方法:移除ScrollView,让RecyclerView直接作为ConstraintLayout的子View,调整约束关系:

<!-- 修改后的fragment_browse_recipes.xml -->
<?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="match_parent"
    tools:context=".BrowseRecipesFragment">

    <TextView
        android:id="@+id/textview_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here will be the search bar"
        android:textSize="20sp"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recipe_row_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textview_second"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:listitem="@layout/recipe_row_view" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. 数据更新后未通知Adapter

你在onViewCreated中从数据库获取数据并添加到recipesList,但Adapter未收到数据变化的通知——虽然初始绑定会让文本短暂显示,但后续布局重绘时就会丢失状态。

修复方法:数据加载完成后调用Adapter的notifyDataSetChanged():

// 修改BrowseRecipesFragment的onViewCreated方法
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // 从数据库获取食谱并更新列表
    val db = DBHelper(requireContext(), null)
    val cursor = db.getAllRecipes()
    recipesList.clear() // 清空旧数据,避免重复添加
    if(cursor != null && cursor.moveToFirst()){
        do {
            recipesList.add(RecipeRowViewModel(cursor.getString(cursor.getColumnIndex(DBHelper.RECIPE_NAME_COL))))
        } while(cursor.moveToNext())
        cursor.close()
        // 通知Adapter数据已更新
        customAdapter.notifyDataSetChanged()
    }
}

3. 移除不必要的双向绑定(可选优化)

recipe_row_view.xml中使用了双向绑定@={recipeRow.recipe},但Adapter已经通过代码手动设置按钮文本,双向绑定在这里没有实际作用,还可能引发潜在冲突。可以改为单向绑定:

<!-- 修改recipe_row_view.xml中的Button文本属性 -->
<Button
    android:id="@+id/recipe_name_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="@{recipeRow.recipe}">
</Button>

总结

主要问题是RecyclerView嵌套ScrollView导致布局计算错误,加上数据更新未通知Adapter,二者共同造成了文本显示后消失的现象。按照上述步骤修复后,就能正常显示食谱按钮列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 03:10:59