设备旋转时,Recyclerview数据会消失。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设备旋转时,Recyclerview数据会消失。相关的知识,希望对你有一定的参考价值。
即使我使用ViewModel,每当设备旋转时,Recyclerview中的数据就会消失。我不得不把makeSearch()方法放在onClick()方法里面,因为我需要得到按钮抓取的文本,并把它作为搜索参数。我有没有更好的处理方法来避免这个问题?我的代码就在这里。
SearchActivity: SearchViewModel:
@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);
}
SearchViewModel:
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);
}
}
SearchRepository:
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
已经创建了,而你的工厂没有被第二次调用,这意味着你永远不会得到第二个查询。
相反,你应该将 视图模型概述文档并使用 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;
}
}
你就会把你的活动更新为。
- 始终遵守
getQuestionLiveData()
(注意,在你真正设置第一个查询之前,你不会得到一个回调到你的Observer) - 呼叫
setQuery()
在你SearchViewModel
在你makeSearch()
- 移除您的
CustomSearchViewModelFactory
(不再需要)。
以上是关于设备旋转时,Recyclerview数据会消失。的主要内容,如果未能解决你的问题,请参考以下文章
iOS 8 - 切换控件后,即使在纵向模式下,旋转也会使状态栏消失