我是 android 架构组件的新手,我对 viewmodel 有点困惑。我正在构建一个应用程序,它从服务器获取项目列表并在布局中显示为列表。我已经在 Repository 类中实现了网络调用。

存储库.java:

//Get list of top rated movies
    public LiveData<NetworkResponse> getTopRatedMovies() {
        final MutableLiveData<NetworkResponse> result = new MutableLiveData<>();
        ApiService api = retrofit.create(ApiService.class);
        Call<MovieData> call = api.getTopRateMovies("api_key");
        call.enqueue(new Callback<MovieData>() {
            @Override
            public void onResponse(Call<MovieData> call, Response<MovieData> response) {
                result.postValue(new NetworkResponse(response.body()));
            }

            @Override
            public void onFailure(Call<MovieData> call, Throwable t) {
                Log.e(TAG, t.getLocalizedMessage());
                result.postValue(new NetworkResponse(t));
            }
        });
        return result;
    }

现在在 ViewModel 类中,我正在这样做:
public class MovieListViewModel extends ViewModel {


    public LiveData<NetworkResponse> result, topRatedMovies;
    public LiveData<List<MovieEntity>> favoriteMovies;

    private Repository repository;

    public MovieListViewModel() {
        repository = new Repository(MyApplication.getInstance());
    }

    public void getTopRatedMovieList() {
        topRatedMovies = repository.getTopRatedMovies();
    }

}

现在在 MainActivity.java 中:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ((MyApplication) getApplication()).getComponent().inject(this);
        movieListViewModel = ViewModelProviders.of(this).get(MovieListViewModel.class);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new MovieListAdapter(this);
        movieListViewModel.getTopRatedMovieList();
        observeTopRatedMovies();

    }
private void observeTopRatedMovies() {
        movieListViewModel.topRatedMovies.observe(this, new Observer<NetworkResponse>() {
            @Override
            public void onChanged(@Nullable NetworkResponse networkResponse) {
                if (networkResponse.getPostData() != null) {
                    Log.e(TAG, "Successful");
                    topRatedData = networkResponse.getPostData();
                    adapter.addData(networkResponse.getPostData().getResults());
                    recyclerView.setAdapter(adapter);
                } else {
                    Log.e(TAG, "failure");
                }
            }
        });
    }

现在一切正常,我可以看到列表。但是如果我旋转手机, View 模型会再次进行网络调用。如何在屏幕方向更改时避免再次网络调用?

最佳答案

您只能初始化实时数据一次。这应该足够了:

public class MovieListViewModel extends ViewModel {


    public LiveData<NetworkResponse> result, topRatedMovies;
    public LiveData<List<MovieEntity>> favoriteMovies;

    private Repository repository;

    public MovieListViewModel() {
        repository = new Repository(MyApplication.getInstance());
        topRatedMovies = repository.getTopRatedMovies();
    }

}

10-07 20:08