我正在使用两个主要视图的片段,该视图基于用于在线搜索数据的AsyncTask的结果显示或隐藏了setVisibility()。片段无法在循环中恢复正确的视图
例如,这里是视图之间切换的方法:
private void switchView()
{
Log.d(LOG_TAG, "switchView(): show = " + show);
mListView.setVisibility(show ? View.VISIBLE : View.GONE);
searchView.setVisibility(show ? View.GONE : View.VISIBLE);
mCompanyArrayAdapter.notifyDataSetChanged();
}
我采取这种方法,使的AsyncTask完成时,它可以将数据分析为包装类和创建的这些一个ArrayList在ListView适配器中使用的对象。 (如果还有另一种方法可以将自定义类传递给另一个片段,我会开放使用它。)
一旦从Async得到结果== true,它将隐藏searchView并显示mListView。但是在旋转时,屏幕将返回到searchView,而不是继续显示mListView结果。
我对日志输出困惑,显示什么,我相信从同一个片段两次调用onCreateView(DiscoverFragment),在这里看到:
10-17 10:29:21.872 6603-6603/nz.co.exium.panther D/DiscoverFragment﹕ onCreateView: savedinstancestate is null = false
10-17 10:29:21.877 6603-6603/nz.co.exium.panther D/AbsListView﹕ Get MotionRecognitionManager
10-17 10:29:21.882 6603-6603/nz.co.exium.panther D/DiscoverFragment﹕ onCreateView: savedinstancestate is null = true
所以第一个表明savedInstanceState被发现! = null并适当地设置布尔值,但随后会调用另一个onCreateView(),其中savedInstanceState为null并将其设置为false。为什么在旋转后双onCreateView()?
感谢您的帮助。
按照要求,代码在MainActivity替换片段(使用的FrameLayout的content_frame和位置== DrawerLayout位置项):
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, Fragment.instantiate(
getApplicationContext(), mClasses[position]))
.commit();
从我MainActivity.onCreate():
if (savedInstanceState == null) {
selectItem(0);
} else if (placeID == null || placeID.isEmpty()) {
selectItem(savedInstanceState.getInt(SELECTED_KEY));
} else {
selectItem(2);
}
如果null设置了默认Fragment,我已经执行了前一个savedInstanceState的检查。如果变量placeID(来自BroadcastReceiver)为空/空,则恢复之前的状态。否则,转到将显示通知数据的碎片。
您可以添加代码,将片段添加到活动中(我假设您以编程方式添加它,而不是通过XML中的“”)? –
ianhanniballake
2014-10-16 21:38:39
是的,使用DrawerLayout,方法selectItem(int position)使用在问题中添加的代码在content_frame中切换片段。 – fakataha 2014-10-16 21:43:05
那么在你的activity的'onCreate()'中显示一个片段会发生什么? – ianhanniballake 2014-10-16 21:45:22