恢复回收站视图状态
Restore recyclerview state
我找到了一些代码可以在屏幕旋转后保存和恢复 RecyclerView
的状态,但它不起作用。
代码:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(Constants.LIST_SAVED_POSITION, recyclerView.getLayoutManager().onSaveInstanceState());
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState!=null && savedInstanceState.containsKey(Constants.LIST_SAVED_POSITION)){
Parcelable parcelable;
parcelable = savedInstanceState.getParcelable(Constants.LIST_SAVED_POSITION);
recyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
}
}
屏幕旋转后旧位置没有恢复。我知道我可以保存整数变量,但这种方式似乎更好。
谁能帮我用这种方法解决这个问题。
我最近在使用回收站视图并希望拥有与您想要的相同的功能,我已经按照下面提到的教程进行操作,即使在旋转屏幕后它也会保持 view.Please 的状态,请参阅正在关注 link。
http://www.androidtutorialpoint.com/material-design/listing-items-using-recyclerview/
希望对您有所帮助!!
你可以添加这个
到 manifist.xml 中的 activity 以禁用 activity 重新创建自身,因此在屏幕旋转后 activity 保存所有变量。
android:configChanges="orientation|keyboardHidden"
>
我发现了问题。正在片段上调用此代码。但问题在于在 activity 中重新创建片段。当我修复重新创建片段时,列表即使不保存状态也会恢复位置。
旧代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supplication_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
chapter = getIntent().getIntExtra(Constants.SELECTED_ITEM, 1);
fragmentSupplicationDetail = new FragmentSupplicationDetail();
Bundle extras = new Bundle();
extras.putInt(Constants.SELECTED_ITEM, chapter);
fragmentSupplicationDetail.setArguments(extras);
transaction.replace(R.id.container, fragmentSupplicationDetail, SUPPLICATION_FRAGMENT_TAG).commit();
}
修正:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supplication_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
chapter = getIntent().getIntExtra(Constants.SELECTED_ITEM, 1);
if (getSupportFragmentManager().findFragmentByTag(SUPPLICATION_FRAGMENT_TAG) == null){
fragmentSupplicationDetail = new FragmentSupplicationDetail();
Bundle extras = new Bundle();
extras.putInt(Constants.SELECTED_ITEM, chapter);
fragmentSupplicationDetail.setArguments(extras);
Log.d(Constants.LOG_TAG, getClass().getSimpleName() + ": Создаем фрагмент ");
}else {
fragmentSupplicationDetail = (FragmentSupplicationDetail) getSupportFragmentManager().findFragmentByTag(SUPPLICATION_FRAGMENT_TAG);
Log.d(Constants.LOG_TAG, getClass().getSimpleName() + ": Восстанавливаем фрагмент ");
}
transaction.replace(R.id.container, fragmentSupplicationDetail, SUPPLICATION_FRAGMENT_TAG).commit();
}
我找到了一些代码可以在屏幕旋转后保存和恢复 RecyclerView
的状态,但它不起作用。
代码:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(Constants.LIST_SAVED_POSITION, recyclerView.getLayoutManager().onSaveInstanceState());
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState!=null && savedInstanceState.containsKey(Constants.LIST_SAVED_POSITION)){
Parcelable parcelable;
parcelable = savedInstanceState.getParcelable(Constants.LIST_SAVED_POSITION);
recyclerView.getLayoutManager().onRestoreInstanceState(parcelable);
}
}
屏幕旋转后旧位置没有恢复。我知道我可以保存整数变量,但这种方式似乎更好。
谁能帮我用这种方法解决这个问题。
我最近在使用回收站视图并希望拥有与您想要的相同的功能,我已经按照下面提到的教程进行操作,即使在旋转屏幕后它也会保持 view.Please 的状态,请参阅正在关注 link。
http://www.androidtutorialpoint.com/material-design/listing-items-using-recyclerview/
希望对您有所帮助!!
你可以添加这个 到 manifist.xml 中的 activity 以禁用 activity 重新创建自身,因此在屏幕旋转后 activity 保存所有变量。
android:configChanges="orientation|keyboardHidden"
>
我发现了问题。正在片段上调用此代码。但问题在于在 activity 中重新创建片段。当我修复重新创建片段时,列表即使不保存状态也会恢复位置。
旧代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supplication_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
chapter = getIntent().getIntExtra(Constants.SELECTED_ITEM, 1);
fragmentSupplicationDetail = new FragmentSupplicationDetail();
Bundle extras = new Bundle();
extras.putInt(Constants.SELECTED_ITEM, chapter);
fragmentSupplicationDetail.setArguments(extras);
transaction.replace(R.id.container, fragmentSupplicationDetail, SUPPLICATION_FRAGMENT_TAG).commit();
}
修正:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supplication_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
chapter = getIntent().getIntExtra(Constants.SELECTED_ITEM, 1);
if (getSupportFragmentManager().findFragmentByTag(SUPPLICATION_FRAGMENT_TAG) == null){
fragmentSupplicationDetail = new FragmentSupplicationDetail();
Bundle extras = new Bundle();
extras.putInt(Constants.SELECTED_ITEM, chapter);
fragmentSupplicationDetail.setArguments(extras);
Log.d(Constants.LOG_TAG, getClass().getSimpleName() + ": Создаем фрагмент ");
}else {
fragmentSupplicationDetail = (FragmentSupplicationDetail) getSupportFragmentManager().findFragmentByTag(SUPPLICATION_FRAGMENT_TAG);
Log.d(Constants.LOG_TAG, getClass().getSimpleName() + ": Восстанавливаем фрагмент ");
}
transaction.replace(R.id.container, fragmentSupplicationDetail, SUPPLICATION_FRAGMENT_TAG).commit();
}