Reputation: 1723
Hiy guys,
I have a fragment with a RecyclerView
in it. That RecyclerView
is populated by a Firebase 'DatabaseReference' object and on that reference I have added a ValueListener as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
Log.d(TAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_home, container, false);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Do something
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
ItemRecyclerViewAdapter adapter = new ItemRecyclerViewAdapter(activity, itemsDatabaseReference);
adapter.setHasStableIds(true);
// use a linear layout manager
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(),
DividerItemDecoration.VERTICAL_LIST);
recyclerView.addItemDecoration(itemDecoration);
recyclerView.setAdapter(adapter);
return view;
}
ItemRecyclerViewAdapter
extends FirebaseRecyclerAdapter
. The problem is that onDataChange
happens after onResume
so that at times the recycler is empty. What I can't understand is the fact that the recycler view is not always empty. Sometimes data are shown and sometimes not.
Thanks
Upvotes: 2
Views: 1382
Reputation: 2165
I think if you are using FirebaseRecyclerAdapter
(or any class extended from that) as your RecyclerView
adapter, you should set recyclerView.setHasFixedSize(false)
. So the recyclerView
knew that data is constantly updated.
Upvotes: 1
Reputation: 3283
Actually onResume(); is strictly bound with activity onResume();
If you are using viewpager then there is a method in fragment
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
//do your logic . updating recycler view
}
if you are infalting in framelayout
then you can update in onViewCreated();
Upvotes: 1