旋转后以及关闭应用程序并再次打开后,无法恢复RecyclerView的滚动位置。在第一种情况下,我可以在清单中使用android:configChanges
,但是重新打开应用程序后没有帮助。
这是我的代码。尝试恢复状态后,我看到一个没有我的列表的空白屏幕。
public class MainActivity extends AppCompatActivity implements IMainContract.View {
private final String BUNDLE_RECYCLER_LAYOUT = MainActivity.class.getSimpleName() + ".save.state.list";
private IMainContract.Presenter mainPresenter;
private MainAdapter adapter;
private LinearLayoutManager layoutManager;
private ImageView ivRefresh;
private ProgressBar progressBar;
private RecyclerView rvUsers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivRefresh = (ImageView) findViewById(R.id.ivRefresh);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
rvUsers = (RecyclerView) findViewById(R.id.rvUsers);
layoutManager = new LinearLayoutManager(this);
rvUsers.setLayoutManager(layoutManager);
mainPresenter = new MainPresenter(Repository.getInstance(this), this);
adapter = new MainAdapter(new ArrayList<>(), this);
rvUsers.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ivRefresh.setOnClickListener(v -> mainPresenter.onRefreshPressed());
if (savedInstanceState != null) {
Parcelable savedRecyclerLayoutState = savedInstanceState.getParcelable(BUNDLE_RECYCLER_LAYOUT);
layoutManager.onRestoreInstanceState(savedRecyclerLayoutState);
} else {
mainPresenter.subscribe();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(BUNDLE_RECYCLER_LAYOUT, layoutManager.onSaveInstanceState());
}
//...
}
最佳答案
您需要在stackoverflow上遵循此answer。更改代码
public class MainActivity extends AppCompatActivity {
private final String KEY_RECYCLER_STATE = "recycler_state";
private MainAdapter adapter;
private LinearLayoutManager layoutManager;
private ImageView ivRefresh;
private ProgressBar progressBar;
private RecyclerView rvUsers;
private static Bundle mBundleRecyclerViewState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivRefresh = (ImageView) findViewById(R.id.ivRefresh);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
rvUsers = (RecyclerView) findViewById(R.id.rvUsers);
layoutManager = new LinearLayoutManager(this);
rvUsers.setLayoutManager(layoutManager);
adapter = new MainAdapter(new ArrayList<>(), this);
rvUsers.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
protected void onPause()
{
super.onPause();
// save RecyclerView state
mBundleRecyclerViewState = new Bundle();
Parcelable listState = rvUsers.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState);
}
@Override
protected void onResume()
{
super.onResume();
// restore RecyclerView state
if (mBundleRecyclerViewState != null) {
Parcelable listState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
rvUsers.getLayoutManager().onRestoreInstanceState(listState);
}
}
}
关于android - 旋转后无法还原滚动位置(RecyclerView),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43704113/