动态添加Fragment
在程序运行时, 根据具体情况来动态地添加Fragment到Activity中.
1 新建 AnotherRightFragment 作为另一个右侧Fragment . 并写好布局文件 another_right_fragment.xml.(黄色背景)
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AnotherRightFragment extends Fragment {
    
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment, container, false);
        return view;
    }
}
布局文件 another_right_fragment.xml.(黄色背景)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="另一个Fragment"
        android:textSize="20sp" />
</LinearLayout>  
2 修改 activity_main.xml. 将右侧Fragment放在了一个 FrameLayout 中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.yassblog.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.yassblog.RightFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
</LinearLayout>   
3 修改MainActivity 中的代码
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button:
            AnotherRightFragment fragment = new AnotherRightFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.right_layout, fragment);
            transaction.commit();
            break;
        default:
            break;
        }
    }
}
可以看到,首先我们给左侧Fragment中的按钮注册了一个点击事件,然后将动态添加Fragment的逻辑都放在了点击事件里进行。
4 动态添加Fragment的逻辑
- 创建待添加的Fragment实例。
- 获取到 FragmentManager,在Activity中可以直接调用 getFragmentManager()方法得到。
- 开启一个事务,通过调用 beginTransaction()方法开启。
- 向容器内加入Fragment,一般使用 replace()方法实现,需要传入容器的 id 和待添加的Fragment实例。
- 提交事务,调用 commit()方法来完成。