Android应用开发中Fragment的静态加载与动态加载实例

Android应用开发中Fragment的静态加载与动态加载实例,第1张

概述1、Fragment的静态使用Fragment是作为Activity的UI的一部分,它内嵌在Activity中,多个Fragment可以把一个Activity分成多个部分,这在大屏幕手机或者平板电脑中会比较多的用到,这样就不用使用多个Activity来切换这

1、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,true)方法中的第一个参数R.layout.leftfragment是这个Fragment对应的布局文件ID,第二个参数container是要插入的目标Activity,第三个参数是决定这个Fragment是否依附于这个container.
leftFragment对应的布局文件:

 <?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:background="@androID:color/holo_orange_dark"   androID:orIEntation="vertical" >    <button androID:ID="@+ID/prevIoUs_button"     androID:layout_wIDth="fill_parent"     androID:layout_height="wrap_content"     androID:text="@string/prevIoUs_button" />    <button androID:ID="@+ID/next_button"     androID:layout_wIDth="fill_parent"     androID:layout_height="wrap_content"     androID:text="@string/next_button" />   <button androID:ID="@+ID/exit_button"     androID:layout_wIDth="fill_parent"     androID:layout_height="wrap_content"     androID:text="@string/exit_button" />  </linearLayout>

RightFragment类:和leftFragment类似

public class RightFragment extends Fragment {   @OverrIDe   public voID onCreate(Bundle savedInstanceState)   {     super.onCreate(savedInstanceState);     System.out.println("RightFragment onCreate");   }      @OverrIDe   public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState)   {     System.out.println("RightFragment onCreateVIEw");     return inflater.inflate(R.layout.rightfragment,true);  }     @OverrIDe   public voID onResume()  {    super.onResume();     System.out.println("RightFragment onResume");   }      @OverrIDe   public voID onPause()   {     super.onPause();     System.out.println("RightFragment onPause");  }      @OverrIDe   public voID onStop()   {     super.onStop();     System.out.println("RightFragment onStop");   } }

RightFragment对应的布局文件:

<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >    <TextVIEw androID:ID="@+ID/show_message"     androID:layout_wIDth="fill_parent"     androID:layout_height="fill_parent"     androID:background="@androID:color/holo_blue_dark"     androID:text="@string/show_message" />  </linearLayout>

最后是Activity的布局文件:

<?xml version="1.0" enCoding="utf-8"?>  <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:orIEntation="horizontal" >      <fragment  androID:ID="@+ID/left_fragment"      androID:name="com.sunflower.leftFragment"      androID:layout_wIDth="match_parent"      androID:layout_height="fill_parent"      androID:layout_weight="3" />      <fragment  androID:ID="@+ID/right_fragment"      androID:name="com.sunflower.RightFragment"      androID:layout_wIDth="match_parent"      androID:layout_height="fill_parent"      androID:layout_weight="1" />   </linearLayout> 

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

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

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

2、动态地使用Fragment

上面已经演示了最简单的使用Fragment的方式,下面分享一下如何动态的添加、更新、以及删除Fragment。

 首先是,MainActivity的布局文件activity_main.xml,该文件布局文件上面的顶部是一个TitleFragment,是一个静态声明的Fragment。

中间也是一个Fragment,但是这个Fragment是动态使用的。

最下面是四个按钮。用include标签包含外部的布局文件进来的。

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent" >  <fragment    androID:ID="@+ID/ID_fragment_Title"    androID:name="com.example.dynamicfragment.TitleFragment"    androID:layout_wIDth="fill_parent"    androID:layout_height="45dp" />  <include    androID:ID="@+ID/ID_ly_bottombar"    androID:layout_wIDth="fill_parent"    androID:layout_height="55dp"    androID:layout_alignParentBottom="true"    layout="@layout/bottombar" />  <FrameLayout    androID:ID="@+ID/ID_content"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:layout_above="@ID/ID_ly_bottombar"    androID:layout_below="@ID/ID_fragment_Title" /></relativeLayout>

然后是,MainActivity.java文件。也是我们这个demo当中最重要的代码文件,首先是将上面的布局文件通过setContentVIEw()加载进来.然后是通过setDefaultFragment();将默认的ContentFragment动态的加载进来。接下来就是通过我们在最下面防止的四个按钮可以随意的动态切换Fragment。这也是为什么Fragment会有如此火的原因吧~~~^^

