我在其上顯示一次一個的兩個片段,我切換它們與下面的方法:RecyclerView狀態恢復未示出的物品
public void toggle() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment instanceof OtherFragment && null != listFragment) {
// Other frag is visible - we should show the list now
// We reuse the fragment if it was already added to the manager
fragment = getSupportFragmentManager().findFragmentByTag(TAG_LIST);
if (fragment == null) {
fragment = listFragment;
}
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in,
R.anim.fade_out)
.replace(R.id.fragment_container, fragment, TAG_LIST)
.addToBackStack(TAG_LIST)
.commit();
} else if (fragment instanceof ListFragment && null != otherFragment) {
// List is visible - we show show the other fragment now
// We reuse the fragment if it was already added to the manager
fragment = getSupportFragmentManager().findFragmentByTag(TAG_OTHER);
if (fragment == null) {
fragment = otherFragment;
}
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in,
R.anim.fade_out)
.replace(R.id.fragment_container, fragment, TAG_OTHER)
.addToBackStack(TAG_OTHER)
.commit();
} else {
getSupportFragmentManager().popBackStack();
}
}
以我列表片段,在onCreateView()我確認如果recyclerView實例仍然有效(我有,一個全球性領域):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
ButterKnife.bind(this, view);
if (null != mRecyclerView) {
mRecyclerView.invalidate();
// Skip the initialization process
return;
} else {
// init recycler view
}
return view;
}
我遇到的問題是,當我從另一個片段切換到列表中的片段,該適配器的數據視圖不再創建。永遠不會調用onBindViewHolder
和onCreateViewHolder
。
我檢查了並且getItemCount()
返回有效值。 我也確保recyclerView
可見。 recyclerView
與他展示時似乎具有確切的狀態。
有沒有人有一個想法,爲什麼發生這種情況?有沒有人有這個解決方案?
的問題是不是片段的狀態,但的recyclerView和/或適配器。 正如我所提到的,片段的狀態已正確恢復,recyclerView保留了它的原始值。 另外,add方法不會改變任何東西(replace()方法的文檔:「替換已添加到容器的現有片段,這與爲所有當前添加的片段調用remove(Fragment)基本相同使用相同的containerViewId,然後添加(int,Fragment,String),這裏給出的參數相同。「) –
因此,回收站視圖中的數據沒有得到更新的問題? – Swapnil
是的,當我更改回列表片段時,行不再顯示。 'onBindViewHolder()'永遠不會被調用。 如果我還將recyclerView的初始化設置爲恢復狀態,則會顯示數據(將創建一個新的適配器),但會產生閃爍的效果,這是我不想避免的。 –