【问题标题】:Sliding Menu with Fragment changing带有片段更改的滑动菜单
【发布时间】:2013-06-13 16:20:54
【问题描述】:

我在我的应用程序中使用滑动菜单库。 滑动菜单是一个片段。

蓝色部分是打开时的滑动菜单。

红色部分是静态的,不会改变,顺便说一句,它是主要活动。 黄色部分是用户点击滑动菜单项时发生变化的片段。

这是我实现它时的问题:

在滑动菜单片段中,我监听 OnItemClick 事件,并根据点击项目的位置创建一个新片段。 之后,我用片段替换了黄色的帧 id。

        @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment frag = null;
        switch (arg2) {
        case 1:
            frag = new ExpFragment();
            break;

        case 2:
            frag = new FormFragment();
            break;

        case 3:
            frag = new CompFragment();
            break;

        default:
            frag = new ContactFragment();
            break;
        }
        transaction.replace(R.id.fragment, frag);
        transaction.commit();
    }

看起来不错吧?嗯,不。这是我得到的 logcat 异常。

06-13 09:28:29.739: E/AndroidRuntime(15422): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

所以,如果有人有线索,或者可以告诉我要看什么,那就太棒了!

TL;DR : 有 2 个片段(蓝色和黄色),蓝色必须改变黄色。给我一个例外。

谢谢,

编辑:布局文件: 活动主体:https://gist.github.com/dommerq/5771887 一个片段项目示例:https://gist.github.com/dommerq/5771892

【问题讨论】:

  • 嗯看起来不错...也许也发布您的布局?
  • 更新了我的问题(见编辑)! :)
  • 片段代码本身呢?
  • 是的,请看下面我的回答,谢谢 Stefan!

标签: android menu fragment


【解决方案1】:

答案在我的片段 java 代码中。

我有:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(com.quentindommerc.flatme.R.layout.f_contact, container);
    return v;
}

我应该有:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(com.quentindommerc.flatme.R.layout.f_contact, container, false);
    return v;
}

所以基本上,在 inflate 方法中,将“false”作为第三个参数。

编辑:更正拼写错误。

【讨论】:

  • 没错,想在回答之前确定你有这个:) 很高兴你自己发现了:)
  • @Quentin 我在我的项目中使用它。我问我是否在我的一个从右侧滑动的片段中创建了另一个 DrawerLayout 并且我想在其他片段中禁用它。就像这个play.google.com/store/apps/… .. 你能帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
相关资源
最近更新 更多