【问题标题】:Firebase ReyclerView adapter not displaying on activity startFirebase RecyclerView 适配器未在活动开始时显示
【发布时间】:2017-06-14 08:01:20
【问题描述】:

我有一个RecyclerView,它显示了一个消息列表。当活动开始时,Firebase 数据库不会填充 RecyclerView。但是,如果我单击后退按钮或EditTextRecyclerView 会正确显示所有项目。我尝试使用notifyDataSetChanged 方法手动更新它。我看过其他帖子,但没有看到任何人提供最终解决方案。

Similar Problem

这是onStart期间调用的代码。

    RecyclerView menu = (RecyclerView) findViewById(R.id.chat_view);

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setReverseLayout(false);

    menu.setAdapter(messageAdapter);
    menu.setLayoutManager(manager);

    messageAdapter.notifyDataSetChanged();`

【问题讨论】:

  • 必须先设置布局管理器,然后再将适配器设置为回收站视图。

标签: android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

您需要在 firebase 请求内部使用 messageAdapter.notifyDataSetChanged();,而不是在其外部使用,因为它是异步的,因此 firebase 在完成之前不会阻塞主线程

【讨论】:

    【解决方案2】:

    这是因为您在设置布局管理器之前设置了适配器。您应该将 2 行的顺序更改为:

        menu.setLayoutManager(manager);
        menu.setAdapter(messageAdapter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2020-07-02
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多