RecyclerView doesn't load items on first start of activity

后端 未结 4 512
醉酒成梦
醉酒成梦 2020-12-11 11:59

Everytime we open the activity that bares the recycler adapter it fails to load on the first try. Exiting the activity and re-entering fixes the problem. Here is a gif for e

相关标签:
4条回答
  • 2020-12-11 12:23

    Replace this:

    recyclerView = (RecyclerView)findViewById(R.id.active_chats);
    ActiveChatConvo adapter = new ActiveChatConvo(users,this);
    recyclerView.setAdapter(adapter);
    mLinearLayoutManager = new LinearLayoutManager(this);
    //mLinearLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(mLinearLayoutManager);
    

    with this:

    recyclerView = (RecyclerView)findViewById(R.id.active_chats);
    ActiveChatConvo adapter = new ActiveChatConvo(users,this);
    mLinearLayoutManager = new LinearLayoutManager(this);
    //mLinearLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(mLinearLayoutManager);
    recyclerView.setAdapter(adapter);
    

    You need to set adapter after you set LayoutManager.

    0 讨论(0)
  • 2020-12-11 12:34

    In my case I managed to solve it I found a way to start FirebaseDatabase before opening the activity and added the cod

    adapter.notifyDataSetChanged();

    and then I started the firebase before opening it with this

    FirebaseDatabase.getInstance().getReference().keepSynced(true);

    0 讨论(0)
  • 2020-12-11 12:42

    Firebase is synchronize so it doesn't block the main thread of your application, that mean that your application continue executing, you need to notify the adapter after firebase finishing his job by using adapter.notifiyDatasetChanged() after the for loop

    0 讨论(0)
  • 2020-12-11 12:44

    You can use Boolean button to check if it is true adapter is made

    0 讨论(0)
提交回复
热议问题