Fragment例子 .
Fragment翻譯為碎片,功能和Activity類似,依賴於父Activity,有自己獨立的聲明周期,用來描述一些行為或一部分用戶界麵,在一個Activity中你可以合並多個fragment,在一個單獨的activity中建立多個UI麵板,同時重用fragment在多個activity中使用.你可以認為fragment作為一個activity中的一子模塊 ,接收自己的輸入事件,你可以向運行中的activity添加或移除fragment。兩個Activity通過兩個Fragment合並到一個Activity的布局方式.
下麵上例子:
效果圖:
首先創建main.xml為第一啟動頁麵
在一個activity的布局文件中嵌入兩個fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:name="com.jiayuan.fragment.fragmentImp.FirstFragment"
android:color:#ff0000;">"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment android:name="com.jiayuan.fragment.fragmentImp.SecondFragment"
android:
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隱藏"
/>
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隱藏"
/>
</LinearLayout>
分別使用getFragmentManager().findFragmentById(id);
getFragmentManager().findFragmentByTag(id);
FragmentManager:
可以使用FragmentManager來管理Fragment。在Activity中通過getFragmentManager來獲得。FragmentManager
類一些主要的方法有通過findFragmentById()來獲取一個Activity中有關Fragment布局。
當然還有類似findFragmentByTag()方法,以及當Fragment中出棧的popBackStack()同時可以注冊 addOnBackStackChangedListener()管理。具體的可以在android.app.FragmentManager類中了解。
可以使用FragmentManager進行事物處理。通過begintransaction方法獲取一個事物處理實例。
FragmentTransaction transaction = fragmentManager.beginTransaction();
在這期間可以使用 add(), remove()和replace()。最終需要改變時執行 commit()。
然後創建兩個fragment的布局文件first.xml和second.xml,其實這兩個布局文件和activity文件第一完全相同,這個就隨意定義兩個
frist.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello,你是美女?" /> </LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="sorry,我是醜男...." /> </LinearLayout>
在然後定義兩個fragement的實現類FirstFragment.java和SecondFragment.java
1、FirstFragment.java
package com.jiayuan.fragment.fragmentImp; import android.app.Fragment; import android.os.Bundle; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; public class FirstFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //繪製視圖 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.first, container, false); registerForContextMenu(root.findViewById(R.id.editText1)); return root; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(Menu.NONE, 0, Menu.NONE, "菜單1"); menu.add(Menu.NONE, 1, Menu.NONE, "菜單2"); } @Override public boolean onContextItemSelected(MenuItem item) { return super.onContextItemSelected(item); } }
2、SecondFragment.java
package com.jiayuan.fragment.fragmentImp; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SecondFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.second, container, false); } }
做幾點說明:
1、你可以在fragement的實現方法的onCreate生命周期方法中使用res/layout/xxx.xml文件,就像activity一樣。
2、如果你顯示的為列表,那麼你就可以使用PreferenceFragment來實現,它裏封裝了ListView用作列表顯示。其使用的布局文件為res/xml/xxx.xml
3、當係統調用fragment在首次繪製用戶界麵時,如果畫一個UI在你的fragment你必須返回一個View當然了你可以返回null代表這個fragment沒有UI.
在然後是在activity中使用
TestFragmentActivity.javapackage com.jiayuan.frament.activity; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestFragmentActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // FirstFragment firstFragment=new FirstFragment(); // //在Activity中通過這個與Fragment通訊 // getFragmentManager().beginTransaction().add(android.R.id.content, firstFragment).commit(); FragmentManager fm = getFragmentManager(); addShowHideListener(R.id.btn_1, fm.findFragmentById(R.id.firstFragment)); addShowHideListener(R.id.btn_2, fm.findFragmentById(R.id.secondFragment)); } void addShowHideListener(int buttonId, final Fragment fragment) { final Button button = (Button)findViewById(buttonId); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); //為Fragment設置淡入淡出效果 ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out); if (fragment.isHidden()) { ft.show(fragment); button.setText("隱藏"); } else { ft.hide(fragment); button.setText("顯示"); } ft.commit(); } }); } }
描述一下fragement的生命周期
生命周期圖:
1、從這個圖裏麵可以看出,fragment的聲明周期方法並不像activity那樣容易理解
2、一個fragment必須總是嵌入在一個activity中,同時fragment的生命周期受activity而影響
3、這是activity和fragment的回調順序:
activity.onCreate
fragment.onAttach
fragment.onCreate
activity.onStart
fragment.onActivityCreated
fragment.onStart
activity.onResume
fragment.onResume
fragment.onStop
activity.onStop
fragment.onDestroyView
fragment.onDestroy
fragment.onDetach
activity.onDestroy
4、當activity 暫停,那麼所有在這個activity的fragments將被destroy釋放。 你可以在activity的不同生命周期中,對fragment執行不同操作
最後更新:2017-04-02 16:47:43