Android app开发中的Fragment入门学习教程

 更新时间:2016年02月25日 16:33:24   作者:yanfeichening  
这篇文章主要介绍了Android app开发中的Fragment入门学习教程,包括Fragment的创建和XML布局文件中的Fragment定义等,需要的朋友可以参考下

在Android3.0上开始引入了一个新概念叫Fragment。它有自己的布局文件,可以作为组件排布,也可以相互组合去实现不同的布局显示。使用Fragment可以重复利用代码,并且可以满足不同设备尺寸的需求。Fragment不能单独存在,只能存在于Activity中,而一个Activity可以拥有多个Fragment。很重要的一点是,Fragment可以和Activity中的其它组件一起使用,无需重写所有Activity的接口。所以使用Fragment就可以这样来完成上例中“主界面—详细界面”的APP需求。

在手机上是这样显示的:

2016225161854439.png (300×192)

而在平板上是这样的:

2016225161915086.png (300×177)

在一个小屏幕的设备上,一个activity通常占据了整个屏幕,同时显示各种UI视图组件。Activity实际上就是视图的容器。然后,当一个activity被显示在一个大屏幕的设备上,例如平板电脑,总会显得有些不适应。因为屏幕太大了,activity中的所有UI组件要充满整个屏幕,这样一来,视图的层次结构就很复杂了。一个更好的办法是使用一种“轻量级”的activity,每个“轻量级”activity包含自己的视图,互不干扰。在运行期间,根据屏幕的方向和尺寸,一个activity可以包含一个或多个“轻量级”activity。在Android3.0以上的版本,这种“轻量级”的activity叫做Fragment.

怎么创建一个Fragment

现在我们了解了Fragment的生命周期了,接着我们就需要知道怎么创建一个Fragment并绑定到Activity中,第一件要做的事就是继承android.app.Fragment来写一个Fragment,假设我们的Fragment叫做Fragment1,创建和定义如下:

public class Fragment1 extends Fragment {
...
}

就像我们上面说的,Fragment只能存在于Activity中,所以我们必须要在某处定义它,有两种方式:
- 直接在xml布局文件中定义;
- 在xml布局文件中定义一个占位符,然后动态地在Activity中操作Fragment;

我们定义Fragment的方式会影响它的生命周期,因为在上述第一种情况下onInflate方法会被调用,而第二种情况下它的生命周期是从onAttach方法开始的。

如果我们在XML文件中定义Fragment的话,我们需要:

<fragment android:id="@+id/f1"
            class="com.survivingwithandroid.fragment.Fragment1"
       android:layout_width="match_parent"
       android:layout_height="20dp"/>

然而如果我们在XML中用占位符的话,需要再做一些工作。

布局框架和Fragment

如果我们在XML布局文件中定义Fragment的话,就不能自由、动态修改Fragment了,还有别的方法可以让我们可以更灵活地操作:使用时需要在XML文件中定义:

<FrameLayout android:id="@+id/fl1"
       android:layout_width="match_parent"
       android:layout_height="200dp"/>

在Activity里面还需要做一点工作,因为我们必须手动初始化Fragment,然后把它“插入”到FrameLayout中。

public class MainActivity extends Activity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  Fragment2 f2 = new Fragment2();
  FragmentTransaction ft = getFragmentManager().beginTransaction();
  ft.replace(R.id.fl1, f2);
  ft.commit();
}


例子

可以把Fragment想象成Activity的另外一种形式。你创建fragments去包含UI组件,就像创建activities那样。但是,Fragment总是被嵌在Activity中。

下面来通过一个例子看一下流程:

1.创建一个名为Fragments的工程。
2.在res/layout文件夹下,新建一个叫fragment1.xml的文件。

<?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:background="#00FF00" 
  android:orientation="vertical" > 
 
  <TextView 
    android:id="@+id/lblFragment1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is fragment #1" 
    android:textColor="#000000" 
    android:textSize="25sp" /> 
 
</LinearLayout> 

3.在res/layout文件夹下,新建一个叫fragment2.xml的文件。

