Android是在Android 3.0 (API level 11)开始引入Fragment的。

  可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。

  可以把Fragment设计成可以在多个Activity中复用的模块。

  当开发的应用程序同时适用于平板电脑和手机时,可以利用Fragment实现灵活的布局,改善用户体验。


显示类似于tabhost,可以切换界面

privateFirstFragmentfirstFragment;firstFragment=newFirstFragment();

privatevoidsetDefaultFragment(){FragmentManagerfm=getFragmentManager();FragmentTransactiontransaction=fm.beginTransaction();transaction.replace(R.id.content,firstFragment);transaction.show(firstFragment);transaction.commit();}

Fragment所使用的佈局<FrameLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"/>


//自定义一个FirstFragment继承Fragment

publicclassFirstFragmentextendsFragmentimplementsOnClickListener{privateButtonbtn;@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){Viewview=inflater.inflate(R.layout.layout_first,container,false);btn=(Button)view.findViewById(R.id.button1);btn.setOnClickListener(this);returnview;}publicvoidonResume(){super.onResume();}@OverridepublicvoidonClick(Viewarg0){if(arg0.getId()==R.id.button1){Toast.makeText(getActivity(),"click",200).show();}}}


//關於Fragment傳值

//可以傳遞數組、對象等

Bundlebundle=newBundle();bundle.putString("name","阿三");firstFragment.setArguments(bundle);FragmentManagerfm=getFragmentManager();FragmentTransactiontransaction=fm.beginTransaction();transaction.replace(R.id.content,firstFragment);transaction.show(firstFragment);transaction.commit();


//接收傳值

Stringname=getArguments().getString("name");


如何切換Fragment顯示不同界面

FragmentManagerfm=getFragmentManager();FragmentTransactiontransactioif(arg1==true){transaction.replace(R.id.content,firstFragment);}else{transaction.replace(R.id.content,secondFragment);}transaction.commit();


Fragment家族常用的API

Fragment常用的三个类:

android.app.Fragment 主要用于定义Fragment

android.app.FragmentManager 主要用于在Activity中操作Fragment

android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~

a、获取FragmentManage的方式:

getFragmentManager() // v4中,getSupportFragmentManager

b、主要的操作都是FragmentTransaction的方法

FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务

transaction.add()

往Activity中添加一个Fragment

transaction.remove()

从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈,这个Fragment实例将会被销毁。

transaction.replace()

使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~

transaction.hide()

隐藏当前的Fragment,仅仅是设为不可见,并不会销毁

transaction.show()

显示之前隐藏的Fragment

detach()

会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。

attach()

重建view视图,附加到UI上并显示。

transatcion.commit()//提交一个事务

注意:常用Fragment的哥们,可能会经常遇到这样Activity状态不一致:State loss这样的错误。主要是因为:commit方法一定要在Activity.onSaveInstance()之前调用。

上述,基本是操作Fragment的所有的方式了,在一个事务开启到提交可以进行多个的添加、移除、替换等操作。

值得注意的是:如果你喜欢使用Fragment,一定要清楚这些方法,哪个会销毁视图,哪个会销毁实例,哪个仅仅只是隐藏,这样才能更好的使用它们。

a、比如:我在FragmentA中的EditText填了一些数据,当切换到FragmentB时,如果希望会到A还能看到数据,则适合你的就是hide和show;也就是说,希望保留用户操作的面板,你可以使用hide和show,当然了不要使劲在那new实例,进行下非null判断。

b、再比如:我不希望保留用户操作,你可以使用remove(),然后add();或者使用replace()这个和remove,add是相同的效果。

c、remove和detach有一点细微的区别,在不考虑回退栈的情况下,remove会销毁整个Fragment实例,而detach则只是销毁其视图结构,实例并不会被销毁。那么二者怎么取舍使用呢?如果你的当前Activity一直存在,那么在不希望保留用户操作的时候,你可以优先使用detach。


部分轉自:http://blog.csdn.net/lmj623565791/article/details/37992017



fragment接收activity的回调处理

// 回调函数

//import android.app.Fragment;

//需要导入.app这个包,V4包测试收不到回调

@OverridepublicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){if(data==null){return;}}


//*布局文件

<?xmlversion="1.0"encoding="utf-8"?><TabHostxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"android:gravity="center"><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"><Viewandroid:id="@+id/layout_order_top"android:layout_width="match_parent"android:layout_height="30dp"android:background="@color/main_color"android:visibility="gone"/><LinearLayoutandroid:id="@+id/ll_order_title"android:layout_width="match_parent"android:layout_height="45dp"android:layout_below="@id/layout_order_top"android:background="@color/main_color"android:visibility="gone"><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_marginLeft="15dp"android:layout_marginRight="5dp"android:layout_weight="1"android:gravity="center"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="@drawable/shape_radius_order_search"android:gravity="center"android:orientation="horizontal"><com.zhiduan.crowdclient.view.ClearEditTextandroid:id="@+id/edt_order_billcode"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginLeft="5dp"android:layout_weight="1"android:background="#00000000"android:digits="@string/edit_digits"android:drawableLeft="@drawable/order_magnifier"android:drawableRight="@drawable/homepage_close"android:gravity="center_vertical"android:hint="输扫描运单号"android:imeOptions="actionSearch"android:maxLength="20"android:singleLine="true"android:textSize="12sp"/><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:layout_marginBottom="8dp"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_marginTop="8dp"android:background="@color/main_bg_color"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="12dp"android:onClick="scan"android:src="@drawable/order_scancode"/></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="60dp"android:layout_height="match_parent"android:gravity="center_vertical"android:onClick="orderSort"android:padding="5dp"android:paddingLeft="5dp"android:paddingRight="20dp"><ImageViewandroid:id="@+id/p_w_picpathView_sort"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:src="@drawable/order_screen"/></LinearLayout></LinearLayout><LinearLayoutandroid:id="@+id/ll_order_menu"android:layout_width="fill_parent"android:layout_height="50dp"android:layout_below="@+id/ll_order_title"android:layout_marginTop="3dp"android:orientation="vertical"><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:visibility="gone"/><FrameLayoutandroid:id="@+id/fl_bottom_bar"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="horizontal"><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_order_wait"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:clickable="true"android:gravity="center"android:onClick="waitTaking"android:text="待取件(0)"android:textColor="@color/main_color"android:textSize="16dp"/><Viewandroid:id="@+id/view_wait"android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/main_color"/></LinearLayout><Viewandroid:layout_width="0.1dp"android:layout_height="match_parent"android:layout_marginBottom="10dp"android:layout_marginTop="10dp"android:background="@color/gray_4"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_order_sending"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:clickable="true"android:gravity="center"android:onClick="distribution"android:text="配送中(0)"android:textColor="#212124"android:textSize="16dp"/><Viewandroid:id="@+id/view_sending"android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/transparent"/></LinearLayout><Viewandroid:layout_width="0.1dp"android:layout_height="match_parent"android:layout_marginBottom="10dp"android:layout_marginTop="10dp"android:background="@color/gray_4"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_order_signed"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:clickable="true"android:gravity="center"android:onClick="signed"android:text="已签收(0)"android:textColor="#212124"android:textSize="16dp"/><Viewandroid:id="@+id/view_signed"android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/transparent"/></LinearLayout><Viewandroid:layout_width="0.1dp"android:layout_height="match_parent"android:layout_marginBottom="10dp"android:layout_marginTop="10dp"android:background="@color/gray_4"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/tv_order_abnormal"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:clickable="true"android:gravity="center"android:onClick="abNormal"android:text="异常件(0)"android:textColor="#212124"android:textSize="16dp"/><Viewandroid:id="@+id/view_abnormal"android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/transparent"/></LinearLayout></LinearLayout></FrameLayout></LinearLayout><!--Tab内容--><LinearLayoutandroid:id="@+id/ll_order_content"android:layout_width="fill_parent"android:layout_height="match_parent"android:layout_below="@+id/ll_order_menu"android:orientation="vertical"><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"android:background="#ffffff"/><FrameLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout><!--Tab按钮--></RelativeLayout></TabHost>