Fragment学习记录二 动态创建fragment

动态创建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调用多次

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值