I'm having two fragments which are displayed one at a time and I toggle them with the following method:
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();
}
}
In my list fragment, in the onCreateView() I verify if the recyclerView instance is still valid (I have an global field for that):
@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;
}
The issue I have encountered is that when I change from the other fragment to the list fragment, the views for the adapter data are not created anymore. onBindViewHolder
and onCreateViewHolder
are never called.
I have checked and getItemCount()
returns an valid value.
I have also made sure the recyclerView
is visible.
The recyclerView
seems to have the exact state as when he was displayed.
Does anyone have an idea why this is happening? Does anyone have an solution for this?
答案 0 :(得分:0)
我建议使用.add
而不是.replace
添加第二个片段,并使用.remove
删除第二个片段,以防您想要切换回来。
这将有助于保留片段1的状态,并将为片段2重新创建视图。
编辑#1
尝试将数据设置放在适配器中,并在ifCreateView()的if语句中延迟1秒后调用notifyDataSetChanged()
。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
\\ set adapter and call notifyDataSetChanged() here.
}
}, 1000);
答案 1 :(得分:0)
经过一些密集调试后,我设法解决了这个问题。
似乎RecyclerView
有一些恢复选项,但只考虑它的布局管理器。
我已经修复了这个问题,通过使用RecyclerView
的全局变量和onCreateView()
我重用了它上面设置的适配器(如果有的话):
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
ButterKnife.bind(this, rootView);
// Try to reuse the old adapter, if any set on the recyclerView
MyAdapter mAdapter = null != mRecyclerView ? (MyAdapter) mRecyclerView.getAdapter() : null;
mRecyclerView = findById(rootView, android.R.id.list);
// Setup the recycler view everytime
...
// If no adapter to reuse, just create a new one
if (null == mAdapter) {
// create a new adapter again
...
}
// Bind the adapter to the RecyclerView
mRecyclerView.setAdapter(mAdapter);