fragment动态加载

来源:互联网 发布:万网查询域名备案号 编辑:程序博客网 时间:2024/06/06 04:27

上一篇博客讲了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>
1 1