Android 基础-Fragment的使用

  初学Android时,我们都知道Activity,也知道Activity是怎么使用的。Activity的含义就是活动,一个界面,简而言之,放在App应用上来说,一个Activity就相当于是App中的一个界面。
  但是今天我们学的是Fragment,可能初学Android的,可能不知道Fragment是什么,但是不急,我们在这里会详细的讲解。

1.Fragment是什么?

  Fragment翻译过来就是碎片,片段,其实它是一种可以嵌入活动(Activity)的UI片段,它能让程序使用屏幕资源,使得一个屏幕充分的展现内容。Fragment是从Android 3.0(API 11)加入的,主要是为了给大屏幕(如平板电脑)上更加动态和灵活的 UI 设计提供支持。由于平板电脑的屏幕比手机屏幕大得多,因此可用于组合和交换 UI 组件的空间更大。利用片段实现此类设计时,您无需管理对视图层次结构的复杂更改。 通过将 Activity 布局分成片段,您可以在运行时修改 Activity 的外观,并在由 Activity 管理的返回栈中保留这些更改。
  例如:
  一个浏览新闻的界面,才开始我们使用一个RecyclerView来展现每一个新闻的大概信息。如图,在手机上显示:



  因为手机上的屏幕有限,所以,必须分为两个界面(Activity)来实现。但是平板上的情况就不一样:



  因为平板的屏幕比较大,所以为了节约屏幕的资源,我们尽可能在一个界面展现的我们内容,所以,此时我们可以使用新加入的Fragment来帮助我们设计布局。在平板上,左边的新闻列表是一个Fragment,右边的内容部分也是Fragment。有人想问,为什么不用一个Activity来实现这些布局,首先一个Activity有那么的控件,设计起来非常的麻烦,其次,在管理起来也非常的麻烦,一个布局上一个View的位置变换会影响到别的View的位置。所以我们使用Fragment来管理不同的布局,每个Fragmen只关注自己的布局,而Activity只关注的是每一个Fragment在它的布局上怎么摆放。

2.Fragment的使用

  我们可以将Fragment看成一个微型的Activity,有些用法跟Activity是一样的。比如,都是自己的布局文件,都有自己生命周期;但是具体的用法还是有一定的区别的,比如Fragment必须寄宿在Activity里面,不能单独使用,什么意思?就是Fragment不能像Activity那样,直接使用,直接的显示一个界面,而必须放在Activity里面使用,这时候我们将Fragment当成一个普通的View。
  那么,Fragment的具体是怎么使用的呢?Fragment有两种使用方式:静态使用和动态使用。我们来看看怎么使用的。

(1).静态使用

  我们先来看看一个案例



  整个界面,由三个Fragment来构成的,都是用Fragment的静态绑定实现的。

A.Fragment的布局文件

  这部分非常的简单,不在详细的解释,但是需要注意的是,一定要注意每一个Fragment里面的控件的layout_width 和 laout_height。

TitleFragment的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#00BFFF">

    <com.kingja.magicmirror.MagicMirrorView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/head"
        app:mirrorShape="circle" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="我是title"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:text="待定"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />
</RelativeLayout>

  这里补充一下,这里为了圆角ImageView的效果,导入一个依赖:compile 'com.kingja.magicmirror:magicmirror:1.2.0',只需要将这一句加入app中的build.grade中就行了。其次,一定要注意:这里的Layout的高度是60dp,不是march_parent。

ContentFragment的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#63B8FF">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:text="1"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:text="2"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:text="3"
        android:textSize="20sp" />
</RelativeLayout>

B.分别实现三个Fragment

  我将三个Fragment分别命名为TitleFragment、ContenFragment、TabFragment,他们都继承于Fragment。这里是TitleFragment的实现,其他的Fragment实现方式也是类似的

