android 3.0提供的Fragment(碎片)

HoneyComb3.0组件运用可以看这里: 
http://blog.csdn.net/mayingcai1987/article/category/786494  

http://www.android123.com.cn/androidkaifa/772.html  
Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面Android123介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity. 

  一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。 

1,先定义2个Fragment,布局文件R.layout.first&R.layout.second根据自己需求随便写一个,我这里就不贴代码了。 
Java代码   收藏代码
  1. import android.app.Fragment;  
  2. import android.os.Bundle;  
  3. import android.view.ContextMenu;  
  4. import android.view.LayoutInflater;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.view.ContextMenu.ContextMenuInfo;  
  10.   
  11. public class FirstFragment extends Fragment{  
  12.   
  13.       
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.     }  
  19.   
  20.     public View onCreateView(LayoutInflater inflater, ViewGroup container,   
  21.                              Bundle savedInstanceState) {  
  22.         View root = inflater.inflate(R.layout.first, container, false);  
  23.         registerForContextMenu(root.findViewById(R.id.editText1));  
  24.         return root;   
  25.     }   
  26.       
  27.       
  28.     @Override  
  29.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
  30.         super.onCreateContextMenu(menu, v, menuInfo);  
  31.         menu.add(Menu.NONE, 0, Menu.NONE, "菜单1");  
  32.         menu.add(Menu.NONE, 1, Menu.NONE, "菜单2");  
  33.     }  
  34.   
  35.     @Override  
  36.     public boolean onContextItemSelected(MenuItem item) {  
  37.         return super.onContextItemSelected(item);  
  38.     }  
  39.       
  40. }  

Java代码   收藏代码
  1. import android.app.Fragment;  
  2. import android.os.Bundle;  
  3. import android.view.LayoutInflater;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6.   
  7. public class SecondFragment extends Fragment{  
  8.   
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         // TODO Auto-generated method stub  
  12.         super.onCreate(savedInstanceState);  
  13.     }  
  14.       
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,   
  16.             Bundle savedInstanceState) {   
  17.         return inflater.inflate(R.layout.second, container, false);   
  18.     }   
  19. }  


2,在Activity中使用 
Java代码   收藏代码
  1. import android.app.Activity;  
  2. import android.app.Fragment;  
  3. import android.app.FragmentManager;  
  4. import android.app.FragmentTransaction;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class Honeycomb extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17. //        FirstFragment firstFragment=new FirstFragment();  
  18. //        //在Activity中通过这个与Fragment通讯  
  19. //        getFragmentManager().beginTransaction().add(android.R.id.content, firstFragment).commit();  
  20.           
  21.         FragmentManager fm = getFragmentManager();  
  22.         addShowHideListener(R.id.btn_1, fm.findFragmentById(R.id.firstFragment));  
  23.         addShowHideListener(R.id.btn_2, fm.findFragmentById(R.id.secondFragment));  
  24.           
  25.     }  
  26.       
  27.     void addShowHideListener(int buttonId, final Fragment fragment) {  
  28.         final Button button = (Button)findViewById(buttonId);  
  29.         button.setOnClickListener(new OnClickListener() {  
  30.             public void onClick(View v) {  
  31.                 FragmentTransaction ft = getFragmentManager().beginTransaction();  
  32.                 //为Fragment设置淡入淡出效果  
  33.                 ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);  
  34.                           
  35.                 if (fragment.isHidden()) {  
  36.                     ft.show(fragment);  
  37.                     button.setText("隐藏");  
  38.                 } else {  
  39.                     ft.hide(fragment);  
  40.                     button.setText("显示");  
  41.                 }  
  42.                 ft.commit();  
  43.             }  
  44.         });  
  45.     }  
  46.       
  47. }  

3,布局R.layout.main中引用碎片 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"   
  4.     android:layout_width="match_parent"   
  5.     android:layout_height="match_parent"  
  6.     >  
  7.      
  8.      <fragment android:name="com.ql.app.FirstFragment"   
  9.             android:id="@+id/firstFragment"   
  10.             android:layout_weight="1"   
  11.             android:layout_width="0dp"   
  12.             android:layout_height="match_parent"   
  13.             />   
  14.     <fragment android:name="com.ql.app.SecondFragment"   
  15.             android:id="@+id/secondFragment"   
  16.             android:layout_weight="2"   
  17.             android:layout_width="0dp"   
  18.             android:layout_height="match_parent"   
  19.             />  
  20.               
  21.      <Button android:id="@+id/btn_1"  
  22.         android:layout_width="wrap_content"   
  23.         android:layout_height="wrap_content"  
  24.         android:text="隐藏"  
  25.      />   
  26.      <Button android:id="@+id/btn_2"  
  27.         android:layout_width="wrap_content"   
  28.         android:layout_height="wrap_content"  
  29.         android:text="隐藏"  
  30.      />   
  31. </LinearLayout>  

4,上图 

 

http://blog.csdn.net/nkmnkm/article/details/7256605  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值