寫的只是我的的理解,但願有錯大神們能指出來。 java
經過Fragments來構建你的動態Activity佈局: android
預先構建出模型,like this:
<?xml version=」1.0」 encoding=」utf-8」?>
<LinearLayout xmlns:android=」http://schemas.android.com/apk/res/android」
android:orientation=」horizontal」
android:layout_width=」match_parent」
android:layout_height=」match_parent」>
<FrameLayout
android:id=」@+id/ui_container」
android:layout_width=」match_parent」
android:layout_height=」match_parent」
android:layout_weight=」1」
/>
<FrameLayout
android:id=」@+id/details_container」
android:layout_width=」match_parent」
android:layout_height=」match_parent」
android:layout_weight=」3」
/>
</LinearLayout> ide
這是某個activity的佈局,一個大容器裏面包含了2個小容器,那麼小容器什麼用處呢? 模塊化
爲了動態在程序中添加fragment,預先先構建模型,到時候動態填充。(以前咱們曾提到容器,其實本質就是Layout)。這樣一個坑位就對應一個fragment,這也就是爲何咱們FragmentManager它的方法中像add、replace都會涉及到一個容器id,實際上是一個思想,先構建藍圖,而後不一樣模塊也就是不一樣容器放不一樣的fragment,而後每一個容器又能夠隨時更換本身的fragment,這樣就實現了動態機制。 佈局
Fragments 和 Back Stack (後臺的棧) 學習
咱們知道操做事務能夠動態改變fragments,從而改變整個UI,那麼既然涉及到了事務,從SQL學習咱們知道事務通常都有回滾的機制,那麼fragment的事務也有嗎? 動畫
答案是確定的,情形好比你在用戶交互中經過事務改變了界面,而後用戶又但願回退回上個界面,那麼只要在commit以前調用addToBackStack就能夠將這個事務保存起來,而後按回退按鈕就能夠回退了。 (~。~) ui
例子:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); this
fragmentTransaction.add(R.id.ui_container, new MyListFragment()); 線程
Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);
fragmentTransaction.remove(fragment);
String tag = null;
ragmentTransaction.addToBackStack(tag); //commit以前哦!
fragmentTransaction.commit();
這樣在你回退的時候,MyListFragment會被移除,DetailFragment會被重啓恢復。
注意:很是注意:若是你的fragment是在xml中直接定義的,你是沒法正常使用一些事務的(好比回退什麼的),因此用事務你的fragment必須用事物增長進來的。
按照個人話來講,從哪裏來就從哪裏回去。
如何使用Fragment的過分動畫,又如何自定義本身的動畫?
要使用默認的動畫,須要使用FragmentTransaction的方法setTransition方法,傳遞FragmentTransaction.TRANSIT_FRAGMENT_*,就像FragmentTransaction.TRANSIT_FRAGMENT_OPEN.
例子:transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
你也能夠自定義本身的過分動畫,使用setCustomAnimations()方法,裏面傳遞2個XML動畫資源,第一個參數的動畫是在事務加入fragment的時候,第二個參數的動畫是在事務移除或者替換了某個fragment時觸發。
例子:fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
注意:animator是3.0引入的(API Level 11)動畫,3.0以後的應該使用animator(Property Animation)這個,3.0以前support library 的只能用之前的動畫資源。
Fragments 和 Activities之間的接口:
在Fragment類中,你可使用getActivity即可得到與其綁定的activity。這對找到當前的上下文對象(context)很是有用,能夠訪問其餘的fragments經過FragmentManager,還能夠找到層次佈局中的某個View.
TextView textView = (TextView)getActivity().findViewById(R.id.textview);
可能有種需求,如何讓你的fragment與activity共享事件呢?看例子
//這是某個fragment類的片斷: 經過定義接口,讓後讓activity去實現這個接口,fragment這邊在onAttach的時候獲取到activity的引用,而後經過多態能夠將其轉化爲接口對象,而後你就能夠作共享事件的事兒了。
public interface OnSeasonSelectedListener { public void onSeasonSelected(Season season); } private OnSeasonSelectedListener onSeasonSelectedListener; private Season currentSeason; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { onSeasonSelectedListener = (OnSeasonSelectedListener)activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + 「 must implement OnSeasonSelectedListener」); } } private void setSeason(Season season) { currentSeason = season; onSeasonSelectedListener.onSeasonSelected(season); }
沒有佈局的Fragments的使用:
在絕大多數狀況,fragments仍是用來分裝模塊化的UI設計而生的,然而你也能夠建立一個沒有UI的fragment,用來提供後臺的行爲(好比保存activity的狀態。(當配置發生改變形成activity重啓)).
下面給出些代碼片斷:
public class NewItemFragment extends Fragment { @Override public void onAttach(Activity activity) { super.onAttach(activity); //獲取activity的引用。 } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 建立後臺工做線程和任務。 } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // 初始化工做線程和任務 } }
fragmentTransaction.add(workerFragment, MY_FRAGMENT_TAG); //由於沒有UI因此不能指定父容器ID,只能指定tag了。
fragmentTransaction.commit();
其它的Fragments: (這些暫時不討論,是否是很像activity的擴展呢?)
1.DialogFragment
2.ListFragment
3.WebViewFragment