动态创建Fragment
步骤:
1.获取到FragmentManager,V4包中 getSupportFragmentManager,系统中 getFragmentManager
2.开启一个事务,通过调用beginTransaction方法开启。
3.向容器内加入Fragment,一般使用add或者replace方法实现,需要传入容器的id和Fragment的实例。
4.提交事务,调用commit方法提交。
create_dynamic_activity.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="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="fragment1"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="fragment2"/>
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3">
</FrameLayout>
</LinearLayout>
CreateDynamicActivityActivity.java
package com.mytest.fragmentall.create;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import com.mytest.fragmentall.R;
public class CreateDynamicActivityActivity extends FragmentActivity implements View.OnClickListener {
private FrameLayout fragment;
FragmentManager manager ;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_dynamic_activity);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
fragment = (FrameLayout) findViewById(R.id.fragment);
manager = getSupportFragmentManager();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
transaction = manager.beginTransaction();
TitleFragment
fragment = new TitleFragment();
transaction.add(R.id.fragment, fragment);
transaction.commit();
break;
case R.id.button2:
transaction = manager.beginTransaction();
ContentFragment
fragment2 = new ContentFragment();
transaction.add(R.id.fragment, fragment2);
transaction.commit();
break;
}
}
}
参考记录一
fragment_1.xml
TitleFragment.java
fragment_2.xml
ContentFragment.java
fragment_2.xml 简单修改
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e6a0a0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"
android:id="@+id/textView2"
android:gravity="center"
android:textSize="20sp"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
注意事项:
......
transaction 在点击事件前赋值
transaction = manager.beginTransaction();
......
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
TitleFragment
fragment = new TitleFragment();
transaction.add(R.id.fragment, fragment);
transaction.commit();
break;
case R.id.button2:
ContentFragment
fragment2 = new ContentFragment();
transaction.add(R.id.fragment, fragment2);
transaction.commit();
break;
}
}
报异常
java.lang.IllegalStateException: commit already called
原因是:
commit不能被同一个FragmentTransaction调用多次