微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 将片段添加到片段中(嵌套片段)

我想动态地将youtube片段添加到我已经存在的片段中.我使用的代码如下:

        // setting the Youtube Player Dynamically
private int setYoutubePlayer(String desc,View view,int prevID,Bundle input) {

    if (desc.indexOf("=") != -1) {
        desc = desc.substring(desc.indexOf("=") + "=".length());
    } else {
        return prevID;
    }

    final String url = desc;

    LinearLayout videoLayout = new LinearLayout(view.getContext());
    videoLayout.setorientation(LinearLayout.VERTICAL);
    prevID++;
    videoLayout.setId(prevID);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    fragment.setVideoId(url);
    LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.items);
    itemLayout.addView(videoLayout);

    fragmentTransaction.add(itemLayout.getId(),fragment,"youtube fargment " + prevID);

    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

    return prevID;
}

我需要在适当的片段中获取youtube片段.当我检查总是加载一个新片段时(当在片段之间滑动时),新的内部片段需要是第一个加载的片段.

任何帮助都会很乐意接受.

解决:谢谢你Koby你是对的.我不得不替换“getActivity().getSupportFragmentManager();”使用“getChildFragmentManager()”.问题显然是Sherlock库附带了一个旧的android v4支持库.我不得不更新Sherlock中的支持库.它对我有用…..

最佳答案
要在片段内创建嵌套片段,您应该使用:

http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()

从父片段调用getChildFragmentManager(),
并在父项中执行事务以将子项嵌套在其中.

https://stackoverflow.com/a/13381825/1693674

告诉我你是否需要更多帮助…

原文地址:https://www.jb51.cc/android/430926.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