视图模型在屏幕方向更改时进行网络调用 android

viewmodel making network call on screen orientation change android

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

实现了网络调用

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 class 我正在这样做:

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

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

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

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

}

我建议你使用无头片段设计模式。无头片段是保留其配置的片段,并且不会膨胀任何 xml。如果你旋转你的应用程序片段继续他的逻辑和配置并且当你必须执行异步任务或异步调用时非常有用(就像你在改造中)

定义您的片段:

public class YourFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true); // <--------- the fragment retain his configuration
    }

    public void yourLogic(){
        // do your logic
    }

}

在您的 MainActivity class 中创建片段或获取 istance 片段(如果它已经存在):

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     _yourHeadLessFragment= (YourFragment) getSupportFragmentManager().findFragmentByTag(HEADLESS_FRAGMENT);

     if (_yourHeadLessFragment== null) {
         _yourHeadLessFragment= new YourFragment();
         _yourHeadLessFragment.setListener(this); // if you want a callback
                getSupportFragmentManager().beginTransaction().add(_yourHeadLessFragment, HEADLESS_FRAGMENT).commit();

     }
     else{
         _yourHeadLessFragment.setListener(this); // refresh the callbacks if a rotation happened
     }
   }
}