Android Fragment详解

短命女 2023-07-05 11:42 34阅读 0赞

一、Fragment概述

1、定义:Fragment,中文意思:碎片,与activity相似。在一个activity中描述一些行为或一部门用户界面

2、Fragment的生命周期:一个Fragment必须嵌入到一个Activity中,其生命周期受该activity的生命周期影响。
在这里插入图片描述
返回栈:一组Activity的集合,按先进后出的方式放置一系列的Activity,在进行Fragment转换时,可把Fragment放入返回栈中,用户就可以使用返回操作了。

3、创建Fragment
创建一个Fragment的子类,即extends Fragment或继承自已存在的Fragment的子类。

ListFragment.java文件

  1. public class ListFragment extends Fragment{
  2. @Nullable
  3. //为Fragment加载所使用的的布局文件
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  6. View view=inflater.inflate(R.layout.fragment_list,container,false);
  7. return view;
  8. }
  9. }

fragment_list.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" 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:text="Fragement内容"/>
  9. </LinearLayout>

二、在Activity中添加Fragment

两种方法:

  1. 直接在布局文件中添加Fragment。
  2. 在Activity运行时动态添加Fragment。
1、直接添加

代码列表:
在这里插入图片描述
效果:
在这里插入图片描述
ListFragment.java与DetailFragment.java文件

  1. //List:
  2. public class ListFragment extends Fragment{
  3. @Nullable
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  6. View view=inflater.inflate(R.layout.fragment_list,container,false);
  7. return view;
  8. }
  9. }
  10. //Detail
  11. public class DetailFragment extends Fragment{
  12. @Nullable
  13. @Override
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  15. View view=inflater.inflate(R.layout.fragment_detail,container,false);
  16. return view;
  17. }
  18. }

List和Detail的xml文件

  1. //List
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical" android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="List_fragment"/>
  10. </LinearLayout>
  11. //Detail
  12. <?xml version="1.0" encoding="utf-8"?>
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  14. android:orientation="vertical" android:layout_width="match_parent"
  15. android:layout_height="match_parent">
  16. <TextView
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="Detail_fragment"/>
  20. </LinearLayout>

activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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:orientation="horizontal"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="com.mingrisoft.demo.MainActivity">
  12. <!--添加两个fragment,name为fragment的来源。 -->
  13. <fragment
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:name="com.mingrisoft.demo.ListFragment"
  17. android:id="@+id/list"
  18. android:layout_weight="1"></fragment>
  19. <fragment
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:name="com.mingrisoft.demo.DetailFragment"
  23. android:id="@+id/detail"
  24. android:layout_weight="2"></fragment>
  25. </LinearLayout>
2、动态添加

Detail和List的java,xml文件不变。
activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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:orientation="vertical"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="com.mingrisoft.demo.MainActivity">
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Activity"/>
  16. <FrameLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/fl"></FrameLayout>
  20. </LinearLayout>

Mainactivity.java文件

  1. package com.mingrisoft.demo;
  2. import android.app.Fragment;
  3. import android.app.FragmentTransaction;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. public class MainActivity extends ActionBarActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. //实例化DetailFragment对象
  12. DetailFragment detailFragment=new DetailFragment();
  13. //获取FragmentTransaction实例
  14. FragmentTransaction ft=getFragmentManager().beginTransaction();
  15. //第一个参数用于指定Fragment放在哪个容器中,通常为ViewGroup对象,可用android提供的
  16. //content,第二个参数要添加的对象。
  17. ft.add(R.id.fl,detailFragment);
  18. //调用commit来提交事务
  19. ft.commit();
  20. }
  21. }

实例:实现模拟微信界面中的Tab标签切换功能。

代码列表:
在这里插入图片描述
效果:
在这里插入图片描述
在这里插入图片描述
4个Fragment的内容和上相似。
四个Fragment.xml文件(内容相似,只列出一个)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content" android:layout_height="match_parent">
  4. <ImageView
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:src="@drawable/we4"
  8. android:scaleType="fitXY"/>
  9. </RelativeLayout>

activity_main.xml文件

  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. tools:context="com.mingrisoft.fragment_wechat.MainActivity">
  7. <!-- 放fragment的地方,默认wechat-->
  8. <fragment
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:name="com.mingrisoft.fragment_wechat.Wechat_Fragment"
  12. android:id="@+id/fragment"
  13. />
  14. <!-- 下方的4个图标 -->
  15. <LinearLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="50dp"
  18. android:layout_alignParentBottom="true"
  19. android:orientation="horizontal">
  20. <ImageView
  21. android:id="@+id/image1"
  22. android:layout_width="0dp"
  23. android:layout_height="50dp"
  24. android:layout_weight="1"
  25. android:src="@drawable/ww1"/>
  26. <ImageView
  27. android:id="@+id/image2"
  28. android:layout_width="0dp"
  29. android:layout_height="50dp"
  30. android:layout_weight="1"
  31. android:src="@drawable/ww2"/>
  32. <ImageView
  33. android:id="@+id/image3"
  34. android:layout_width="0dp"
  35. android:layout_height="50dp"
  36. android:layout_weight="1"
  37. android:src="@drawable/ww3"/>
  38. <ImageView
  39. android:id="@+id/image4"
  40. android:layout_width="0dp"
  41. android:layout_height="50dp"
  42. android:layout_weight="1"
  43. android:src="@drawable/ww4"/>
  44. </LinearLayout>
  45. </RelativeLayout>

MainActivity.java文件

  1. package com.mingrisoft.fragment_wechat;
  2. import android.app.Fragment;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.ImageView;
  9. public class MainActivity extends ActionBarActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. ImageView image1= (ImageView) findViewById(R.id.image1);
  15. ImageView image2= (ImageView) findViewById(R.id.image2);
  16. ImageView image3= (ImageView) findViewById(R.id.image3);
  17. ImageView image4= (ImageView) findViewById(R.id.image4);
  18. image1.setOnClickListener(l);
  19. image2.setOnClickListener(l);
  20. image3.setOnClickListener(l);
  21. image4.setOnClickListener(l);
  22. }
  23. //由于每一个ImgaeView的单击事件监听器都是相似的,所以我们创建一个单击事件监听器对象。
  24. View.OnClickListener l=new View.OnClickListener() {
  25. @Override
  26. public void onClick(View view) {
  27. //FragMent的管理器
  28. FragmentManager fm=getFragmentManager();
  29. //开启一个事务
  30. FragmentTransaction ft=fm.beginTransaction();
  31. //创建一个Fragment的空对象
  32. Fragment f=null;
  33. //判断点击的是哪个图标
  34. switch (view.getId())
  35. {
  36. case R.id.image1:
  37. f=new Wechat_Fragment();
  38. break;
  39. case R.id.image2:
  40. f=new Talk_fragment();
  41. break;
  42. case R.id.image3:
  43. f=new Find_fragment();
  44. break;
  45. case R.id.image4:
  46. f=new Me_fragment();
  47. break;
  48. default:
  49. break;
  50. }
  51. //将f内容显示在R.id.fragment中
  52. ft.replace(R.id.fragment,f);
  53. 调用commit来提交事务
  54. ft.commit();
  55. }
  56. };
  57. }

发表评论

表情:
评论列表 (有 0 条评论,34人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android Fragment

    fragment,简称碎片,是Android 3.0(API 11)提出的,为了兼容低版本,support-v4库中也开发了一套Fragment API,最低兼容Android