public class MainActivity extends ActionBaractivity implements OnClickListener {  private Imagebutton mTabWeixin;  private Imagebutton mTabFrIEnd;  private Imagebutton mTabdiscover;  private Imagebutton mTabMe;  private ContentFragment mWeiXinFragment;  private FrIEndFragment mFrIEndFragment;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    requestwindowFeature(Window.FEATURE_NO_Title);    setContentVIEw(R.layout.activity_main);    initVIEw();  }  public voID initVIEw() {    // 初始化控件和声明事件    mTabWeixin = (Imagebutton) findVIEwByID(R.ID.weixin);    mTabFrIEnd = (Imagebutton) findVIEwByID(R.ID.frIEnd);    mTabWeixin.setonClickListener(this);    mTabFrIEnd.setonClickListener(this);    // 设置默认的Fragment    setDefaultFragment();  }  @Suppresslint("NewAPI")  private voID setDefaultFragment() {    FragmentManager manager = getFragmentManager();    FragmentTransaction transaction = manager.beginTransaction();    mWeiXinFragment = new ContentFragment();    transaction.replace(R.ID.ID_content,mWeiXinFragment);    transaction.commit();  }  @Suppresslint("NewAPI")  @OverrIDe  public voID onClick(VIEw v) {    FragmentManager fm = getFragmentManager();    // 开启Fragment事务    FragmentTransaction transaction = fm.beginTransaction();    switch (v.getID()) {    case R.ID.weixin:      if (mWeiXinFragment == null) {        mWeiXinFragment = new ContentFragment();      }      // 使用当前Fragment的布局替代ID_content的控件      transaction.replace(R.ID.ID_content,mWeiXinFragment);      break;    case R.ID.frIEnd:      if (mFrIEndFragment == null) {        mFrIEndFragment = new FrIEndFragment();      }      transaction.replace(R.ID.ID_content,mFrIEndFragment);      break;    }    // transaction.addToBackStack();    // 事务提交    transaction.commit();  }}

从上面的代码,我们可以看出,我们可以使用FragmentManager对Fragment进行动态的加载,这里使用的replace方法~~~下一节我们会详细的介绍FragmentManager的常用API。。。。^^

注:如果使用androID3.0一下的版本,需要引入v4的包,然后Activity继承FragmentActivity,然后通过getSupportFragmentManager()获得FragmentManager对象,不过还是建议把Menifest文件的uses-sdk的minSdkVersion和targetSdkVersion都改为11以上,这样就不必引入v4的包了。

代码的中间有俩个动态加载进来的Fragment,这个和静态使用Fragment的声明方式是一样的,写一个继承Fragment的类,然后设置相应的布局,由于时间的关系,我这里只写了俩个Fragment,现在把这俩个的代码页贴出来:

第一个Fragment和他相应的布局文件:

public class ContentFragment extends Fragment {  @OverrIDe   public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState)   {     return inflater.inflate(R.layout.fragment_content,false);   } }
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >    <TextVIEw     androID:layout_wIDth="fill_parent"     androID:layout_height="fill_parent"     androID:gravity="center"     androID:text="weixin"     androID:textSize="20sp"     androID:textStyle="bold" />  </linearLayout> 


第二个Fragment和他相应的布局文件:

public class FrIEndFragment extends Fragment {  @OverrIDe   public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState)   {     return inflater.inflate(R.layout.fragment_frIEnd,false);   } }


<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >    <TextVIEw     androID:layout_wIDth="fill_parent"     androID:layout_height="fill_parent"     androID:gravity="center"     androID:text="frIEnd"     androID:textSize="20sp"     androID:textStyle="bold" />  </linearLayout> 

好了,现在基本的代码都有了,我们把demo的运行图贴出来给大家分享一下(注:时间原因,没注意布局以及图片的美化,只是功能的实现),这是分别点击下面第一个和第二个按钮的效果图,从而实现了中间用一个Fragment动态的加载这俩个Fragment的显示。

ps:为了代码的简洁,就不添加按钮的点击变化什么的了,主要讲解功能了~~~

总结

以上是内存溢出为你收集整理的Android应用开发中Fragment的静态加载与动态加载实例全部内容,希望文章能够帮你解决Android应用开发中Fragment的静态加载与动态加载实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1142160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存