调用 MutableLiveData 网络服务引擎更新列表

call MutableLiveData web service agine to update the list

我在我的项目中使用了MVVM结构。 正如您在代码中看到的那样,我有一个带有列表的主要片段观察到一个 Web 服务

片段:

mViewModel.getHomePageList().observe(this, homeDataWrapper -> {
           if (homeDataWrapper!=null) {
               if (homeDataWrapper.isStatus()) {
                   binding.homeProgressBar.setVisibility(View.INVISIBLE);
                   ToastUtil.showTosat(homeDataWrapper.getData().getMessage(), getContext());
                   Log.d(TAG, "onChanged: ");
               }
           }
       });

查看模型:

ublic class HomePageViewModel extends AndroidViewModel {
    private MutableLiveData<DataWrapper<Home>> data;
    public ObservableInt loading;
    private HomeRepository homeRepository;
    private HomePageAdapter adapter;

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

    public void init() {
        adapter = new HomePageAdapter(R.layout.main_page_list, this);
        homeRepository = new HomeRepository();
        if (this.data != null) {
            // ViewModel is created per Fragment so
            // we know the userId won't change
            return;
        }
        data = homeRepository.getHomeScreen();

    }


    public HomePageAdapter getAdapter() {
        return adapter;
    }

    public void onItemClick(Integer index) {

    }

    public void onSerachClicked(View view) {
        Navigation.findNavController(view).navigate(R.id.action_homePageFragment_to_searchActivity);
    }



    public MutableLiveData<DataWrapper<Home>> getHomePageList() {
        return this.data;
    }

}

主页存储库:

public MutableLiveData<DataWrapper<Home>> getHomeScreen() {
    final MutableLiveData<DataWrapper<Home>> homeMutableLiveData = new MutableLiveData<>();
    final DataWrapper<Home> dataWrapper = new DataWrapper<>();
    RetrofitInstance.getApiService().getHome().enqueue(new Callback<Home>() {
        @Override
        public void onResponse(@NotNull Call<Home> call, @NotNull Response<Home> response) {
            Log.d("", "onResponse: " + response);
            if (response.code() == 200) {
                dataWrapper.setData(response.body());
                dataWrapper.setStatus(true);
                homeMutableLiveData.postValue(dataWrapper);
            }

        }

        @Override
        public void onFailure(Call<Home> call, Throwable t) {
            Log.d("", "onResponse: " + t);
            dataWrapper.setApiException((Exception) t);
            dataWrapper.setStatus(false);
            homeMutableLiveData.postValue(dataWrapper);
        }
    });

    return homeMutableLiveData;
}

我想添加 SwipeRefreshLayout 来更新主列表。再次调用网络服务并更新列表的正确方法是什么? 谁能帮帮我?

片段的 onRefereshListener 内部

swifeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mViewModel.getHomeScreenDetail();
        }
    });

在 Viewmodel 中创建 getHomeScreenDetail 方法

public void getHomeScreenDetail(){
    data = homeRepository.getHomeScreen();
}

您只需从存储库 class 中调用 getHomeScreen 即可再次触发从服务器拉取数据,拉取请求完成后,将使用 MutableLiveData 通知观察者。

但这就是您的问题,您每次调用 getHomeScreen 时都会创建一个新的 MutableLiveData 对象。因此,第一个不会收到通知,列表也不会更新!

要解决这个问题,您必须在其他地方初始化您的 MutableLiveData,这样它就不会在您每次调用 getHomeScreen 时再次创建。

我建议你把你的 HomeRepository class 做成单例 class 并在构造函数中初始化 MutableLiveData 对象,然后你可以使用这个对象 post 从服务器获得新数据后,将数据发送给观察者。

public class HomeRepository {

    private static HomeRepository instance;
    private MutableLiveData<DataWrapper<Home>> homeMutableLiveData;

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

    private HomeRepository() {
        homeMutableLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<DataWrapper<Home>> getHomeScreen() {
        final DataWrapper<Home> dataWrapper = new DataWrapper<>();
        RetrofitInstance.getApiService().getHome().enqueue(new Callback<Home>() {
            @Override
            public void onResponse(@NotNull Call<Home> call, @NotNull Response<Home> response) {
                Log.d("", "onResponse: " + response);
                if (response.code() == 200) {
                    dataWrapper.setData(response.body());
                    dataWrapper.setStatus(true);
                   homeMutableLiveData.postValue(dataWrapper);
                }

            }

            @Override
            public void onFailure(Call<Home> call, Throwable t) {
                Log.d("", "onResponse: " + t);
                dataWrapper.setApiException((Exception) t);
                dataWrapper.setStatus(false);
                homeMutableLiveData.postValue(dataWrapper);
            }
        });

        return homeMutableLiveData;
    }

}