LiveData不更新UI第一次创建视图时,
仅当我在片段之间切换时显示。
这是我的HomeFragment。 在这里,我初始化了ViewModel并观察LiveDate值。
viewModel = new ViewModelProvider(requireActivity()).get(HomeFragmentViewModel.class);
viewModel.init();
categoryAdapter = new CategoryAdapter(getContext(),viewModel.getCategories().getValue());
newRecipeAdapter = new HomeRecipeAdapter(getContext(),viewModel.getNewRecipes().getValue());
viewModel.getCategories().observe(getViewLifecycleOwner(), new Observer<ArrayList<CategoryList>>() {
@Override
public void onChanged(ArrayList<CategoryList> categoryLists) {
categoryAdapter.notifyDataSetChanged();
}
});
viewModel.getNewRecipes().observe(getViewLifecycleOwner(), new Observer<ArrayList<NewRecipes>>() {
@Override
public void onChanged(ArrayList<NewRecipes> newRecipes) {
newRecipeAdapter.notifyDataSetChanged();
}
});
这是我的ViewModel类
public class HomeFragmentViewModel extends ViewModel {
private MutableLiveData<ArrayList<CategoryList>> categoryLists;
private MutableLiveData<ArrayList<NewRecipes>> newRecipes;
public void init(){
if (categoryLists!=null&&newRecipes!=null){
return;
}
categoryLists = HomeRepo.getInstance().getCategories();
newRecipes = HomeRepo.getInstance().getNewRecipes();
}
public LiveData<ArrayList<CategoryList>> getCategories() {
return categoryLists;
}
public LiveData<ArrayList<NewRecipes>> getNewRecipes() {
return newRecipes;
}
}
这是我从Firebase检索数据的仓库。
public class HomeRepo {
private static HomeRepo instance;
private ArrayList<CategoryList> categoryLists = new ArrayList<>();
private MutableLiveData<ArrayList<CategoryList>> category = new MutableLiveData<>();
private ArrayList<NewRecipes> newRecipesLists = new ArrayList<>();
private MutableLiveData<ArrayList<NewRecipes>> newRecipe = new MutableLiveData<>();
public static HomeRepo getInstance(){
if (instance==null){
instance = new HomeRepo();
}
return instance;
}
public MutableLiveData<ArrayList<CategoryList>> getCategories(){
if (categoryLists.size()==0){
loadCategories();
}
category.setValue(categoryLists);
return category;
}
public MutableLiveData<ArrayList<NewRecipes>> getNewRecipes(){
if (newRecipesLists.size()==0){
loadNewRecipes();
}
newRecipe.setValue(newRecipesLists);
return newRecipe;
}
private void loadNewRecipes() {
DatabaseReference newRecipeRef = FirebaseDatabase.getInstance().getReference().child("lt");
newRecipeRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
newRecipesLists = new ArrayList<>();
for(DataSnapshot items : dataSnapshot.getChildren())
{
newRecipesLists.add(items.getValue(NewRecipes.class));
}
newRecipe.postValue(newRecipesLists);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void loadCategories() {
DatabaseReference categoryRef = FirebaseDatabase.getInstance().getReference().child("cl");
categoryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
categoryLists = new ArrayList<>();
for(DataSnapshot items : dataSnapshot.getChildren())
{
categoryLists.add(items.getValue(CategoryList.class));
}
category.postValue(categoryLists);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
如果我在视图模型中初始化MutableLiveData并在onCreateView中获取列表的大小,则会生成NullPointerException作为尝试在空对象引用上调用虚拟方法的