Android之Fragment静态加载

来源:互联网 发布:python反编译 编辑:程序博客网 时间:2024/05/10 03:27

1、Fragment知识概要

Android3.0引入了Fragment,主要目的是用在大屏幕设备上,支持更加动态和灵活的UI设计。Fragment在你的应用中应当是一个模块化和可重用的组件,因为Fragment定义了它自己的布局,以及通过使用它自己的声明周期回调回调方法定义了它自己的行为,可以将Fragment包含到多个Activity中。

(1)Fragment可以作为Activity界面的一部分组成出现;

(2)可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用;

(3)在Activity运行过程中,可以添加、移除或替换Fragment;

(4)Fragment可以响应自己的输入事件,并且有自己的声明周期,它们的生命周期受宿主Activity的生命周期影响;

(5)Fragment在第一次绘制它的用户界面时,系统会调用onCreateView()方法,此方法返回一个View。(如果不显示UI,返回null);

Fragment两种加载方式:静态加载、动态加载。

2、准备阶段:

本文以及后续将使用一个APP来讲解关于Fragment的知识,大致布局如下:


      values添加color.xml:

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <color name="gray">#88000000</color>  
  5.     <color name="white">#ffffff</color>  
  6.   
  7. </resources>  
      drawable中添加radio_pressed.xml

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@color/gray" android:state_checked="true"></item>  
  5.     <item android:drawable="@color/white" android:state_pressed="true"></item>  
  6.     <item android:drawable="@color/white"></item>  
  7.   
  8. </selector>  
      main主布局:

[html] view plain copy
print?
  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="@drawable/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="@drawable/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="@drawable/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="@drawable/ic_launcher"  
  64.             android:gravity="center_horizontal"  
  65.             android:text="传值通信" />  
  66.     </RadioGroup>  
  67.   
  68. </RelativeLayout>  
     MainActivity加载main:

[java] view plain copy
print?
  1. package com.example.fragment;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.RadioGroup.OnCheckedChangeListener;  
  8.   
  9. public class MainActivity extends Activity implements OnCheckedChangeListener {  
  10.   
  11.     private RadioGroup group;  
  12.       
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         group=(RadioGroup) findViewById(R.id.radiogroup);  
  19.         group.setOnCheckedChangeListener(this);  
  20.     }  
  21.   
  22.     @Override  
  23.     public boolean onCreateOptionsMenu(Menu menu) {  
  24.         // Inflate the menu; this adds items to the action bar if it is present.  
  25.         getMenuInflater().inflate(R.menu.main, menu);  
  26.         return true;  
  27.     }  
  28.   
  29.     @Override  
  30.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  31.         // TODO Auto-generated method stub  
  32.         switch (checkedId) {  
  33.         case R.id.first:  
  34.             //演示静态加载  
  35.               
  36.             break;  
  37.   
  38.         case R.id.second:  
  39.             //演示动态加载  
  40.               
  41.             break;  
  42.               
  43.         case R.id.thrid:  
  44.             //演示生命周期  
  45.               
  46.             break;  
  47.               
  48.         case R.id.fourth:  
  49.             //演示传值通信  
  50.               
  51.             break;  
  52.         }  
  53.     }  
  54.   
  55. }  

3、静态加载

       在Activity的layout文件中声明Fragment(特别注意:在<fragment>标签中的Android: name属性中指定了在layout中实例化的Fragment类),标识Fragment的方法:A.android: id 属性提供一个唯一ID;B.android: tag属性提供一个唯一字符串;

添加fragment.xml:

[html] view plain copy
print?
  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.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:id="@+id/text"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="我的Fragment"/>  
  12.     <Button android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/button"/>  
  15.       
  16. </LinearLayout>  
添加MyFragment类,并加载fragment布局:
[java] view plain copy
print?
  1. package com.example.fragment;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class MyFragment extends Fragment {  
  13.     @Override  
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  15.             Bundle savedInstanceState) {  
  16.         // layout布局文件转换成View对象  
  17.         /** 
  18.          * inflater.inflate(resource, root, attachToRoot) 
  19.          * resource:Fragment需要加载的布局文件 
  20.          * root:加载layout的父ViewGroup 
  21.          * attactToRoot:false,不返回父ViewGroup 
  22.          */  
  23.         View view = inflater.inflate(R.layout.fragment, container, false);  
  24.         TextView text = (TextView) view.findViewById(R.id.text);  
  25.         Button button = (Button) view.findViewById(R.id.button);  
  26.         text.setText("静态加载Fragment");  
  27.         button.setText("获取内容");  
  28.         button.setOnClickListener(new OnClickListener() {  
  29.   
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 // TODO Auto-generated method stub  
  33.                 // String value = getAaa();  
  34.                 // Toast.makeText(getActivity(), "value="+value,  
  35.                 // Toast.LENGTH_SHORT).show();  
  36.             }  
  37.         });  
  38.         return view;  
  39.   
  40.     }  
  41. }  
添加jigntai.xml:

[html] view plain copy
print?
  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.     android:orientation="vertical" >  
  6.       
  7.     <!-- android:id:静态加载必须指定一个ID -->  
  8.     <!-- android:name:完整包名 -->  
  9.     <fragment   
  10.         android:id="@+id/fragment"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:name="com.example.fragment.MyFragment"    
  14.         />  
  15. </LinearLayout>  

添加JingTaiActivity类:

[java] view plain copy
print?
  1. public class JingTaiActivity extends Activity {  
  2.       
  3.     private TextView tv;  
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         // TODO Auto-generated method stub  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.jingtai);  
  10.           
  11.         Button button=(Button) findViewById(R.id.button);  
  12.         tv=(TextView) findViewById(R.id.text);  
  13.         button.setText("改变");  
  14.         button.setOnClickListener(new OnClickListener() {  
  15.               
  16.             @Override  
  17.             public void onClick(View v) {  
  18.                 // TODO Auto-generated method stub  
  19.                 tv.setText("TextView改变了");  
  20.             }  
  21.         });  
  22.     }  
  23. }  

主MainActivity中演示静态加载部分添加:

[java] view plain copy
print?
  1. case R.id.first:  
  2.     //演示静态加载  
  3.     Intent jingtaiIntent=new Intent(MainActivity.this,JingTaiActivity.class);  
  4.     startActivity(jingtaiIntent);  
  5.     break;  

      MainActivity跳转到JingTaiActivity,里面加载了一个<fragment>,而其中android:name属性是com.example.fragment.MyFragment,在这个MyFragment中又有自己的text、button布局。再回到JingTaiActivity,在其中加载了jingtai.xml,并且可以直接通过findViewById找到MyFragment的布局文件fragment中的text、button。

      也就是说,当一个布局文件中通过静态加载Fragment加载到Activity中来,Fragment中的布局文件对Activity也是共享的。

0 0