zoukankan      html  css  js  c++  java
  • Android之Fragment静态实现实例

         Fragment是作为Activity的UI的一部分,它内嵌在Activity中,多个Fragment可以把一个Activity分成多个部分,这在大屏幕手机或者平板电脑中会比较多的用到,这样就不用使用多个Activity来切换这么麻烦了。当然Fragment也可以不显示,只在后台处理一些数据,这篇文章中就暂时不谈到这个。以下来看怎么静态地在Activity的布局文件中添加Fragment.

      自定义的Fragment通常要继承Fragment这个类,也有一些特殊的是继承ListFragment,DialogFragment等。继承Fragment类通常要实现三个方法:onCreate(), onCreateView(), onPause();

      我在Activity中定义了两个Fragment,一个是放在左边的LeftFragment,一个是放在右边的RightFragment.以下是代码:首先我们要实现自己的Fragment类

      LeftFragment类

    public class LeftFragment extends Fragment
    {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    System.out.println("LeftFragment onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    System.out.println("LeftFragment onCreateView");
    // 第一个参数是这个Fragment将要显示的界面布局,第二个参数是这个Fragment所属的Activity,第三个参数是决定此fragment是否附属于Activity
    return inflater.inflate(R.layout.leftfragment, container, true);
    }

    @Override
    public void onResume()
    {
    super.onResume();
    System.out.println("LeftFragment onResume");
    }

    @Override
    public void onPause()
    {
    super.onPause();
    System.out.println("LeftFragment onPause");
    }


    @Override
    public void onStop()
    {
    super.onStop();
    System.out.println("LeftFragment onStop");
    }
    }

    这里实现了几种回调函数,主要是为了看清Activity和Fragment生命周期之间的关系.其中onCreateView()方法是将本Fragment对应的布局返回给Activity的布局,让Activity进行加载. inflater.inflate(R.layout.leftfragment, container, true)方法中的第一个参数R.layout.leftfragment是这个Fragment对应的布局文件ID, 第二个参数container是要插入的目标Activity, 第三个参数是决定这个Fragment是否依附于这个container.

      LeftFragment对应的布局文件:

     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:background="@android:color/holo_orange_dark"
    6 android:orientation="vertical" >
    7
    8 <Button
    9 android:id="@+id/previous_button"
    10 android:layout_width="fill_parent"
    11 android:layout_height="wrap_content"
    12 android:text="@string/previous_button" />
    13
    14 <Button
    15 android:id="@+id/next_button"
    16 android:layout_width="fill_parent"
    17 android:layout_height="wrap_content"
    18 android:text="@string/next_button" />
    19
    20 <Button
    21 android:id="@+id/exit_button"
    22 android:layout_width="fill_parent"
    23 android:layout_height="wrap_content"
    24 android:text="@string/exit_button" />
    25
    26 </LinearLayout>

      RightFragment类:和LeftFragment类似

     1 public class RightFragment extends Fragment
    2 {
    3 @Override
    4 public void onCreate(Bundle savedInstanceState)
    5 {
    6 super.onCreate(savedInstanceState);
    7 System.out.println("RightFragment onCreate");
    8 }
    9
    10 @Override
    11 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    12 {
    13 System.out.println("RightFragment onCreateView");
    14 return inflater.inflate(R.layout.rightfragment, container, true);
    15 }
    16
    17 @Override
    18 public void onResume()
    19 {
    20 super.onResume();
    21 System.out.println("RightFragment onResume");
    22 }
    23
    24 @Override
    25 public void onPause()
    26 {
    27 super.onPause();
    28 System.out.println("RightFragment onPause");
    29 }
    30
    31 @Override
    32 public void onStop()
    33 {
    34 super.onStop();
    35 System.out.println("RightFragment onStop");
    36 }
    37 }

      RightFragment对应的布局文件:

     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/show_message"
    9 android:layout_width="fill_parent"
    10 android:layout_height="fill_parent"
    11 android:background="@android:color/holo_blue_dark"
    12 android:text="@string/show_message" />
    13
    14 </LinearLayout>

      最后是Activity的布局文件:

     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:layout_width="fill_parent"
    4 android:layout_height="fill_parent"
    5 android:orientation="horizontal" >
    6
    7 <fragment
    8 android:id="@+id/left_fragment"
    9 android:name="com.sunflower.LeftFragment"
    10 android:layout_width="match_parent"
    11 android:layout_height="fill_parent"
    12 android:layout_weight="3" />
    13
    14 <fragment
    15 android:id="@+id/right_fragment"
    16 android:name="com.sunflower.RightFragment"
    17 android:layout_width="match_parent"
    18 android:layout_height="fill_parent"
    19 android:layout_weight="1" />
    20
    21 </LinearLayout>

    在Activity中的布局文件中加入Fragment标签,其中android:name属性对应的就是自定义Fragment类的全名,系统会根据这个调用指定的Fragment的onCreateView()方法来得到这个Fragment的布局,然后加入Activity中. onCreateView()方法中的Container参数就是这时候传递过去的。

    看看显示结果:

    打开程序时生命周期显示:

    按返回键时生命周期显示:



     

  • 相关阅读:
    看过的代码
    ScipyLectures-simple学习笔记
    机器学习1一个月2017/11/24-2017/12/24
    机器学习课程 matlab 练习
    win7 win8 快捷键直接调出任务管理器
    java 关于getProperty()方法中反斜杠问题
    把myeclipse中html/jsp文件的视图调到只看代码
    Win7 server2008 共享文件夹 不输入网络密码
    别用visual editor了,用WindowBuilder
    visual editor ve1.5下载
  • 原文地址:https://www.cnblogs.com/hanyuan/p/fragment_in_layout.html
Copyright © 2011-2022 走看看