Fragment的静态加载

静态加载:把fragment当控件来使用

main2.xml在layout中定义fragment控件,

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     /* 1,Fragment的静态加载
 6 
 7       在activity的layout文件中声明Fragment,需要特别注意的是《fragment》中的android:name属性指定了在laytout中实例化的Fragemnt类
 8 
 9       标示Fragment的方法
10 
11       android:id 属性提供唯一的id
12 
13       android:name 属性提供唯一的name,为继承的Fragment类
14 
15       Fragment静态加载比较简单,可以当成普通的控件直接写在activity的布局中,
16 
17     */
18 <fragment
19     android:id="@+id/frame"
20     android:layout_width="wrap_content"
21     android:layout_height="wrap_content"
22     android:name="fragmentdemo.example.administrator.fragmentdemo.MyFragment"></fragment>
23 </LinearLayout>

创建一个MyFragment类继承Fragment

package fragmentdemo.example.administrator.fragmentdemo;

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 android.widget.TextView;

/**
 * Created by Administrator on 2016/5/6.
 */
public class MyFragment  extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /*resource:Fragment需要加载的布局文件
        root:加载layout中父ViewGroup
        attachToRoot:false,不返回父ViewGroup*/
        View view=inflater.inflate(R.layout.fragment,container,false);
        TextView textView= (TextView) view.findViewById(R.id.text);
        textView.setText("静态加载");
        return view;
    }
}

fragment加载的文件,fragment.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 <TextView
 6     android:layout_width="wrap_content"
 7     android:layout_height="wrap_content"
 8     android:id="@+id/text"
 9 
10     />
11     <Button
12         android:text="改变"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:id="@+id/button"/>
16 
17 </LinearLayout>

主方法

 1 package fragmentdemo.example.administrator.fragmentdemo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import android.app.Activity;
 8 import android.app.Fragment;
 9 import android.app.FragmentManager;
10 import android.app.FragmentTransaction;
11 import android.content.Intent;
12 import android.os.Bundle;
13 import android.widget.RadioGroup;
14 import android.widget.RadioGroup.OnCheckedChangeListener;
15 /*1)Fragment可以作为Activity界面的一部分组成出现;
16         (2)可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用;
17         (3)在Activity运行过程中,可以添加、移除或替换Fragment;
18         (4)Fragment可以响应自己的输入事件,并且有自己的声明周期,它们的生命周期受宿主Activity的生命周期影响;
19         (5)Fragment在第一次绘制它的用户界面时,系统会调用onCreateView()方法,此方法返回一个View。(如果不显示UI,返回null);
20         Fragment两种加载方式:静态加载、动态加载。*/
21 
22 public class MainActivity extends Activity implements OnCheckedChangeListener
23 {
24 
25     private RadioGroup group;
26 
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         // TODO Auto-generated method stub
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.activity_main);
32         group = (RadioGroup) findViewById(R.id.radiogroup);
33         group.setOnCheckedChangeListener(this);
34 
35     }
36 
37     @Override
38     public void onCheckedChanged(RadioGroup group, int checkedId) {
39         // TODO Auto-generated method stub
40 
41         switch (checkedId) {
42             case R.id.first: {
43                 Intent intent=new Intent(this,MainActivity2.class);
44                 startActivity(intent);
45                 break;
46 
47             }
48             case R.id.second: {
49                /*Fragment 动态加载*/
50                 MyFragment2 myFragment2=new MyFragment2();/*创建实例*/
51                 FragmentManager fragmentManager = getFragmentManager();/*获取到FragmentManager*/
52                 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();/*开启事务*/
53                 fragmentTransaction.add(R.id.frame,myFragment2);
54                 fragmentTransaction.addToBackStack(null);/*通过物理返回键返回*/
55                 fragmentTransaction.commit();/*提交事务*/
56 
57 
58                 break;
59             }
60             case R.id.thrid: {
61                 Intent intent=new Intent(this,MainActivity3.class);
62                 startActivity(intent);
63                 break;
64 
65 
66             }
67             case R.id.fourth: {
68                 Intent intent=new Intent(this,MainActivity4.class);
69                 startActivity(intent);
70                 break;
71 
72 
73             }
74         }
75     }
76 
77 
78 
79 
80 
81 
82 }

主方法加载布局

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

 在AndroidManifest中注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="fragmentdemo.example.administrator.fragmentdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="fragmentdemo.example.administrator.fragmentdemo.MainActivity2"
            ></activity>
        <activity android:name=".MainActivity3"></activity>
        <activity android:name=".MainActivity4"></activity>
    </application>

</manifest>

 

posted @ 2016-05-07 10:48  成功人土  阅读(1333)  评论(0编辑  收藏  举报