fragment动态加载

这篇博客介绍了如何在Android中实现Fragment的动态加载。通过点击button,将不同的Fragment加载到Activity中的FrameLayout中。主要步骤包括获取FragmentManager,创建FragmentTransaction,替换布局中的Fragment并提交。同时强调FragmentTransaction的commit()不应作为全局变量,避免出现异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇博客讲了fragment静态加载,现在来讲讲动态加载。
想要实现的效果是一个Activity中有两个button,下方是一个FrameLayout;当点击button,就会促发相应事件,并将对应的fragment页面加载到FrameLayout中。如下图所示:
这里写图片描述

这里写图片描述

MainActivity.java:
【注意】FragmentTransaction不要设置为全局变量,否则,其commit()只能使用一次,如果再被调用则会出现 java.lang.IllegalStateException: commit already called的异常

动态加载的5个步骤:
1.通过getFragmentManager()方法FragmentManager 对象;
2.通过fragmentManager.beginTransaction()获取FragmentTransaction对象;
3.创建一个MyFragmentone对象;
4.通过fragmentTransaction.replace(加载fragment页面的控件的id(即“容器”),MyFragmentone对象);
5.通过fragmentTransaction.commit()方法提交。

package activity.wyc.com.framentdemoone;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

import com.wyc.fragment.MyFragmentone;
import com.wyc.fragment.MyFragmenttwo;


public class MainActivity extends ActionBarActivity {

    private Button btn01Obj,btn02Obj;

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



        btn01Obj = (Button)findViewById(R.id.btn01id);
        btn01Obj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                MyFragmentone fragment01 = new MyFragmentone();
                fragmentTransaction.replace(R.id.flid, fragment01);
                fragmentTransaction.commit();
            }
        });

        btn02Obj = (Button)findViewById(R.id.btn02id);
        btn02Obj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                MyFragmenttwo fragment02 = new MyFragmenttwo();
                fragmentTransaction.replace(R.id.flid,fragment02);
                fragmentTransaction.commit();
            }
        });
    }
}

MyFragmentone.java:
动态加载布局文件(和fragment静态加载一样)

package com.wyc.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import activity.wyc.com.framentdemoone.R;

/**
 * Created by yc on 2015/3/4.
 */
public class MyFragmentone extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment01,null);

        return view;
    }
}

MyFragmenttwo.java

package com.wyc.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import activity.wyc.com.framentdemoone.R;

/**
 * Created by yc on 2015/3/4.
 */
public class MyFragmenttwo extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment02,null);

        return  view;
    }
}

activity_main.xml:
使用fragment动态加载可以不需要写fragment标签了o(^▽^)o

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载第一个fragment页面"
        android:textSize="20sp"
        android:id="@+id/btn01id"
        android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加载第二个fragment页面"
            android:textSize="20sp"
            android:id="@+id/btn02id"
            android:layout_weight="1"/>


    </LinearLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/flid"></FrameLayout>

</LinearLayout>

fragment01.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第一个fragment页面"
        android:textSize="20sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/red"
        android:id="@+id/fm01imgid" />


</LinearLayout>

fragment02.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第二个fragment页面"
        android:textSize="20sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/blue"
        android:id="@+id/fm02imgid" />


</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值