解决RecycleView非用户本意关闭

tech2022-12-19  110

思路

* 当RecycleView所在的进程非正常关闭时 * 使用onSaveInstanceState保存当前RecycleView第一个可见条目和距离屏幕的高度 * 当界面在此打开得到上次保存的状态即可 /* 解决RecycleView非用户本意关闭 */ @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // binging.newsRecyclerView. if (savedInstanceState != null) { // 获取recycle位置和高度坐标 targetPosition = savedInstanceState.getInt("position"); targetOffset = savedInstanceState.getInt("offset"); } } @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); LinearLayoutManager layoutManager = ((LinearLayoutManager) binding.pageRv.getLayoutManager()); int fistVisiblePosition = layoutManager.findFirstVisibleItemPosition(); View itemView = layoutManager.findViewByPosition(fistVisiblePosition); int y = (int) itemView.getY(); // 当Fragment页面被迫关闭保存RecycleView位置和高度坐标 outState.putInt("position", fistVisiblePosition); outState.putInt("offset", y); }
最新回复(0)