Fragment

Fragment是可以嵌入Activity的UI片段,能让程序更加合理和充分地利用空间。

Fragment的基本用法
静态加载
1.  直接在宿主Activity的Layout中引入<fragment>

注意:
    name指定绑定的Fragment
    tag与id属性是必须添加的,作为Fragment的标记。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.MainActivity">

    <fragment
        android:id="@+id/fra_id"
        android:name="com.w.review.ui.fragment.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:tag="fra_demo" />

</android.support.constraint.ConstraintLayout>


2.  为Fragment设置布局
为了方便演示,就设置个纯色背景
<android.support.constraint.ConstraintLayout 
    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="match_parent"
    android:background="@color/colorAccent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是Fragment"
        android:textColor="#FFF"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


3.  设置Fragment类
public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        //绑定样式
        return inflater.inflate(R.layout.fragment_demo, container, false);
    }
}
效果
动态加载

1.  在宿主Activity中留出一块布局,用来装载Fragment的样式
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.MainActivity">

    <FrameLayout
        android:id="@+id/fra_content"

        android:layout_width="match_parent"
        android:layout_height="300dp" />

</android.support.constraint.ConstraintLayout>


2.  设置Fragment的布局
<android.support.constraint.ConstraintLayout
    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="match_parent"
    android:background="@color/colorAccent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是动态加载的Fragment"
        android:textColor="#FFF"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


3.设置Fragment类,和上面静态加载代码相同


4.  Activity中用代码动态加载Fragment
public class MainActivity extends AppCompatActivity {

    private MyFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragment();
    }

    private void initFragment(){
        mFragment = new MyFragment();
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();

        transaction
                .replace(R.id.fra_content,mFragment,"fra_demo")
                .commit();
    }
}
效果
Fragment的生命周期

Fragment依赖于Activity存在,它的生命周期相较于宿主Activity触发晚。

public class MyFragment extends Fragment {

    //Fragment和宿主 Activity 绑定
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    //Fragment 和宿主 Activity 解除绑定
    @Override
    public void onDetach() {
        super.onDetach();
    }

    //开始创建
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    //UI创建时,可见时调用
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        //绑定样式
        return null;
    }


    //宿主Activity的onCreate执行完成后回掉
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    //UI销毁,变得不可见
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    //销毁,开始回收内存
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
FragmentTransaction常用API
1.  add(int containerViewId, Fragment fragment, String tag);
    添加Fragment,UI效果是层层叠加。


    id:添加目标空间的id
    fragment:要添加的 fragment 实例
    tag:与添加的 fragment 对应,用于findFragmentByTag时查找到对应的Fragment


2.  replace(int containerViewId, Fragment fragment, String tag)
    先将之前添加的所有Fragment清除,然后再添加。


3.  detach(Fragment fragment)
    解除Fragment与宿主Activity的UI绑定,但是不调用Fragment的onDestroy()。
    即Fragment依旧存在,只是不与宿主关联。

4.  attach(Fragment fragment)
    重新绑定Fragment,调用onCreateView()。


5.  remove(Fragment fragment)
    调用Fragment的onDestroy(),彻底删除Fragment与Activity的关联。


6.  hide(Fragment fragment)
    隐藏Fragment,不调用任何生命周期方法只是隐藏UI,避免反复调用创建,消耗内存。

7.  show(Fragment fragment)
    将隐藏UI显示出来。
commit() 与 commitAllowingStateLoss() 提交区别

Activity在以下情况容易触发onSaveInstanceState(Bundle outState)
   按下HOME键
   按下电源(关闭屏幕显示)键
   屏幕方向切换
   Activity跳转切换

在以上情况提交Fragment会抛出异常,
也就说尽量不能在onPause(),onStop(),onDestroy(),做commit()提交操作。

如果需要,就要用commitAllowingStateLoss()代替commit()。

Fragment的返回栈

Fragment默认不会响应Back键,因此点击Back键时,Fragment无回退状态。
如果想按下Back键回退到上一个碎片,就需要设置Fragment的返回栈。

设置Fragment
public class RedFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = new View(getContext());

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, 56);

        view.setLayoutParams(params);
        params.setMargins(0, 0, 0, 20);

        view.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        //绑定样式
        return view;
    }
}


public class MainActivity extends AppCompatActivity {

    private Button mBtnAddFra, mBrnPopupFra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }


    private void initEvent() {

        mBtnAddFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadFra();
            }
        });

        mBrnPopupFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getSupportFragmentManager().popBackStack();
            }
        });
    }

    private void initView() {
        mBtnAddFra = findViewById(R.id.btn_add_fra);
        mBrnPopupFra = findViewById(R.id.btn_popup_fra);
    }

    private void loadFra() {
        FragmentTransaction fraTran = getSupportFragmentManager().beginTransaction();
        
          addToBackStack(String name)
          name 代表这次Transaction的名称
          可以根据这个名称找到相应的添加Fragment操作,进行回退操作
          popBackStack("popup1", POP_BACK_STACK_INCLUSIVE);
          也可以传null代表不记录该次操作
        
        fraTran.add(R.id.fra_content, new RedFragment(), "Tag")
                .addToBackStack(null)
                .commit();
    }
}

