Android碎片 -- Fragment

 一, Fragment是什么?
是Android 3.0后出现,  API 11以上, 是Activity的一部分
Fragment 可以显示内容, 可以和用户交换
Fragment 的作用:  提高代码重用性  和 用户体验 ,可以对Activity的组件进行分组和模块话的管理
Fragment 有自己的生命周期和接收处理用户的事件  , 可以添加  移除   替换
注意:
1, Fragment 必须要显示在Activity中
2, 一个Activity可以包含多个Fragment ; 一个Fragment 可以被多个Actvity使用

   二, 如何创建Fragment?
1, 定义一个类, 继承Fragment <app>
2, 重写父类的生命周期方法: onCreateView()
   三, Fragment的显示方式?
1, 静态方式显示(固定内容 不需要接收数据)
1.1   在Activity 要显示的布局页面中通过标签引入<fragment/>
1.2   在标签中必须要指定id  , 用来标识Fragment的唯一性
1.3   在标签中必须要指定name, 指定要显示的是那个Fragment
2, 动态方式显示(传值)
2.1    在要显示的页面中, 通过布局容器占位
2.2    在Activity中
1, 得到Fragment的管理器对象
2, 通过管理器对象  开启Fragment的事务处理
3, 实例化要显示的Fragment
4, 动态显示Fragment
5, 提交事务
方法:  
替换  replace
移除  remove
添加  add 
隐藏  hide
显示  show
transaction.addToBackStack(null);//将当前的Fragment加入到回退栈中

  四, Activity向Fragment传值
1, Activity中
Bundle bundle = new Bundle();
bundle.putString(key,value);
fragment.setArguments(bundle);
2, Fragment中
Bundle bundle = getArguments();
String key = bundle.getString(key);
   五, Fragment向Activity传值(接口回调)
1, Fragment中
public void getEditText(CallBack callBack)
{
String msg = et.getText().toString().trim();

callBack.sendResult(msg);

}
//回调接口
public interface CallBack
{
public void sendResult(String result);
}

2, Activity中
fragment.getEditText(new CallBack() {
@Override
public void sendResult(String result) {
tv.setText(result);
}
});


   六, Fragment向Fragment传值
         1, LeftFragment  :
//传值
Bundle args = new Bundle();
args.putString("fileName", data.get(position));
fragment.setArguments(args);
2, RightFragment:
//取值
   七, Fragment的生命周期  11个

1, 初始化阶段   4个
onAttach()         与归属Activity建立连接
onCreate()  初始化Fragment
onCreateView() 初始化Fragment中显示的UI布局
onActivityCreatde()     归属Activity的onCreate()方法执行完成
2, 显示/隐藏   4个
onStart()  显示Fragment
onResume()  获得用户焦点
onPause()  失去用户焦点
onStop()  关闭Fragment
3, 销毁阶段    3个
onDestroyView() 销毁Fragment显示的UI布局
onDestroy()  销毁Fragment
onDetach()  与归属Activity断开连接
    静态方式:
显示:  F:onAttach -- F:onCreate -- F:onCreateView -- M:onCreate -- F:onActivityCreated  -- M:onStar -- F:onStrar -- M:onResume -- F:onResume
退出:  F:onPause  -- M:onPause -- F:onStop -- M:onStop -- F:onDestroyView  -- F:onDestroy -- F:ondetach -- M:onDestroy
   动态方式:
显示: M:onCrate -- F:onAttach -- F:onCreate -- F:onCreateView --F:onActivityCreated  -- M:onStar -- F:onStrar -- M:onResume -- F:onResume

退出:  F:onPause  -- M:onPause -- F:onStop -- M:onStop -- F:onDestroyView  -- F:onDestroy -- F:ondetach -- M:onDestroy

静态添加Fragment的方法实例:

[html]  view plain  copy
 print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <fragment  
  8.         android:id="@+id/left"  
  9.         android:name="com.example.day44fragment.Fragmentleft"  
  10.         android:layout_width="0dp"  
  11.         android:layout_height="match_parent"  
  12.         android:layout_weight="1" />  
  13.   
  14.     <fragment  
  15.         android:id="@+id/right"  
  16.         android:name="com.example.day44fragment.Fragmentright"  
  17.         android:layout_width="0dp"  
  18.         android:layout_height="match_parent"  
  19.         android:layout_weight="2" />  
  20.   
  21. </LinearLayout>  
[java]  view plain  copy
 print ?
  1. public class Fragmentleft extends Fragment {  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         View v = inflater.inflate(R.layout.left, null);  
  7.         return v;  
  8.     }  
  9. }  

[java]  view plain  copy
 print ?
  1. public class Fragmentright extends Fragment {  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         View v = inflater.inflate(R.layout.right, null);  
  6.         return v;  
  7.     }  
  8. }  

动态添加Fragment实例:

[java]  view plain  copy
 print ?
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.     }  
  8.   
  9.     public void add(View v) {  
  10.         // 得到fragment管理器对象  
  11.         FragmentManager manager = getFragmentManager();  
  12.         // 通过管理器打开fragment实物  
  13.         FragmentTransaction transaction = manager.beginTransaction();  
  14.         // 实例化显示fragment  
  15.         HelloFragment helloFragment = new HelloFragment();  
  16.         // 动态显示fragment  
  17.         // transaction.add(R.id.layout_id, helloFragment);  
  18.         transaction.replace(R.id.layout_id, helloFragment);  
  19.         // 提交事务  
  20.         transaction.commit();  
  21.   
  22.     }  
  23. }  
[html]  view plain  copy
 print ?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button   
  8.         android:id="@+id/button_id"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="添加Fragment"  
  12.         android:onClick="add"/>  
  13.     <!-- 布局容器的占位 -->  
  14.   
  15.     <LinearLayout  
  16.         android:layout_below="@id/button_id"  
  17.         android:layout_width="wrap_content"  
  18.         android:orientation="horizontal"  
  19.         android:id="@+id/layout_id"  
  20.         android:layout_height="wrap_content" />  
  21.   
  22. </RelativeLayout>  

[java]  view plain  copy
 print ?
  1. public class HelloFragment extends Fragment {  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         View v = inflater.inflate(R.layout.hello, null);  
  7.         return v;  
  8.     }  
  9. }  

[html]  view plain  copy
 print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.     <TextView   
  6.         android:layout_width="match_parent"  
  7.         android:layout_height="match_parent"  
  8.         android:id="@+id/tv"  
  9.         android:textSize="26sp"  
  10.         android:text="我是动态加载的fragment"/>  
  11.   
  12. </LinearLayout>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值