Firebase+RecyclerView: RecyclerView not displaying on application start

谁说胖子不能爱 提交于 2019-12-04 12:37:06

In my case this was caused by mRecyclerView.setHasFixedSize(true); If you comment out this line of code the list loads properly. I got my solution from this discussion: https://github.com/firebase/FirebaseUI-Android/issues/204

Let me try, as you say on the question title,

RecyclerView not displaying on application start

so, the

Initializes Recycler View and Layout Manager.

should be declared on the onStart

@Override
   public void onStart() {
       super.onStart();
       mFirebaseRef = new Firebase("<your Firebase link here>");
       firebaseRecyclerAdapter = ...
       //and so on

Hope it helps!

 firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int friendlyMessageCount = firebaseRecyclerAdapter.getItemCount();
            int lastVisiblePosition =
                    linearLayoutManager.findLastCompletelyVisibleItemPosition();
            // If the recycler view is initially being loaded or the
            // user is at the bottom of the list, scroll to the bottom
            // of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (friendlyMessageCount - 1) &&
                            lastVisiblePosition == (positionStart - 1))) {
                linearLayoutManager.scrollToPosition(positionStart);
            }
        }
    });
    recyclerListIdeas.setAdapter(firebaseRecyclerAdapter);

** Just add Recyclerview.AdapterDataObserver() . worked for me ! hope it helps :)**

i had the same issue, check the documentation:

https://codelabs.developers.google.com/codelabs/firebase-android/#6

fixed it by adding a data observer:

mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
   @Override
   public void onItemRangeInserted(int positionStart, int itemCount) {
   super.onItemRangeInserted(positionStart, itemCount);
   int friendlyMessageCount = mFirebaseAdapter.getItemCount();
   int lastVisiblePosition =
          mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
   // If the recycler view is initially being loaded or the 
   // user is at the bottom of the list, scroll to the bottom 
   // of the list to show the newly added message.
   if (lastVisiblePosition == -1 ||
           (positionStart >= (friendlyMessageCount - 1) &&
                   lastVisiblePosition == (positionStart - 1))) {
       mMessageRecyclerView.scrollToPosition(positionStart);
      }
   }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!