我正在尝试保存和恢复RecyclerView的状态,更改了片段中的方向,它有点工作,但只有1个布局管理器。例如:如果只是LinearLayoutManager,它会保持位置,但一旦旋转并更改为StaggeredGrid,它会加载项目但会丢失滚动位置
if (Configuration.landscape(getActivity())) {
newslistview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
} else {
if(getResources().getBoolean(R.bool.is_tablet)) {
newslistview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
} else {
newslistview.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
这是一个在方向上更改布局的代码,它在onCreateView中,之后是实例检查
if (savedInstanceState != null) {
productsListSaved = new ArrayList<>();
productsListSaved = (ArrayList<HashMap<String,String>>) savedInstanceState.getSerializable("newsfeed");
adapter = new RecyclerViewAdapter(getActivity(), productsListSaved);
newslistview.setAdapter(adapter);
} else {
new Synchronize.LoadAllProducts(getActivity()).execute(categorie);
}
那么,是否可以同时保存和恢复&更改布局管理器?
先谢谢