Fragment

来源:互联网 发布:淘宝旅拍潮搭活动 编辑:程序博客网 时间:2024/06/09 20:23

fragment生命周期

onCreate过程Activity &&&& onCreateFragment **** onAttachFragment **** onCreateFragment **** onCreateViewFragment **** onActivityCreate
onStart过程Activity &&&& onStartFragment **** Start
onResume过程Activity &&&& onResumeFragment **** onResume
onPause过程Fragment **** onPauseActivity &&&& onPause
onStop过程Fragment **** onStopActivity &&&& onStop
onDestroy过程Fragment **** onDestroyViewFragment **** onDestroyFragment **** onDetachActivity &&&& onDestroy

静态fragment:如TitleFragment

(另外可以使用组合控件实现)

//新建TitleFragment继承Fragment(必须使V4包)public class TitleFragment extends Fragment {    @Nullable    @Override    //重写onCreateView,加载Fragemnt布局    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View layout = View.inflate(getContext(), R.layout.fragment_title_bar,null);        ButterKnife.bind(this, layout);        return layout;    }}
//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="horizontal"    android:padding="10dp"    android:gravity="center_vertical">    <TextView        android:id="@+id/tv_location"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableRight="@drawable/lbs_red_vector"        android:text="定位中"        android:textColor="@color/colorAccent" />    <RelativeLayout        android:id="@+id/rl_search"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:layout_weight="1">        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:drawableLeft="@mipmap/search_icon"            android:gravity="center_vertical"            android:hint="搜索您想要的商品" />        <View            android:layout_width="match_parent"            android:layout_height="1px"            android:layout_alignParentBottom="true"            android:background="@drawable/selector_color" />    </RelativeLayout>    <ImageView        android:id="@+id/iv_message"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/msg_red_vector" /></LinearLayout>
//在Activity的xml中声明此Fragment,就当和普通的view一样(注意:要写上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/title_bar"        android:layout_width="match_parent"        android:layout_height="50dp"        android:name="ui.fragment.TitleBarFragment"/></LinearLayout>

动态Fragment:如结合RadioButton结合使用

//新建几个类继承Fragment(必须使V4包),重写onCreateView决定Fragemnt的布局(同上)
//Fragment的布局(同上)
//在Activity中声明FrameLayout

<?xml version="1.0" encoding="utf-8"?><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"    tools:context="ui.activity.MainActivity">    <RadioGroup        android:id="@+id/rg_tab"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal"        android:padding="5dp">        <RadioButton            android:id="@+id/tab_home"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/tab_home_selector"            android:gravity="center"            android:tag="0"            android:text="首页"            android:textColor="@color/tab_selector"            android:textSize="18sp" />        <RadioButton            android:id="@+id/tab_goods"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/tab_goods_selector"            android:gravity="center"            android:tag="1"            android:text="全部商品"            android:textColor="@color/tab_selector"            android:textSize="18sp" />        <RadioButton            android:id="@+id/tab_shopping"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/tab_shoppingcar_selector"            android:gravity="center"            android:tag="2"            android:text="购物车"            android:textColor="@color/tab_selector"            android:textSize="18sp" />        <RadioButton            android:id="@+id/tab_personal"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/tab_personal_selector"            android:gravity="center"            android:tag="3"            android:text="个人中心"            android:textColor="@color/tab_selector"            android:textSize="18sp" />    </RadioGroup>    <FrameLayout        android:id="@+id/fl_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/rg_tab"></FrameLayout></RelativeLayout>

//通过getSupportFragmentManager获得的FragmentManager开启事务,对Fragment实例进行操作 (replace是remove和add的合体,hide与remove的区别是是否保存数据)

    //要切换的四个Fragment    Fragment[] tabList = new Fragment[4];    //当前页角标    int currIndex = -1;    @Override    protected void initView() {        rgTab.setOnCheckedChangeListener(this);        //默认选择第一个界面        rgTab.check(R.id.tab_home);    }
  @Override    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkId) {        showToast(checkId+"");        //拿到Tag        int tag = Integer.parseInt(rgTab.findViewById(checkId).getTag().toString());        //切换三步骤        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();        if (currIndex != -1)            //隐藏上一个界面            ft.hide(tabList[currIndex]);        if (tabList[tag]==null){            tabList[tag] = createFragment(tag);            ft.add(R.id.fl_content,tabList[tag]);        }else            ft.show(tabList[tag]);        currIndex = tag;        ft.commit();    }    private Fragment createFragment(int tag) {        switch (tag){            case 0:                return new HomeFragment();            case 1:                return new GoodsFragment();            case 2:                return new ShoppingFragment();            case 3:                return new PersonalFragment();        }        return null;    }

管理Fragment回退栈

前提是动态加载fragment,代码为ft.addToBackStack(null);

Fragment与Activity通信

1.获取Fragment引用

TitleBarFragment titleBarFragment = (TitleBarFragment) getFragmentManager().findFragmentById(R.id.title_bar);TitleBarFragment titleBarFragment = (TitleBarFragment) getFragmentManager().findFragmentByTag("TAG0");

2.获取Activity引用

可以通过getActivity得到当前绑定的Activity的实例,然后进行操作。

3.在Fragment声明一个接口,回调fragment的点击事件 (点击前的准备,点击,点击后的回调)

避免重复new Fragment

给一个前提 if(savedInstanceState == null)