Recyclerview 不使用 MVVM 在片段中显示数据

Recyclerview not displaying data In Fragment using MVVM

我正在从 firebase 加载数据,我想使用 MVVM 在 recyclerview 中显示它 我从 firebase 检索了数据,它工作正常。 但是我想使用 adapter.notifyDataSetChanged(); 来更新 Repo class 中的 recyclerview 这是我的仓库 class:

public class CategoriesRepo {
    private static CategoriesRepo instance;
    private final ArrayList<Cat> categoriesModel = new ArrayList<>();
    private DatabaseReference dbCategories;

    public static CategoriesRepo getInstance() {
        if (instance == null) {
            instance = new CategoriesRepo();
        }
        return instance;
    }

    public MutableLiveData<ArrayList<Cat>> getCategories() {
        loadCats();
        MutableLiveData<ArrayList<Cat>> categories = new MutableLiveData<>();
        categories.setValue(categoriesModel);
        return categories;
    }

    private void loadCats() {
        dbCategories = FirebaseDatabase.getInstance().getReference("categories");
        dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        String name = ds.getKey();
                        // this is not showing in recyclerview 
                        categoriesModel.add(new Cat("Name", 1));
                        Log.d("TAGD", "onDataChange: " + ds.getKey() + " " + categoriesModel.size());
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });
    }
}

有什么方法可以使用 MVVM 更新 recyclerview 吗?

LiveData 将在值更改时提供回调,即 setValuepostValue 。所以你需要在获取数据之后设置值,而不是之前。

public class CategoriesRepo {
    private static CategoriesRepo instance;
    private DatabaseReference dbCategories;
    private MutableLiveData<ArrayList<Cat>> categories = new MutableLiveData<>();


    public static CategoriesRepo getInstance() {
        if (instance == null) {
            instance = new CategoriesRepo();
        }
        return instance;
    }

    public MutableLiveData<ArrayList<Cat>> getCategories() {
        loadCats();
        return categories;
    }

    private void loadCats() {
        dbCategories = FirebaseDatabase.getInstance().getReference("categories");
        dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    ArrayList<Cat> categoriesModel = new ArrayList<>()
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        String name = ds.getKey();
                        categoriesModel.add(new Cat("Name", 1));
                    }
                    categories.setValue(categoriesModel);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });
    }
}

这应该可行。您还必须通过数据加载处理错误状态。 通过处理所有状态。