如何通过编程创建多个碎片?

4
我正在尝试通过编程方式创建多个片段并在一个屏幕上显示它们。如果将每个片段包含到活动布局文件中,我可以轻松地完成这个任务。但是当我尝试通过编程方式实现时,我感到困惑了。下面是我为一个屏幕上的两个片段所做的工作。
主类:
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentOne one = new FragmentOne();
        FragmentTwo two = new FragmentTwo();
        FragmentThree three = new FragmentThree();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.imOne, one, "fragmentone");
        ft.add(R.id.imTwo, two, "fragmenttwo");
        ft.add(R.id.imThree, three, "fragmenthree");
        ft.commit();

    }
}

片段一:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_one, container, false);
        return view;
    }

}

第二个片段:

public class FragmentTwo extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_two, container, false);
        return view;
    }

}

我的两个片段类的布局文件只包含一个简单的TextView。 然后是我的主类的activity_main.xml布局文件:
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/imOne"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imTwo"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imThree"
        android:name="com.david.twofragexample.FragmentThree"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

当我试图通过编程来完成时,我对我的activity_main.xml文件应该包含什么感到困惑。因此,根据Android开发者指南(http://developer.android.com/guide/components/fragments.html)的说法,建议使用。
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

当我将此代码添加到我的onCreate方法中时,我需要对activity_main.xml文件进行哪些更改才能以编程方式执行此操作?我看不到R.id.fragment_container来自哪里。我如何确定我的新片段在屏幕上的位置?谢谢
编辑:我正在平板电脑上开发而不是手机。这个问题现在已经解决了,上述代码将以编程方式添加三个片段。

你的 activity_main.xml 文件中是否有 fragment_container 元素? - Audrius
是的,我刚刚添加了它,谢谢。 - DMC
3个回答

3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/list"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/viewer"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

在您的Activity中:

FragmentOne one = new FragmentOne();
FragmentTwo two = new FragmentTwo();

fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.list, one, "fragmentone").commit();
fm.beginTransaction().replace(R.id.viewer, two, "fragmenttwo").commit();

我建议你学习这个链接:http://developer.android.com/reference/android/app/Fragment.html,并深入理解碎片的生命周期等。虽然我的方法快而简便,但不一定是最好的选择。

非常感谢,您已经通过布局部分为我澄清了这个问题!在您上面的代码中,您只需要调用一次“commit”即可。那对我来说是有效的。再次感谢。 - DMC
@accordionfolder 这很有帮助,而且很容易。链接也很好。 - user1707035

2
"最初的回答"
由于我还没有足够高的声望来发表评论,所以我只会在这里留下信息 - Rafael T建议的方法确实很好,尽管一开始我也有疑问。
1)像这样定义您的xml:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

2) 将每个片段添加到同一插槽中:

View box = v.findViewById(R.id.container);
FragmentTransaction ft = getFragmentManager().beginTransaction();
for (String catalog : mCatalogs) {
    Fragment fragment = HighlightedProductFragment.newInstance(catalog);
    ft.add(R.id.container, fragment, catalog);
}
ft.commit();

感谢您提供解决这个棘手需求的方案。
最初的回答:

感谢您为这个棘手需求提供的解决方案。


0

可以尝试这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

作为你的activity_main.xml

你问了自己一个正确的问题:id为fragment_container在哪里,答案是:不存在。此外,你将布局设置为水平的,这很可能会使添加的视图有点“超出屏幕”。


我的布局主要文件里只需要这些吗?没有其他的了吗? - DMC
如果你想以编程方式添加Fragment,那么是的。 - Rafael T
那么我如何选择新片段在屏幕上的位置呢?如果我想像上面所做的那样将3个片段并排放置,我该如何以编程方式实现呢? - DMC
你有找到解决方案吗?我也遇到了类似的问题。 如果我们在XML中放置两三个FrameLayout,没有定义任何片段,然后将片段添加到这些FrameLayout中,这样会起作用吗? - DcodeChef
我不会使用FrameLayouts,而是使用Linear或Relative Layouts,因为FrameLayouts有时候边界很丑。但无论如何,这应该可以工作。 - Rafael T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接