抱歉,这个问题是针对那些使用 Eclipse 并可以访​​问 ApiDemo示例代码的人员。
具体来说,我正在尝试将片段 Activity 基于名为的样本FragmentLayout

以下代码对我来说是有问题的(您可以在ApiDemo FragmentLayout.java ShowDetails()方法中找到完整的代码):

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if (index == 0) {
                    ft.replace(R.id.details, details);
                } else {
                    ft.replace(R.id.a_item, details);
                }
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();

我有两个问题:
  • index == 0index != 0有什么区别?
  • 资源R.id_a_item(仅在搜索后出现在所有ApiDemos中)属于某种菜单快捷方式资源,根本不清楚为什么在这里使用它。

  • android.developers指南没有解释这段代码。

    最佳答案



    位置0与列表的其他位置之间应该没有任何区别,因为代码被设置为用新的片段替换先前的细节片段。



    这很可能是示例中的错误,因为使用该id会引发异常,因为它在当前布局中不存在(我运行了4.2模拟器上找到的API Demos项目,并且未引发 View 未发现异常。 .etc作为该ID)。样本的最后一个版本中可能有一个错误,因为您所质疑的代码段在其他版本中不存在。

    08-24 19:29