我正在使用 FirebaseAndroid-UI 库附带的 FirebaseRecyclerAdapter,我假设当 Firebase 上的数据发生变化时,它会自动刷新和更新 RecyclerView。
我的代码很简单:
Firebase firebasePicturesRef = new Firebase(FIREBASE_URL);
mAdapter = new FirebaseRecyclerAdapter<Image, ImageViewHolder>(
Image.class, R.layout.pictures_grid_item,
ImageViewHolder.class, firebasePicturesRef) {
@Override
protected void populateViewHolder(ImageViewHolder viewHolder, Image model, int position) {
super.populateViewHolder(viewHolder, model, position);
Glide.with(getActivity()).load(model.getThumbnailUrl()).into(viewHolder.mImageView);
}
};
我还尝试在onDataChange方法中使用notifyDatasetChanged手动刷新适配器:
firebasePicturesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mAdapter != null)
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
难道我做错了什么?解决方法是什么?