【发布时间】:2015-10-20 13:12:45
【问题描述】:
我正在尝试在导航抽屉模式旁边合并一个主-详细信息流。我已按照this answer 中的说明进行操作。这适用于显示一个窗格,但对于这些步骤,她的指令状态是向 MainActivity 添加相关代码,在我的例子中是 NavigationDrawerActivity:
(5B) 复制ItemListActivity中onCreate的部分代码并粘贴 在 MainActivity 中 onCreate。这部分:
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ItemListFragment) getFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
(5C) 同时从 ItemListActivity 复制 onItemSelected 方法并粘贴 它进入 MainActivity。你已经有了一个 onItemSelected 方法 如果您告诉 Eclipse “添加未实现的方法”以响应 步骤 5A 之后会出现的错误。如果没有,请复制 在整个方法上。 (此步骤已针对中的问题进行了编辑 cmets):
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, ItemDetailActivity.class);
detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
这一行:
if (findViewById(R.id.item_detail_container) != null) {
始终为空。我认为我不需要在 NavigationDrawerActivity 的 onCreate 方法中使用它,因为在从我的导航菜单中选择该项目之前不会创建主细节片段,但我不确定将它放在哪里。我试过在片段被替换和提交后把它放好,但这似乎也不起作用。为清楚起见,我试图实现的是一个导航菜单,其中大多数片段将是主详细信息类型。如果有什么不清楚的地方请告诉我。
【问题讨论】:
标签: android android-fragments navigation-drawer master-detail