【问题标题】:Navigation Drawer with Master Detail flow never shows two panes具有主详细信息流的导航抽屉从不显示两个窗格
【发布时间】: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


    【解决方案1】:

    您是否在项目中添加了横向布局? Android 有 layout-land 资源文件夹,其中包含横向布局。

    所以你需要添加两个布局来使用这个过程。

    layout资源文件夹包含在正常方向使用的布局文件

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

    所以,

    res/layout 中的布局将仅包含 list container 用于列表

    res/layout-land 的布局将包括 list containeritem_detail_container

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

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

    谢谢

    【讨论】:

    • 我现在不清楚。你想怎么做?您的代码适用于此设计。如果手机是纵向的,应用程序将只显示列表,onItemClick 将转到详细信息页面。如果手机是横向的,应用会按照你说的显示两种布局。
    • 我正在一个宽度超过 600dp 的实际平板电脑上进行测试。我知道这一点是因为我创建的其他布局在此设备上是正确的。当我添加主从活动时,向导会为 sw600dp 创建一个布局。为了完整起见,我将其复制到 layout-land 文件夹中。我从来没有得到两视图布局。我在 Android Studio 中创建了一个空的主从项目,它工作正常。一旦我添加了导航抽屉,它就不再显示双视图布局,即使我已经按照链接答案中的说明进行操作。我不知道为什么或我可能会错过什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2016-01-04
    • 2013-10-22
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多