0

我在我的项目中使用 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 来更新主列表。再次调用 Web 服务并更新列表的正确方法是什么?谁能帮我?

4

2 回答 2

0

在片段的onRefereshListener里面

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

在 Viewmodel 创建 getHomeScreenDetail 方法

public void getHomeScreenDetail(){
    data = homeRepository.getHomeScreen();
}
于 2019-09-10T13:55:59.217 回答
0

您可以调用getHomeScreen您的 Repository 类再次触发从服务器拉取数据,拉取请求完成后,将使用MutableLiveData.

但这是你的问题,MutableLiveData每次调用getHomeScreen. 因此,将不会通知第一个并且不会更新列表!

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

我建议你让你的HomeRepository类成为一个单例类并MutableLiveData在构造函数中初始化对象,然后一旦你从服务器获得新数据,你就可以使用这个对象向观察者发布数据。

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;
    }

}
于 2019-09-13T19:54:44.717 回答