繁体   English   中英

具有“主详细信息”流的导航抽屉永远不会显示两个窗格

[英]Navigation Drawer with Master Detail flow never shows two panes

我正在尝试在导航抽屉模式旁边合并主从流程。 我已按照此答案中给出的说明进行操作。 这适用于显示一个窗格,但是对于这些步骤,她的指令要求将相关代码添加到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中。 如果您告诉Eclipse“添加未实现的方法”以响应在步骤5A之后将引发的错误,那么您将已经具有onItemSelected方法。 如果不这样做,请复制整个方法。 (此步骤已针对评论中的问题进行了编辑):

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);
}

这行:

如果(findViewById(R.id.item_detail_container)!= null){

始终为null。 我不认为我在NavigationDrawerActivity的onCreate方法中需要它,因为直到从我的导航菜单中选择了该主细节片段之后,才创建该主细节片段,但是我不确定该将它放在哪里。 我试过在片段被替换并提交后放入它,但这似乎也不起作用。 为了清楚起见,我要实现的是导航菜单,其中大多数片段将成为主从类型。 如果有任何不清楚的地方,请告诉我。

您是否在项目中添加了景观布局? Android具有包括土地布局的layout-land资源文件夹。

因此,您需要添加两个布局才能使用该过程。

布局资源文件夹包含以常规方向使用的布局文件

layout-land资源文件夹包含用于横向的布局文件

所以,

res / layout中的布局将仅包含列表的列表容器

res / layout-land的布局将包括列表容器item_detail_container

当没有res / layout-land时,Android从res / layout获取布局以横向显示。 而且您的布局不包含该视图。 这就是为什么您的条件始终为空的原因。

如果您添加具有包含两个视图的布局的res / layout-land ,它将解决您的错误。 请检查您的样品。 :)

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM