发布或设置 MutableLiveData - 未调用观察者 onChanged

gil*_*emy 4 android android-fragments rx-android android-livedata android-viewmodel

我发现了新的 android 架构组件,我想通过一个小的测试应用程序来测试这对 ViewModel / LiveData。后者有两个片段(在 a 中ViewPager),第一个创建/更新卡片列表(通过 an EditText),第二个显示所有卡片。

我的视图模型:


public class CardsScanListViewModel extends AndroidViewModel {

    private MutableLiveData> cardsLiveData = new MutableLiveData();
    private HashMap cardsMap = new HashMap();

    public CardsScanListViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData> getCardsLiveData() {
        return this.cardsLiveData;
    }

    public void saveOrUpdateCard(String id) {
        if(!cardsMap.containsKey(id)) {
            cardsMap.put(id, new Card(id, new AtomicInteger(0)));
        }
        cardsMap.get(id).getCount().incrementAndGet();
        this.cardsLiveData.postValue(cardsMap);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的第二个片段:

public class CardsListFragment extends Fragment {

    CardsAdapter cardsAdapter;

    RecyclerView recyclerCardsList;

    public CardsListFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final CardsScanListViewModel viewModel =
                ViewModelProviders.of(this).get(CardsScanListViewModel.class);

        observeViewModel(viewModel);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_cards_list, container, false);
        recyclerCardsList = v.findViewById(R.id.recyclerCardsList);
        recyclerCardsList.setLayoutManager(new LinearLayoutManager(getActivity()));
        cardsAdapter = new CardsAdapter(getActivity());
        recyclerCardsList.setAdapter(cardsAdapter);

        return v;
    }

    private void observeViewModel(CardsScanListViewModel viewModel) {
        viewModel.getCardsLiveData().observe(this, new Observer  > () {
            @Override
            public void onChanged(@Nullable HashMap  cards) {
                if (cards != null) {
                    cardsAdapter.setCardsList(cards.values());
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

HashMap,像我MutableLiveData,更新好,但我的第二个片段不接受通过观察者的信息。

adi*_*e49 10

您正在观察新实例ViewModel,而不是观察同一ViewModel您使用的第一片段。

final CardsScanListViewModel viewModel =
            ViewModelProviders.of(this).get(CardsScanListViewModel.class);
Run Code Online (Sandbox Code Playgroud)

上面的代码CardsScanListViewModel为您的第二个片段初始化新实例CardsListFragment,因为您this作为上下文传递。如果您更新此片段中的任何数据,它将在此实例更新ViewModel

它在您的第一个 Fragment 中工作,因为它更新数据并观察来自同一实例的数据 ViewModel

为了使数据在 ViewModel 之间保持通用,通过在两个片段中传递活动上下文而不是片段上下文来启动视图模型。

final CardsScanListViewModel viewModel =
            ViewModelProviders.of(getActivity()).get(CardsScanListViewModel.class);
Run Code Online (Sandbox Code Playgroud)

这将创建单个实例CardsScanListViewModel和将数据片段之间,因为它们正在观察被共享LiveData单个实例ViewModel

为了确认,如果您还没有在适配器本身中这样做,则需要在更新列表后添加 notifyDataSetChanged()

private void observeViewModel(CardsScanListViewModel viewModel) {
    viewModel.getCardsLiveData().observe(this, new Observer  > () {
        @Override
        public void onChanged(@Nullable HashMap  cards) {
            if (cards != null) {
                cardsAdapter.setCardsList(cards.values());
                cardsAdapter.notifyDataSetChanged();
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)