首页 > 技术文章 > Fragment动态加载

633sylss 2016-05-07 11:12 原文

/*Fragment 动态加载*/
MyFragment2 myFragment2=new MyFragment2();/*创建实例*/
FragmentManager fragmentManager = getFragmentManager();/*获取到FragmentManager*/
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();/*开启事务*/
fragmentTransaction.add(R.id.frame,myFragment2);
fragmentTransaction.addToBackStack(null);/*通过物理返回键返回*/
fragmentTransaction.commit();/*提交事务*/

主方法

package fragmentdemo.example.administrator.fragmentdemo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
/*1)Fragment可以作为Activity界面的一部分组成出现;
        (2)可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用;
        (3)在Activity运行过程中,可以添加、移除或替换Fragment;
        (4)Fragment可以响应自己的输入事件,并且有自己的声明周期,它们的生命周期受宿主Activity的生命周期影响;
        (5)Fragment在第一次绘制它的用户界面时,系统会调用onCreateView()方法,此方法返回一个View。(如果不显示UI,返回null);
        Fragment两种加载方式:静态加载、动态加载。*/

public class MainActivity extends Activity implements OnCheckedChangeListener
{

    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        group = (RadioGroup) findViewById(R.id.radiogroup);
        group.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub

        switch (checkedId) {
            case R.id.first: {
                Intent intent=new Intent(this,MainActivity2.class);
                startActivity(intent);
                break;

            }
            case R.id.second: {
               /*Fragment 动态加载*/
                MyFragment2 myFragment2=new MyFragment2();/*创建实例*/
                FragmentManager fragmentManager = getFragmentManager();/*获取到FragmentManager*/
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();/*开启事务*/
                fragmentTransaction.add(R.id.frame,myFragment2);
                fragmentTransaction.addToBackStack(null);/*通过物理返回键返回*/
                fragmentTransaction.commit();/*提交事务*/


                break;
            }
            case R.id.thrid: {
                Intent intent=new Intent(this,MainActivity3.class);
                startActivity(intent);
                break;


            }
            case R.id.fourth: {
                Intent intent=new Intent(this,MainActivity4.class);
                startActivity(intent);
                break;


            }
        }
    }
}

 

加载的main.xml布局,指定linerlayout的id

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <LinearLayout
 7         android:id="@+id/frame"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:orientation="vertical" >
11     </LinearLayout>
12 
13 
14 
15     <RadioGroup
16         android:id="@+id/radiogroup"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:layout_alignParentBottom="true"
20         android:gravity="center_horizontal"
21         android:orientation="horizontal" >
22 
23         <RadioButton
24             android:id="@+id/first"
25             android:layout_width="0dp"
26             android:layout_height="wrap_content"
27             android:layout_weight="1"
28             android:background="@drawable/radio_pressed"
29             android:button="@null"
30             android:drawableTop="@mipmap/ic_launcher"
31             android:gravity="center_horizontal"
32             android:text="静态加载" />
33 
34         <RadioButton
35             android:id="@+id/second"
36             android:layout_width="0dp"
37             android:layout_height="wrap_content"
38             android:layout_weight="1"
39             android:background="@drawable/radio_pressed"
40             android:button="@null"
41             android:drawableTop="@mipmap/ic_launcher"
42             android:gravity="center_horizontal"
43             android:text="动态加载" />
44 
45         <RadioButton
46             android:id="@+id/thrid"
47             android:layout_width="0dp"
48             android:layout_height="wrap_content"
49             android:layout_weight="1"
50             android:background="@drawable/radio_pressed"
51             android:button="@null"
52             android:drawableTop="@mipmap/ic_launcher"
53             android:gravity="center_horizontal"
54             android:text="生命周期" />
55 
56         <RadioButton
57             android:id="@+id/fourth"
58             android:layout_width="0dp"
59             android:layout_height="wrap_content"
60             android:layout_weight="1"
61             android:background="@drawable/radio_pressed"
62             android:button="@null"
63             android:drawableTop="@mipmap/ic_launcher"
64             android:gravity="center_horizontal"
65             android:text="传值通信" />
66     </RadioGroup>
67 
68 </RelativeLayout>

MyFragment2继承自Fragment

 1 package fragmentdemo.example.administrator.fragmentdemo;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.support.annotation.Nullable;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.widget.TextView;
10 
11 /**
12  * Created by Administrator on 2016/5/6.
13  */
14 public class MyFragment2 extends Fragment{
15     @Nullable
16     @Override
17     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
18         /*resource:Fragment需要加载的布局文件
19         root:加载layout中父ViewGroup
20         attachToRoot:false,不返回父ViewGroup*/
21         View view=inflater.inflate(R.layout.fragment,container,false);
22         TextView textView= (TextView) view.findViewById(R.id.text);
23         textView.setText("动态加载");
24         return view;
25 
26     }
27 }

fragment.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">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text"

    />
    <Button
        android:text="改变"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"/>

</LinearLayout>

 

推荐阅读