如何使用以编程方式创建的内容视图向活动添加片段 [英] How do I add a Fragment to an Activity with a programmatically created content view

查看:27
本文介绍了如何使用以编程方式创建的内容视图向活动添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向以编程方式实现其布局的 Activity 添加一个 Fragment.我查看了 Fragment 文档,但描述我需要的示例并不多.这是我尝试编写的代码类型:

I want to add a Fragment to an Activity that implements its layout programmatically. I looked over the Fragment documentation but there aren't many examples describing what I need. Here is the type of code I tried to write:

public class DebugExampleTwo extends Activity {

    private ExampleTwoFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frame = new FrameLayout(this);
        if (savedInstanceState == null) {
            mFragment = new ExampleTwoFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(frame.getId(), mFragment).commit();
        }

        setContentView(frame);
    }
}

...

public class ExampleTwoFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container, 
                             Bundle savedInstanceState) {
        Button button = new Button(getActivity());
        button.setText("Hello There");
        return button;
    }
}

此代码编译但在开始时崩溃,可能是因为我的 FragmentTransaction.add() 不正确.这样做的正确方法是什么?

This code compiles but crashes at start, probably because my FragmentTransaction.add() is incorrect. What is the correct way to do this?

推荐答案

事实证明,该代码存在多个问题.片段不能以这种方式声明,在与活动相同的 java 文件中,而不是作为公共内部类.框架期望片段的构造函数(没有参数)是公开的和可见的.将片段作为内部类移动到 Activity 中,或者为片段创建一个新的 java 文件可以解决这个问题.

It turns out there's more than one problem with that code. A fragment cannot be declared that way, inside the same java file as the activity but not as a public inner class. The framework expects the fragment's constructor (with no parameters) to be public and visible. Moving the fragment into the Activity as an inner class, or creating a new java file for the fragment fixes that.

第二个问题是,当您以这种方式添加片段时,您必须传递对片段的包含视图的引用,并且该视图必须具有自定义 ID.使用默认 ID 会使应用程序崩溃.这是更新后的代码:

The second issue is that when you're adding a fragment this way, you must pass a reference to the fragment's containing view, and that view must have a custom id. Using the default id will crash the app. Here's the updated code:

public class DebugExampleTwo extends Activity {

    private static final int CONTENT_VIEW_ID = 10101010;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frame = new FrameLayout(this);
        frame.setId(CONTENT_VIEW_ID);
        setContentView(frame, new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        if (savedInstanceState == null) {
            Fragment newFragment = new DebugExampleTwoFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(CONTENT_VIEW_ID, newFragment).commit();
        }
    }

    public static class DebugExampleTwoFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            EditText v = new EditText(getActivity());
            v.setText("Hello Fragment!");
            return v;
        }
    }
}

这篇关于如何使用以编程方式创建的内容视图向活动添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