Ò».Fragment¼ò½é
*FragmentÊÇ3.0ÒýÈëµÄAPI,Ö÷ҪΪÁ˽â¾öƽ°å,´óÆÁÄ»ÊÖ»úÏÔʾÎÊÌâ
*Fragment´ú±íÁËActivityµÄ×ÓÄ£¿é,Òò´Ë¿ÉÒÔ°ÑfragmentÀí½â³ÉActivityµÄƬ¶Î
*Fragment±ØÐ뱻ǶÈëActivityÖÐʹÓÃ
¶þ.´´½¨FragmentµÄ²½Öè
1.´´½¨Ò»¸öÀà¼Ì³ÐFragmentÀà»òÕ߯ä×ÓÀà
2.ÖØÐ´FragmentÖеÄһЩonxxx·½·¨
3.Ò»°ãÓÐÈý¸ö·½·¨ÒªÖØÐ´
.onCreate()ϵͳ´´½¨FragmentµÄ¶ÔÏóºó»Øµ÷¸Ã·½·¨,³õʼ»¯²Ù×÷
.onCreateView()µ±fragment»æÖƽçÃæ×é¼þʱ»á»Øµ÷¸Ã·½·¨ .onPause()µ±Óû§À뿪¸ÃFragmentʱ½«»Øµ÷Õâ¸ö·½·¨
ÈýʹÓÃfragmentʹÓÃActivity
*´´½¨Ò»¸ö²¼¾ÖÓÃÀ´Ìî³ä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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ÉùÒô´óС"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ËÙ¶È"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3D"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="×óÉùµÀ"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ÓÒÉùµÀ"/> </LinearLayout> |
*´´½¨Ò»¸öfragement public class SoundFragment extends Fragment
{
@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.sound_item,null) ;
return view; }
|
*Ö÷½çÃæ²¼¾Ö
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layoutwidth="matchparent" android:layoutheight="matchparent" android:orientation="vertical">
<!--ÈÝÆ÷-->
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:onClick="sound"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ÉùÒô"/>
</LinearLayout>
</LinearLayout> |
*Activity
/** * µã»÷ÉùÒô°´¼üÏÔʾfragment * @param v */ public void sound(View v){ //ÉùÃ÷Fragment SoundFragment soundFragment = new SoundFragment(); //»ñÈ¡Fragment¹ÜÀíÆ÷ FragmentManager manager = getFragmentManager(); //¿ªÆôÊÂÎñ FragmentTransaction transaction = manager.beginTransaction(); //Ìæ»»µôµ±Ç°²¼¾ÖÖеÄÖ¡²¼¾ÖÈÝÆ÷ transaction.replace(R.id.fl_container,soundFragment); //Ìá½»ÊÂÎñ transaction.commit(); } |
ËÄÉúÃüÖÜÆÚ
onAttach---ÓÃÓÚÓëAcivity½¨Á¢Á¬½ÓÓë¶Ï¿ª,µ±¸ÃFragment±»Ìí¼Óµ½Activityʱ±»»Øµ÷,Ö»»á±»µ÷ÓÃÒ»´Î
onCreate---´´½¨Fragmentʱ±»µ÷ÓÃ
onCreateView--ÿ´Î´´½¨Fragmentʱ¶¼»á±»µ÷ÓÃ
onActivityCreated:µ±FragmentËùÔÚµÄActivity±»Æô¶¯Íê³Éºóµ÷ÓÃ
onStart:Æô¶¯Fragmentʱµ÷ÓÃ
onResume:»Ö¸´Fragmentʱ±»»Øµ÷,ÔÚonStart·½·¨ºóÒ»¶¨»á±»µ÷ÓÃ
onPause:ÔÝÍ£Fragmentʱ±»»Øµ÷
onStop:Í£Ö¹Fragmentʱ±»»Øµ÷
onDestroyView:½¨Á¢ÓëÏú»ÙÊÓͼ
onDesctory:ÓÃÓÚ³õʼ»¯ÓëÏú»ÙÓëActivityÖеÄÒ»Ñù
onDetach:½«Õâ¸öFragment´ÓActivityÖÐɾ³ý,Ìæ»»Íê³Éʱµ÷ÓÃÕâ¸ö·½·¨,ÔÚonDestroy·½·¨ºóÒ»¶¨»Øµ÷ÓÃÕâ¸ö·½·¨
Îå ActivityÓëFragmentÉúÃüÖÜÆÚ¶Ô±È
Activity--------------------Fragment onAttach() onCreate() onCreateView() onCreate() onActivityCreated
onStart() onStart()
onResume() onResume()
onPause() onPause()
onStop() onStop()
onDestoryView()
onDestory()
onDestory() onDetach() |
|