旋转设备时 Recyclerview 数据消失

Recyclerview data disappears when device is rotated

即使我使用的是 ViewModel,只要旋转设备,Recyclerview 中的数据就会消失。我必须将 makeSearch() 方法放在 onClick() 方法中,因为我需要获取按钮抓取的文本并将其用作搜索参数。有没有更好的方法可以避免这个问题?我的代码就在这里:

搜索活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

// What happens when the search button is clicked
materialButton.setOnClickListener(new View.OnClickListener() {
    @Override
     public void onClick(View v) {
        if (Objects.requireNonNull(textInputEditText.getText()).toString().isEmpty()) {
             textInputEditText.setError("Type a search query");
            } else {
                mSearchInput = Objects.requireNonNull(textInputEditText.getText()).toString();
                textInputEditText.setText("");
                makeSearch();
            }
        }
    });
}

// Gets the ViewModel, Observes the Question LiveData and delivers it to the Recyclerview
private void makeSearch() {
    final SearchAdapter searchAdapter = new SearchAdapter();
    SearchViewModel mSearchViewModel = new ViewModelProvider(this,
            new CustomSearchViewModelFactory(new SearchRepository())).get(SearchViewModel.class);
    mSearchViewModel.setQuery(mSearchInput);
    mSearchViewModel.getQuestionLiveData().observe(this, new Observer<List<Question>>() {
            @Override
            public void onChanged(List<Question> questions) {
                    mQuestions = questions;
                    searchAdapter.setQuestions(questions);
            }
        });
        mRecyclerView.setAdapter(searchAdapter);
        searchAdapter.setOnClickListener(mOnClickListener);
}

搜索视图模型:

public class SearchViewModel extends ViewModel {

private SearchRepository mSearchRepository;
private MutableLiveData<String> mSearchLiveData = new MutableLiveData<>();
private LiveData<List<Question>> mQuestionLiveData = Transformations.switchMap(mSearchLiveData, (query) -> {
    return mSearchRepository.getQuestions(query);
});

SearchViewModel(SearchRepository searchRepository) {
    this.mSearchRepository = searchRepository;
}

public LiveData<List<Question>> getQuestionLiveData() {
    return mQuestionLiveData;
}

public void setQuery(String query) {
    mSearchLiveData.setValue(query);
}
}

搜索存储库:

public class SearchRepository {

//private String inTitle;
private MutableLiveData<List<Question>> mQuestions = new MutableLiveData<>();

public SearchRepository() {
    //getQuestionsWithTextInTitle();
}

private void getQuestionsWithTextInTitle(String inTitle) {
    ApiService apiService = RestApiClient.getApiService(ApiService.class);
    Call<QuestionsResponse> call = apiService.getQuestionsWithTextInTitle(inTitle);
    call.enqueue(new Callback<QuestionsResponse>() {
        @Override
        public void onResponse(Call<QuestionsResponse> call, Response<QuestionsResponse> response) {
            QuestionsResponse questionsResponse = response.body();
            if (questionsResponse != null) {
                mQuestions.postValue(questionsResponse.getItems());
                //shouldShowData = true;
            } else {
                Log.d("SearchRepository", "No matching question");
                //shouldShowData = false;
            }
        }

        @Override
        public void onFailure(Call<QuestionsResponse> call, Throwable t) {
            //shouldShowData = false;
            t.printStackTrace();
        }
    });
}

public LiveData<List<Question>> getQuestions(String inTitle) {
    getQuestionsWithTextInTitle(inTitle);
    return mQuestions;
}
}

您通过 CustomSearchViewModelFactory 将搜索输入传递到 ViewModel 的构造函数和 SearchRepository 的构造函数的方法在任何情况下都行不通。当你第一次搜索你的 CustomSearchViewModelFactory 创建 ViewModel 时,你第二次点击搜索时,你的 SearchViewModel 已经创建并且你的工厂不会被第二次调用,这意味着你永远不会得到第二次查询。

相反,您应该提交 ViewModel Overview documentation,并使用 Transformations.switchMap() 将您的输入(搜索字符串)转换为该给定查询的新 LiveData<List<Question>>

这意味着您的 ViewModel 看起来像

public class SearchViewModel extends ViewModel {

    private SearchRepository mSearchRepository;
    private MutableLiveData<String> mSearchLiveData = new MutableLiveData<String>();
    private LiveData<List<Question>> mQuestionLiveData =
        Transformations.switchMap(mSearchLiveData, (query) -> {
            return mSearchRepository.getQuestions(query);
         });

    public SearchViewModel() {
        mSearchRepository = new SearchRepository();
    }

    public void setQuery(String query) {
        mSearchLiveData.setValue(query);
    }

    public LiveData<List<Question>> getQuestionLiveData() {
        return mQuestionLiveData;
    }
}

然后您将 Activity 更新为:

  1. 始终观察 getQuestionLiveData()(请注意,在您实际设置第一个查询之前,您不会收到对观察者的回调)
  2. makeSearch()
  3. SearchViewModel 上呼叫 setQuery()
  4. 完全删除您的 CustomSearchViewModelFactory(不再需要它)。