<?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:background="#FFFE00" 
  android:orientation="vertical" > 
 
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is fragment #2" 
    android:textColor="#000000" 
    android:textSize="25sp" /> 
 
  <Button 
    android:id="@+id/btnGetText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="onClick" 
    android:text="Get text in Fragment #1" 
    android:textColor="#000000" /> 
 
</LinearLayout> 

4.main.xml中的代码。

<?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/fragment1" 
    android:name="net.learn2develop.Fragments.Fragment1" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 
 
  <fragment 
    android:id="@+id/fragment2" 
    android:name="net.learn2develop.Fragments.Fragment2" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 
 
</LinearLayout> 

5.新建两个类:Fragment1.java和Fragment2.java。
6.Fragment1.java中的代码。

package net.learn2develop.Fragments; 
 
import android.app.Activity; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
public class Fragment1 extends Fragment { 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
 
    Log.d("Fragment 1", "onCreateView"); 
 
    // ---Inflate the layout for this fragment--- 
    return inflater.inflate(R.layout.fragment1, container, false); 
  } 
} 

7.Fragment2.java中的代码。

package net.learn2develop.Fragments; 
 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class Fragment2 extends Fragment { 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
    // ---Inflate the layout for this fragment--- 
    return inflater.inflate(R.layout.fragment2, container, false); 
  } 
} 

相关文章

  • Android性能优化之图片大小,尺寸压缩综合解决方案

    Android性能优化之图片大小,尺寸压缩综合解决方案

    随着Android手机的越来越先进,给我们开发者而言传递的图片也是越来越大,这个时候我们可以对一些没有必要原图展示的图片进行压缩,这篇文章主要给大家介绍了关于Android性能优化之图片大小,尺寸压缩的综合解决方案,需要的朋友可以参考下
    2022-04-04
  • Android逆向入门之常见Davlik字节码解析

    Android逆向入门之常见Davlik字节码解析

    Dalvik是Google公司自己设计用于Android平台的虚拟机。Dalvik虚拟机是Google等厂商合作开发的Android移动设备平台的核心组成部分之一,本篇文章我们来详细解释常见Davlik字节码
    2021-11-11
  • Flutter路由之fluro的配置及跳转

    Flutter路由之fluro的配置及跳转

    本文主要介绍了Flutter路由之fluro的配置及跳转,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 详解Android 在 ViewPager 中使用 Fragment 的懒加载

    详解Android 在 ViewPager 中使用 Fragment 的懒加载

    本篇文章主要介绍了Android 在 ViewPager 中使用 Fragment 的懒加载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • android自定义控件ImageView实现圆形图片

    android自定义控件ImageView实现圆形图片

    这篇文章主要为大家详细介绍了android自定义控件ImageView实现圆形图片,适用于用户头像,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android实现图片轮播列表

    Android实现图片轮播列表

    这篇文章主要为大家详细介绍了Android实现图片轮播列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Android Jetpack 组件LiveData源码解析

    Android Jetpack 组件LiveData源码解析

    这篇文章主要为大家介绍了Android Jetpack 组件LiveData源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android开发apk反编译和二次打包教程

    Android开发apk反编译和二次打包教程

    反编译不是让各位开发者去对一个应用破解搞重装什么的,主要目的是为了促进开发者学习,借鉴好的代码,提升自我开发水平。下面我们就来研究下如何进行APK反编译以及二次打包
    2016-04-04
  • Android面试笔记之常问的Context

    Android面试笔记之常问的Context

    Android技术面试确实常常被问到Context,大概问题就是说说你对Context的理解吧,当时脑袋里浮现了是原来看到的文章片段乱说一通,这样还是不行的。平时还是多积累知识,深刻理解Context,在项目开发过程中也能避免一些陷入坑中。下面就来看看个人的一些总结吧。
    2016-12-12
  • Android仿制淘宝滚动图文条的示例代码

    Android仿制淘宝滚动图文条的示例代码

    这篇文章主要介绍了Android仿制淘宝滚动图文条的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08

最新评论