Android 动态创建Fragment

Fragment是activity的界面中的一部分或一种行为。可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment。可以把Fragment任务模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除。

本文地址:http://www.cnblogs.com/wuyudong/p/5893804.html,转载请注明源地址。

Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,他拥有的所有的Fragment都暂停了,当activity销毁时,他拥有的所有Fragment都被销毁。然而,当activity运行时(在onResume()之后,onPause()之前),可以单独地操作每个Fragment,比如添加或删除它们。当中执行上述针对Fragment的事务时,可以将事务添加到一个栈中,这个栈被activity管理,栈中的每一条都是一个Fragment的一次事务。有了这个栈,就可以反向执行Fragment的事务,这样就可以在Fragment级支持“返回”键(向后导航)。

当向activity中添加一个Fragment时,它须置于ViewGroup控件中,并且需定义Fragment自己的界面。可以在layout.xml布局文件中声明Fragment,元素为:<fragment>;也可以在代码中创建Fragment,然后把它加入到ViewGroup控件中。然而,Fragment不一定非要放在activity的界面中,它可以隐藏在后台为activity工作。

实战一下

项目布局文件代码:

<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:orientation="horizontal"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.wuyudong.fragment.Fragment1"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:name="com.wuyudong.fragment.Fragment2"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </fragment>

</LinearLayout>

接着在layout文件夹下新建两个fragment.xml文件

fragment1.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:background="#0000ff"
    android:orientation="vertical" >
</LinearLayout>

fragment2.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:background="#ff00"
    android:orientation="vertical" >
</LinearLayout>

接着在项目源代码文件夹下新建两个java文件

Fragment1.java

public class Fragment1 extends Fragment {
    /**
     * 当fragment被创建的时候,调用的方法,返回当前fragment显示的内容 
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, null);
    }

}

Fragment2.java

public class Fragment2 extends Fragment {
    /**
     * 当fragment被创建的时候,调用的方法,返回当前fragment显示的内容 
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, null);
    }

}

运行项目


接下来实现动态创建Fragment

在刚才的项目的基础上,新建一个项目。删除原来activity_main.xml代码中的fragment1与fragment2代码段,其他的代码不变

接下来在MainActivity中添加下面的代码:

public class MainActivity extends Activity {

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

        // 1、判断当前手机的朝向
        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        if (width > height) {
            // 水平方向
            ft.replace(android.R.id.content, fragment1);
        } else {
            ft.replace(android.R.id.content, fragment2);
        }

        ft.commit();

    }

}

上面的代码实现了当手机不同朝向的时候,显示的不同的fragment

posted @ 2016-09-22 15:22  wuyudong  阅读(3617)  评论(4编辑  收藏  举报
Top_arrow