Fragment动画
主要是使用FragmentTransaction中setCustomAnimations()设置动画
1.v4的Transaction支持XML动画,但不支持属性动画。

FragmentTransaction setCustomAnimations (int enter, int exit, 
int popEnter, int popExit)

参数
  enter:当一个Fragment被添加added 或者绑定 attached到视图上时
  exit:当一个Fragment从视图上被移除removed或者解除绑定detached时
  popEnter:当调用popBackStack()弹栈后,栈顶Fragment重新被添加时
  popExit:当调用popBackStack()弹栈后,弹出的Fragment
slide_left_in.xml:左视图进入时动画
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="-100%p"
        android:toXDelta="0%p" />

    <alpha
        android:duration="700"
        android:fromAlpha="0.5"
        android:toAlpha="1.0" />
</set>


slide_left_out.xml:左视图离开时动画
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p" />

    <alpha
        android:duration="700"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />
</set>


slide_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="100%p"
        android:toXDelta="0%p" />

    <alpha
        android:duration="700"
        android:fromAlpha="0.5"
        android:toAlpha="1.0" />
</set>


slide_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="0%p"
        android:toXDelta="100%p" />

    <alpha
        android:duration="700"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />
</set>



public class MainActivity extends AppCompatActivity {

    private Button mBtnPrevious, mBtnNext;
    private RedFragment mRedFra;
    private BlueFragment mBlueFra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        initFra();
    }

    private void initFra() {
        mRedFra = new RedFragment();
        mBlueFra = new BlueFragment();

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fra_content, mRedFra)
                .commit();
    }


    private void initEvent() {

        mBtnPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportFragmentManager()
                        .popBackStack();
            }
        });

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportFragmentManager()
                        .beginTransaction()
                        .setCustomAnimations(
                                R.anim.slide_right_in,
                                R.anim.slide_left_out,
                                R.anim.slide_left_in,
                                R.anim.slide_right_out
                        ).replace(R.id.fra_content, mBlueFra)
                        .addToBackStack(null)
                        .commit();
            }
        });
    }

    private void initView() {
        mBtnPrevious = findViewById(R.id.btn_previous);
        mBtnNext = findViewById(R.id.btn_next);
    }
}

Fragment通讯
1.Activity和Fragment通讯,使用Bundle
public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, 
                             @Nullable ViewGroup container, 
                             @Nullable Bundle savedInstanceState) {

        //拿到传递的数据
        String res = getArguments().get("key").toString();

        TextView textView = new TextView(getContext());
        textView.setLayoutParams(
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));

        textView.setText(res);
        return textView;
    }
}


public class MainActivity extends AppCompatActivity {

    private Button mBtnAddFra;
    private MyFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void addFra() {
        mFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key", "传递的数据");
        mFragment.setArguments(bundle);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fra_content, mFragment, "fra")
                .commit();
    }


    private void initEvent() {

        mBtnAddFra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addFra();
            }
        });
    }

    private void initView() {
        mBtnAddFra = findViewById(R.id.btn_add_fra);
    }
}
效果
2.多个Fragment间通讯
Fragment必定依存于Activity,因此将Activity作为中间层,通过接口进行交互。

举个例子
在FragmentTwo监听显示FragmentOne中Button的点击次数。


1.  为了方便,选择静态加载Fragment布局
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ui.activity.MainActivity">

    <fragment
        android:id="@+id/fra_red"
        android:name="com.w.review.ui.fragment.ShowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:tag="showFra" />

    <fragment
        android:id="@+id/fra_blue"
        android:name="com.w.review.ui.fragment.ClickFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:tag="clickFra" />

</LinearLayout>
2.定义接口,由Activity实现
  你可以理解为这个接口成了Activity的父类
  当Fragment调用getActivity()时,根据多态特性,调用的是Activity的实现

public interface IClickCountListener {
    void clickCount(int index);
}


public class MainActivity extends AppCompatActivity implements IClickCountListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void clickCount(int index) {
        ShowFragment showFrat = (ShowFragment) getSupportFragmentManager()
                .findFragmentByTag("showFra");
        showFrat.setCount(index);
    }
}
3.ClickFragment调用点击
public class ClickFragment extends Fragment {

    private View mView;
    private Button mBtnAddCount;

    private int index = 0;
    private IClickCountListener mClickCountListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_blue, container, false);
        mBtnAddCount = mView.findViewById(R.id.btn_add_count);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //获取Activity的引用,子类指向父类
        mClickCountListener = (IClickCountListener) getActivity();
        initEvent();
    }

    private void initEvent() {
        mBtnAddCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                index++;
                //调用的还是子类的实现
                mClickCountListener.clickCount(index);
            }
        });
    }
}
4.showFragment定义方法用于显示计数,当clickFragment数值改变时,相应的也会调用。

public class ShowFragment extends Fragment {

    private View mView;
    private TextView mTvCount;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_red, container, false);
        mTvCount = mView.findViewById(R.id.tv_count);
        return mView;
    }

    public void setCount(int index) {
        mTvCount.setText(String.valueOf(index));
    }
}

Fragment补充
1.屏幕旋转时Fragment数据丢失问题
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("index", index);
}
2.inflate
inflate(R.layout.fragment_one, container, false);

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