gpt4 book ai didi

android - 如何以编程方式创建多个 fragment ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:38 24 4
gpt4 key购买 nike

我试图通过编程方式在一个屏幕上显示多个 fragment 。通过将每个 fragment 包含到 Activity 布局文件中,我可以毫无问题地做到这一点。但是,当我尝试以编程方式执行此操作时,我感到很困惑。这就是我目前对屏幕上的两个 fragment 所做的。

主类:

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();

}
}

fragment 一:

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;
}

}

fragment 二:

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;
}

}

我的两个 fragment 类的布局文件只包含一个简单的 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 的来源。我如何确定我的新 fragment 将在屏幕上的哪个位置?谢谢

编辑:我正在平板电脑而不是手机上开发这个。现在已经解决了,上面的代码将以编程方式添加三个 fragment 。

最佳答案

<?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

并充分理解 fragment 生命周期等。我展示的方式是快速和肮脏的,但不一定是最好的。

关于android - 如何以编程方式创建多个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com