public class TitleFragment extends Fragment {
    /**
     * fragment第一次的创建会调用这个方法
     *
     * @param inflater           布局加载器,用来将xml文件转换成为view对象
     * @param container          viewGroup,我们可以这样理解,fragment需要加入Activity,因此fragment的view需要加入到Activity的ViewGroup,这个ViewGroup就是Activity的ViewGroup
     * @param savedInstanceState 用来进行一些状态恢复的操作
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_title, null);
    }
}

C.在MainActivity(Fragment需要加入到的Activity)的布局文件声明

  定义好了Fragment,我们必须在Activity的布局文件使用fragment标签的android:name来声明你定义好的fragment,记得一定跟完整的路径名:包名 + 类名。

<?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">

    <fragment
        android:id="@+id/id_fragmentTitle"
        android:name="com.example.pby.android_fragmentdemo.fragment.TitleFragment"
        android:layout_width="match_parent"
        android:layout_height="60dp"></fragment>

    <fragment
        android:id="@+id/id_fragmentContent"
        android:name="com.example.pby.android_fragmentdemo.fragment.ContentFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></fragment>

    <fragment
        android:id="@+id/id_fragmentTab"
        android:name="com.example.pby.android_fragmentdemo.fragment.TabFragment"
        android:layout_width="match_parent"
        android:layout_height="60dp"></fragment>
</LinearLayout>

  这里还需要注意的是:我将ContentFragment的layout_height设置为0dp,并且将layout_weight = 1。这个是为了让Content布满屏幕,但是又不会将底部的TabFragment挤出屏幕。
此时,我们不需要改变MainActivity中的任何代码,就可以看到效果了,但是你会发现有一个问题:这个App有一个系统的标题栏,如果你的Activity继承于Activity就在super.onCreate(savedInstanceState)和setContentView(R.layout.activity_main)添加requestWindowFeature(Window.FEATURE_NO_TITLE),如果是继承于AppCompatActivity的话,就写supportRequestWindowFeature(Window.FEATURE_NO_TITLE)。这样就没有系统的标题栏了。

这里还需要注意的是:Fragment在两个包,一个是android.app.Fragment,一个是android.support.v4.app.Fragment。我先来解释这两个Fragment的区别:不带v4的Fragment只能在Android 3.0以上的设备使用,带v4兼容Android 3.0以下的设备。我们在使用Fragment的时候,需要注意的是:我们使用的Fragment必须同一个包下,要么全部是不带v4的Fragment,要么全部是v4的Fragment

(2).动态使用

  学了静态使用,感觉没有卵用,或者说,操作呢?我们想要Fragment加载不同的内容,又怎么搞呢?首先,在这里,我提示一句:静态绑定,通常用于布局相同,但是布局内容不一样的情况下,比如:平板看看新闻那个例子,右边的题目和内容的布局是不会变的,变得是里面的内容,所以我们只需要使用静态绑定来使用Fragment来加载不同的内容就行了。
  如果我们想要在相同的地方加载不同内容,并且布局又是不一样的,那么怎么办呢?
  比如,某讯的某聊天软件,下面的Tab分为三个:消息、联系人、动态。上面的Tiltle只是一些内容在变换,中间的不仅是布局大变,而且内容又不是一样的,这种功能使用静态绑定来实现似乎非常的困难。所以引出我们今天要讲的动态绑定。

A.基本定义

  Fragment和Fragment的布局文件的定义,跟静态绑定的情况相差不多(这里ConetnFragment我定义了三个,因为要像某软件一样,能够切换中间的内容,这三个Fragment就是用来切换的)!最大差别是:在Activity的声明Fragment不一样!

B.Activity布局文件中的改变

  我将ContentFragment的静态改变为动态了,其中将以前的fragment改变成为了FrameLayout,具体看代码:

<?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">

    <fragment
        android:id="@+id/id_fragmentTitle"
        android:name="com.example.pby.android_fragmentdemo.fragment.TitleFragment"
        android:layout_width="match_parent"
        android:layout_height="60dp"></fragment>

    <FrameLayout
        android:id="@+id/id_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>

    <fragment
        android:id="@+id/id_fragmentTab"
        android:name="com.example.pby.android_fragmentdemo.fragment.TabFragment"
        android:layout_width="match_parent"
        android:layout_height="60dp"></fragment>
</LinearLayout>

C.Activity中实现动态加载和切换Fragment的功能

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //底部TabFragment中的三个TextView,主要是实现点击事件来实现切换Fragment的功能
    private TextView mTextView1 = null;
    private TextView mTextView2 = null;
    private TextView mTextView3 = null;

    //在ContentFragment部分需要切换的Fragment
    private Fragment contentFragment1 = null;
    private Fragment contentFragment2 = null;
    private Fragment contentFragment3 = null;
    //FragmentManager Fragment的任务管理器
    private FragmentManager fragmentManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        init();
    }

    private void initView() {
        mTextView1 = (TextView) findViewById(R.id.id_textView1);
        mTextView2 = (TextView) findViewById(R.id.id_textView2);
        mTextView3 = (TextView) findViewById(R.id.id_textView3);
        mTextView1.setOnClickListener(this);
        mTextView2.setOnClickListener(this);
        mTextView3.setOnClickListener(this);
    }

    private void init() {
        contentFragment1 = new ContentFragment();
        contentFragment2 = new ContentFragment2();
        contentFragment3 = new ContentFragment3();
        fragmentManager = getFragmentManager();
    }

    @Override
    public void onClick(View v) {
        //开启一个Fragment的事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.id_textView1: {
                //点击textView1时,将指定位置上的fragment直接替换成为contentFragment1
                //指定位置表示的是,在Activity的布局文件声明的frameLayout,前提是这个
                //FrameLayout必须定义了Android:id,这个也是为什么我们在FrameLayout中定义一个id
                fragmentTransaction.replace(R.id.id_frameLayout, contentFragment1);
                break;
            }
            case R.id.id_textView2: {
                fragmentTransaction.replace(R.id.id_frameLayout, contentFragment2);
                break;
            }
            case R.id.id_textView3: {
                fragmentTransaction.replace(R.id.id_frameLayout, contentFragment3);
                break;
            }
        }
        //记得提交事务
        fragmentTransaction.commit();
    }
}

在使用动态绑定是,我们需要注意几个点:

1.Activity的布局文件中的FrameLayout必须有id,后面操作事务都是依靠这个id进行的。

2.需要切换的Fragment都有独自的对象。

3.Fragment的操作依靠FragmentManager来管理,FragmentManager类的作用是:管理Fragment,除了开启Fragment的事务之外,比如还有findFragmentById()方法,通过id来查找一个fragment,前提是这个Fragment有设置id。

4.开启Fragment的事务之后,操作完成之后,记得调用fragmentTransaction的commit方法,提交事务!

效果:


3.Activity与Fragment的数据通信

  在这里之前,我们学习过Activity之间的数据通信,Activity的数据通信是基于Intent,我们可以向Intent对象里面put很多的数据,而Activity返回数据,则是同时setResult方法来实现。今天,我们学习Activity和Fragment之间的数据通信,包括Activity向Fragment的传递和Fragment向Activity返回数据。记住,我们这里操作仅仅在动态绑定使用!

(1)Activity向Fragment发送数据

  我们知道在动态绑定一个Fragment时,必须得到这个Fragment的对象。因此我们就可以将上面的代码改一下, 让Fragment的对象不在init方法里面进行初始化,而是切换时初始化。

@Override
    public void onClick(View v) {
        //开启一个Fragment的事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.id_textView1: {
                //点击textView1时,将指定位置上的fragment直接替换成为contentFragment1
                //指定位置表示的是,在Activity的布局文件声明的frameLayout,前提是这个
                //FrameLayout必须定义了Android:id,这个也是为什么我们在FrameLayout中定义一个id
                if (contentFragment1 == null) {
                    contentFragment1 = new ContentFragment();
                }
                //初始化一个数据包类
                Bundle bundle = new Bundle();
                //向bundle包类里面加入一些数据
                bundle.putString("data", "我是从Activity到ContentFragment1的数据");
                contentFragment1.setArguments(bundle);
                fragmentTransaction.replace(R.id.id_frameLayout, contentFragment1);
                break;
            }
            case R.id.id_textView2: {
                if (contentFragment2 == null) {
                    contentFragment2 = new ContentFragment2();
                }
                //初始化一个数据包类
                Bundle bundle = new Bundle();
                //向bundle包类里面加入一些数据
                bundle.putString("data", "我是从Activity到ContentFragment2的数据");
                contentFragment2.setArguments(bundle);
                fragmentTransaction.replace(R.id.id_frameLayout, contentFragment2);
                break;
            }
            case R.id.id_textView3: {
                if (contentFragment3 == null) {
                    contentFragment3 = new ContentFragment3();
                }
                //初始化一个数据包类
                Bundle bundle = new Bundle();
                //向bundle包类里面加入一些数据
                bundle.putString("data", "我是从Activity到ContentFragment1的数据");
                contentFragment3.setArguments(bundle);
                fragmentTransaction.replace(R.id.id_frameLayout, contentFragment3);
                break;
            }
        }
        //记得提交事务
        fragmentTransaction.commit();
    }

  然后我们在Fragment里面打印一个Toast看看

public class ContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //取出Bundle对象
        Bundle bundle = getArguments();
        //拿出Bundle对象里面的数据
        String string = bundle.getString("data");
        //打印Toast
        Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show();
        return inflater.inflate(R.layout.fragment_content, null);
    }
}

  这里只是展示了一个Fragment的代码,其他Fragment的代码跟这个差不多。
  效果:


(2).Fragment向Activity返回数据

  Fragment返回数据的方式: 在fragment中定义一个内部回调接口,再让包含该fragment的activity实现该回调接口,这样fragment即可调用该回调方法将数据传给activity。这里就不在详细讲解了!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容