Android FragMent 的初相识(一)

FragMent 的初相识(一)

【1】什么是Fragment ?

Fragment 是在Android 3.0 以后才出现的功能。
Android 运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板或者超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。
中文意思为“碎片”,与Activity 相似,就是其中多了几个方法。
                               
  可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图

onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现。
Fragment 用来在一个Activity 的方法中描述一些行为和一部分的用户的界面。
使用多个Fragment 可以在一个单独的Activity 中建立多个UI面板。
也可以在多个Activity 中重用Fragment
注意:
一个Fragment 必须被嵌入到一个Activity 中,其生命周期直接受其所属的宿主Activity 的生命周期影响。
例如:当Activity 被暂停时,其中的所有Fragment也被暂停;
           当Activity 被销毁掉,其中的所属的Fragment 也被销毁; 
           当一个Activity 处于resumed(正在运行)状态时,可以单独地对每个Fragment 进行操作。

【2】Fragment  的两种创建方式

【2.1】Demo01 Fragment入门(静态的创建Fragment)

            (1)创建一个类继承于 Fragment ( import  android.app.Fragment;
                (2)重写onCreateView()方法,将Layout 对象转化为一个view 返回
            (3)编写xml 文件,定义出来fragment.xml 文件,在ui 界面布局中,添加<fargment> 控件,指定实现Fragment类的name 的值。
(1) Fragment_wx.java(无关是静态创建 还是 动态创建 都需要的这个方法)
   
   
  1. package cn.edu.aynu.fragment;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class Fragment_wx extends Fragment {
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  10. // [1]使用打气筒工具将布局文件转化为view ,并返回view
  11. View v = inflater.inflate(R.layout.fragment_wx, container,false);
  12. return v;
  13. }
  14. }
(2)activity_main.xml  (静态创建需要,动态创建不需要这个方法)
   
   
  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. android:orientation="horizontal" >
  6. <!-- name= 包名.类名 用于指定要添加的实例化的Fragment -->
  7. <fragment
  8. android:name="cn.edu.aynu.fragment.Fragment_wx"
  9. android:id="@+id/fragment_wx"
  10. android:layout_weight="1"
  11. android:layout_width="0dp"
  12. android:layout_height="match_parent"
  13. />
  14. <fragment
  15. android:name="cn.edu.aynu.fragment.Fragment_Contans"
  16. android:id="@+id/fragment_contans"
  17. android:layout_weight="1"
  18. android:layout_width="0dp"
  19. android:layout_height="match_parent"
  20. />
  21. </LinearLayout>
(3)fragment_wx.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. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="我是fragment_wx板块"
  10. />
  11. </LinearLayout>
效果:

【2.2】Demo02 动态添加(查看横竖屏切换)

 注意:多个Fragment 的操作的就像是 事务,要么一起成功,要么一起失败。
(1)创建类继承于 Fragment 重写其中的onCreateView() 方法。
(2)创建相关的fragment.xml 文件
(3)在MainActivity.java 中获取到Fragment 的管理者,开启事务,调用方法,提交事务
    
    
  1. package cn.edu.aynu.fragment;
  2. import android.app.Activity;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.WindowManager;
  9. public class MainActivity extends Activity {
  10. @SuppressWarnings("deprecation")
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. // 获取手机屏幕的宽和高
  16. WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  17. int width = wm.getDefaultDisplay().getWidth(); // 宽
  18. int height = wm.getDefaultDisplay().getHeight(); // 高
  19. // 【1】获取fargment 的管理者
  20. FragmentManager fragmentManager = this.getFragmentManager();
  21. // 【2】开启一个事务
  22. FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  23. if (width < height) {
  24. // 竖屏
  25. // 【3】
  26. beginTransaction.replace(android.R.id.content, new Fragment_wx());
  27. } else {
  28. // 横屏
  29. // 【3】
  30. beginTransaction.replace(android.R.id.content, new Fragment_Contans());
  31. }
  32. // 【4】提交事务
  33. beginTransaction.commit();
  34. }
  35. }
注意: 事务结束必须要进行提交
效果:
 
【2.3】仿微信的简单界面
   
   
  1. package cn.edu.aynu.fangweixin;
  2. import android.app.Activity;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13. public class MainActivity extends Activity implements OnClickListener {
  14. private FragmentManager fragmentManager;
  15. private Context context ;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. this.context = this ;
  21. //【1】获取一个Fragment 的管理者
  22. fragmentManager = this.getFragmentManager();
  23. // 找到我们关心的控件
  24. Button wx = (Button) findViewById(R.id.fragment_wx);
  25. Button friend = (Button) findViewById(R.id.fragment_friend);
  26. Button contans = (Button) findViewById(R.id.fragment_contans);
  27. Button me = (Button) findViewById(R.id.fragment_me);
  28. wx.setOnClickListener(this);
  29. friend.setOnClickListener(this);
  30. contans.setOnClickListener(this);
  31. me.setOnClickListener(this);
  32. }
  33. @Override
  34. public void onClick(View v) {
  35. // [1]开启事务
  36. FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  37. switch (v.getId()) {
  38. case R.id.fragment_wx:
  39. beginTransaction.replace(R.id.nihao, new Fragment_wx());
  40. break;
  41. case R.id.fragment_friend:
  42. beginTransaction.replace(R.id.nihao, new Fragment_friend());
  43. break;
  44. case R.id.fragment_contans:
  45. beginTransaction.replace(R.id.nihao, new Fragment_contans());
  46. break;
  47. case R.id.fragment_me:
  48. beginTransaction.replace(R.id.nihao, new Fragment_me());
  49. break;
  50. default:
  51. break;
  52. }
  53. // 关闭事务
  54. beginTransaction.commit();
  55. }
  56. }
    
    
  1. package cn.edu.aynu.fangweixin;
  2. import android.app.Fragment;
  3. import android.content.Context;
  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.Toast;
  11. public class Fragment_wx extends Fragment {
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. // [1]
  21. View v = inflater.inflate(R.layout.fragment_wx, null);
  22. Button button = (Button) v.findViewById(R.id.fragment_wx_button);
  23. button.setOnClickListener(new OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. // TODO Auto-generated method stub
  27. System.out.println("nihao");
  28. }
  29. });
  30. return v;
  31. }
  32. }
主布局
    
    
  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="cn.edu.aynu.fangweixin.MainActivity" >
  6. <LinearLayout
  7. android:id="@+id/nihao" <!-- 根据这个来找到布局的位置-->
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. >
  12. </LinearLayout>
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentBottom="true"
  17. android:orientation="horizontal"
  18. android:weightSum="4" >
  19. <Button
  20. android:id="@+id/fragment_wx"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:text="微信" />
  25. <Button
  26. android:id="@+id/fragment_friend"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:text="朋友" />
  31. <Button
  32. android:id="@+id/fragment_contans"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_weight="1"
  36. android:text="联系人" />
  37. <Button
  38. android:id="@+id/fragment_me"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_weight="1"
  42. android:text="我" />
  43. </LinearLayout>
  44. </RelativeLayout>

效果
 

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值