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

添加节目至数据库后RecyclerView无法稳定刷新的问题求助

解决RecyclerView添加节目后不稳定显示的问题

核心问题

你当前用startActivity启动添加页面,HomeFragment无法感知节目添加完成的事件,也无法获取新节目数据,导致适配器无法及时、稳定刷新。之前的尝试没抓住这个核心逻辑。

解决方案步骤

  • 改用Activity Result API启动AddShowActivity,监听添加完成后的返回结果
  • 在适配器中维护可修改的数据集合,提供添加数据的方法
  • 拿到返回的新节目数据后,更新适配器并通知刷新

修改后的代码示例

1. HomeFragment 修改

class HomeFragment(private val context: MainActivity) : Fragment() {
    private lateinit var verticalRecyclerView: RecyclerView
    private lateinit var buttonAddShow: Button
    private lateinit var showsAdapter: ShowAdapter
    // 注册Activity Result回调
    private val addShowLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // 获取返回的节目数据(假设你的节目实体类是Show)
            val newShow = result.data?.getParcelableExtra<Show>("NEW_SHOW")
            newShow?.let {
                // 添加到适配器并通知刷新
                showsAdapter.addShow(it)
            }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_home, container, false)
        setupComponents(view)
        initializeComponents()
        return view
    }

    private fun setupComponents(view: View) {
        verticalRecyclerView = view.findViewById(R.id.home_recycler_view)
        buttonAddShow = view.findViewById(R.id.home_button_add_show)
        // 初始化适配器时传入空的可变列表
        showsAdapter = ShowAdapter(context, R.layout.item_show, mutableListOf())
    }

    private fun initializeComponents() {
        verticalRecyclerView.adapter = showsAdapter
        verticalRecyclerView.layoutManager = LinearLayoutManager(context) // 必须设置布局管理器,否则RecyclerView不显示内容

        buttonAddShow.setOnClickListener{
            // 用launcher启动添加页面
            addShowLauncher.launch(Intent(context, AddShowActivity::class.java))
        }
    }
}

2. ShowAdapter 修改

确保适配器内部维护数据集合,并提供添加方法:

class ShowAdapter(
    private val context: Context,
    private val itemLayout: Int,
    private val showList: MutableList<Show> // 用可变列表存储数据
) : RecyclerView.Adapter<ShowAdapter.ShowViewHolder>() {

    inner class ShowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        // 绑定视图的逻辑,示例:设置节目名称、封面等
        fun bind(show: Show) {
            // itemView.findViewById<TextView>(R.id.show_name).text = show.name
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShowViewHolder {
        val view = LayoutInflater.from(context).inflate(itemLayout, parent, false)
        return ShowViewHolder(view)
    }

    override fun onBindViewHolder(holder: ShowViewHolder, position: Int) {
        holder.bind(showList[position])
    }

    override fun getItemCount(): Int = showList.size

    // 添加节目并通知适配器刷新新增项
    fun addShow(show: Show) {
        showList.add(show)
        notifyItemInserted(showList.size - 1) // 仅刷新新增项,提升性能
    }
}

3. AddShowActivity 修改(关键部分)

在用户提交表单后,把新节目数据返回给HomeFragment:

// 提交按钮的点击事件逻辑
submitButton.setOnClickListener {
    // 收集表单数据,创建Show对象
    val newShow = Show(
        name = editTextName.text.toString(),
        // 填充其他字段...
    )
    // 把数据放入Intent返回
    val resultIntent = Intent().apply {
        putExtra("NEW_SHOW", newShow) // 注意:Show类必须实现Parcelable接口
    }
    setResult(Activity.RESULT_OK, resultIntent)
    finish() // 关闭添加页面
}

注意事项

  • 你的Show实体类必须实现Parcelable接口,才能通过Intent传递(用Serializable也可,但Parcelable效率更高)
  • 不要在适配器中直接使用ValueEventListener,数据监听逻辑应放在Fragment或ViewModel中,适配器只负责UI渲染

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 05:25:21