我有两个片段的宿主活动;片段A具有项目的回收视图,片段B用于显示详细信息..
当用户点击某个项目时,片段A会替换为片段B;
我想在点击项目时保存recyclerview的位置..
我尝试了以下内容..
如果片段A ..
Parcelable recyclerViewState;
在onPause中我为变量分配了一个值,如下所示:
@Override
public void onPause() {
super.onPause();
recyclerViewState = imagesRecyclerView.getLayoutManager().onSaveInstanceState();
}
在onResume中我按如下方式检索值:
@Override
public void onResume() {
super.onResume();
if (recyclerViewState != null) {
imagesRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
}
}
然而它还没有用呢?
答案 0 :(得分:0)
如果您只是将片段设置为隐藏或可见,那么如何使用它?
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
// Do something with boolean hidden
}
或者,使用backstackChangedListener。 https://stackoverflow.com/a/40792162/850347
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if (getFragmentManager() != null) {
Fragment topFrag = NavigationHelper.getCurrentTopFragment(getFragmentManager());
if (topFrag != null) {
if (topFrag instanceof YourFragment) {
//This fragment is being shown.
} else {
//Navigating away from this fragment.
}
}
}
}
});