转自http://blog.youkuaiyun.com/gsw333/article/details/51858524
使用过app的人都知道,几乎所有的app都用到fragment进行首页分割,还有的加上了Indicator或者Tablayout或者Viewpager之类的结合使用。总之,fragment是必须掌握的知识,因为在app中使用率几乎高达100%了。对于fragment的使用就我所知就我会的只有两种吧,一种是add方式来进行show和add,这种方式你切换fragment不会让fragment重新刷新,而用replace方式会使fragment重新刷新,因为add方式是将fragment隐藏了而不是销毁再创建,replace方式每次都是重新创建。具体我们还是看代码来说吧,这种基础的东西看看代码就懂了。
我们做个QQ首页那种,3个fragment,首先,我们需要一个首页MainActivity和三个Fragment以及他们的布局。

首页MainActivity布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#ffffff"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <FrameLayout
- android:id="@+id/main_frame_layout"
- android:background="#dddddd"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="0dp">
- </FrameLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="48dp">
- <Button
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:background="#ffffff"
- android:text="消息"/>
- <Button
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:background="#ffffff"
- android:text="联系人"/>
- <Button
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:background="#ffffff"
- android:text="动态"/>
- </LinearLayout>
-
- </LinearLayout>
布局的下方放三个按钮,中间用Framelayout显示不同的内容用于存放fragment。布局效果如下:

其中灰色的部分就是Framelayout用于显示fragment的。
然后现在建立我们的Fragment的布局文件,代码如下:
- <?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="match_parent"
- android:background="#dddddd">
-
- <TextView
- android:id="@+id/fragment_tv"
- android:layout_centerInParent="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"/>
-
- </RelativeLayout>
TextView用于显示我们当前是第几个fragment
然后Fragment逻辑实现代码:
- package com.xjj.demo.fragment;
-
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
-
- /**
- * Created by Administrator on 2016/7/8.
- */
- public class MyFragment extends Fragment {
-
- private TextView tv;
- private String name;
-
- public MyFragment(String fName){
- this.name = fName;
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_my,container,false);
- tv = (TextView) view.findViewById(R.id.fragment_tv);
- tv.setText(name);
- tv.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- tv.setText("我变了-" + name);
- }
- });
- return view;
- }
- }
当点击fragment中的文字时,文字会发生改变。
为了方便,我们创建三个Fragment都用这个的实例,传个name过去表示是哪个fragment。后面看代码就懂了。
然后我们就开始进入MainActivity逻辑开始设置Fragment的显示。
代码中已经写的很清楚了,我就不多做解释了,代码中注释掉了replace方式,用的是add方式然后进行hide和show,他的效果是下图所示的:

上面这个gif图看清楚,我们点击下面三个按钮后切换成功,而且我们点击fragment里面的文字改变后,我们切换到其他fragment再切换回来时,发现他的文字是保持点击后改变的文字,而不是初始化的文字,比如消息fragment显示的是“消息”,我们点击后变成了:“我变了-消息”,当我们点击其他fragment后再点击消息fragment回来时,他显示的依然是“我变了-消息”,而不是初始的消息“”,这说明我们切换回来的时候fragment没有被重新创建,而是保持之前的那个fragment。这就是使用add的hide和show的方式的效果,大部分app都是采用这种方式。
下面我们用replace方式,就是注释掉add方式,使用replace方式就行了,他的效果是:

相信已经看出来了,用replace的效果就是,每次都会重新初始化fragment,比如在消息fragment点击“消息”,变成了“我变了-消息”,此时跳到联系人fragment再跳回消息fragment,发现消息fragment显示的“消息”而不是“我变了-消息”,没有保持消息fragment之前的状态。
所以用add方式实现fragment的效果就是:切换fragment时不会重新创建,是什么样子切换回来还是什么样子;用replace的效果就是:切换fragment时每次都会重新创建初始化。