Reputation: 45
So I am using firebase to retrieve some products. I have logged and the data does come through each time I click. But apparently the recyclerview only shows the data once.
Here is the link to a video showing the issue. video link
Setting up RecyclerView
private void setupRecyclerView() {
Log.d(TAG, "setupRecyclerView: setting up recyclerview");
productList = new ArrayList<>();
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
this.adapter = new ProductListAdapter(productList);
rv.setAdapter(adapter);
}
Getting products from Firebase
private void getProducts() {
//Setting up Event Listener
productsListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Product p = dataSnapshot.getValue(Product.class);
Log.d(TAG, "onChildAdded: Adding Product = "+p.toString());
//Pass Products to Proucts fragment
addProduct(p);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d(TAG, "onChildChanged: child Product changed, snapshot="+dataSnapshot.toString());
editProduct(dataSnapshot.getValue(Product.class));
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
//Get Products from firebase
FirebaseDatabase.getInstance().getReference().child("user_photos").child(mFirebaseStoreID).addChildEventListener(productsListener);
}
Add Product Method
public void addProduct(Product newProduct) {
double price=0;
if (!newProduct.getAssociated_product_price().equals("")){
price = Double.valueOf(newProduct.getAssociated_product_price());
}
productList.add(new ProductCard(newProduct.getAssociated_product_name(), newProduct.getAssociated_product_description(), newProduct.getImage_path(), price,newProduct.getPhoto_id()));
adapter.notifyItemInserted(productList.size() - 1);
Log.d(TAG, "addProduct: Product added total size= " + productList.size() + " and adapter size= " + adapter.getItemCount());
}
There is a lot of code, let me know if this isn't enough. The behavior is very strange though.
Upvotes: 4
Views: 285
Reputation: 3713
In setupRecyclerView
method remove the line rv.setHasFixedSize(false)
.
The doc for setHasFixedSize
says:
RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).
If your use of RecyclerView falls into this category, set this to true. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.
Since your content from adapter is changing when you are notifying it for item inserted so you should not set it to true
.
Upvotes: 1