viewmodel在屏幕方向上进行网络调用更改android

时间:2018-08-21 06:51:51

标签: android android-architecture-components android-livedata android-jetpack android-viewmodel

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

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

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

2 个答案:

答案 0 :(得分:2)

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

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

}

答案 1 :(得分:1)

我建议您使用无头碎片设计模式。无头片段是保留其配置的片段,并且不会膨胀任何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类中创建片段或获取等距片段(如果已存在):

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