欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android應(yīng)用UI開(kāi)發(fā)中Fragment的常見(jiàn)用法小結(jié)

 更新時(shí)間:2016年02月25日 11:09:39   作者:xyz_lmn  
這篇文章主要介紹了Android應(yīng)用UI開(kāi)發(fā)中Fragment的常見(jiàn)用法小結(jié),Fragment的存在是為了解決不同屏幕分辯率的動(dòng)態(tài)和靈活UI設(shè)計(jì),需要的朋友可以參考下

1.Fragment概述
在一個(gè)Activity中, Fragment代表UI的一個(gè)部分或者一個(gè)行為。一個(gè)Activity可以結(jié)合多個(gè)Fragment對(duì)象,也可以在多個(gè)activity中使用相同F(xiàn)ragment字節(jié)碼對(duì)應(yīng)的不同對(duì)象。一個(gè)Fragment對(duì)象必須被嵌入在一個(gè)主Activity對(duì)象中,該Fragment的生命周期與主Activity息息相關(guān)。比如,當(dāng)主Activity處于paused狀態(tài),其對(duì)應(yīng)的所有Fragment對(duì)象均處于paused狀態(tài),只有當(dāng)主Activity處于resumed狀態(tài)時(shí),F(xiàn)ragment才能處于自由控制狀態(tài)。


2.創(chuàng)建Fragment
為了創(chuàng)建一個(gè)Fragment,應(yīng)該去繼承Fragment或者其子類(lèi),覆寫(xiě)相應(yīng)的方法。比如onCreate(),OnCreateView(),onPause()等等
(1).添加UI界面
為該Fragment展現(xiàn)一個(gè)布局,必須去實(shí)現(xiàn)onCreateView()回掉方法。
注意:當(dāng)該Fragment繼承了ListFragment時(shí),不需要覆寫(xiě)onCreateView()方法,因?yàn)槟J(rèn)返回一個(gè)ListView對(duì)象

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  Bundle savedInstanceState) { 
 View view = inflater.inflate(R.layout.list, null); 
 return view; 
} 

(2).添加Fragment到Activity
1).通過(guò)layout布局文件
android:name屬性應(yīng)該為Fragment對(duì)應(yīng)類(lèi)的完整路徑。

<?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" > 
 
 <fragment 
  android:id="@+id/list" 
  android:name="com.example.news.ArticleListFragment" 
  android:layout_width="0dp" 
  android:layout_height="match_parent" 
  android:layout_weight="1" /> 
 <fragment 
  android:id="@+id/viewer" 
  android:name="com.example.news.ArticleReaderFragment" 
  android:layout_width="0dp" 
  android:layout_height="match_parent" 
  android:layout_weight="2" /> 
 
</LinearLayout> 

2).通過(guò)Java代碼
當(dāng)Activity運(yùn)行時(shí),可以自由的在該activity上添加fragment對(duì)象,但應(yīng)該指定一個(gè)ViewGroup容器,可以FragmentTransaction完成fragment的添加移除或者替換。

manager = getFragmentManager(); 
if(manager.findFragmentByTag("right") == null){ 
 manager.beginTransaction().replace(R.id.right, new RightFrag(), "right").commit(); 
} 

(3).fragment唯一標(biāo)示符
每個(gè)fragment需要定義一個(gè)唯一的標(biāo)識(shí)符,如果activity被銷(xiāo)毀又重新啟動(dòng),系統(tǒng)能夠恢復(fù)該fragment的狀態(tài)。如果想重新恢復(fù),需滿足下面有3種方式之一:
1).定義ID
在布局文件中,定義android:id屬性

<fragment 
 android:id="@+id/list" 
 android:name="com.example.news.ArticleListFragment" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" /> 

2).指明tag
android:tag 指明 或者 一個(gè)fragment對(duì)象add()或者replace()時(shí)指定tag

<fragment 
 android:id="@+id/list" 
 android:tag="first" 
 android:name="com.example.news.ArticleListFragment" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" /> 

或者

manager.beginTransaction() 
 .replace(R.id.right, new RightFrag(), "right")//在事務(wù)中指明該fragment的tag 
.commit(); 

3).viewgroup ID
如果該fragment均沒(méi)有id和tag,系統(tǒng)將使用container view布局的id


3.Fragment的管理
通過(guò)getFragmentManager()方法,可以得到FragmentManager對(duì)象,主要完成下面的功能

FragmentManager manager = getFragmentManager(); 
(1).得到已經(jīng)存在Fragment對(duì)象
如果該fragment在布局文件中指定了id,通過(guò)findFragmentById()得到對(duì)象,或者指定了tag可以通過(guò)findFragmentByTag()得到對(duì)象

Fragment fragment = getFragmentManager().findFragmentByTag("right"); 
//or 
Fragment fragment = getFragmentManager().findFragmentById(id); 

(2).注冊(cè)O(shè)nBackStackChangedListener監(jiān)聽(tīng)器
可以用來(lái)監(jiān)聽(tīng)該任務(wù)對(duì)應(yīng)的返回棧信息,當(dāng)該返回棧狀態(tài)發(fā)生改變時(shí),執(zhí)行對(duì)應(yīng)的onBackStackChanged() 方法

manager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { 
 @Override 
 public void onBackStackChanged() { 
  Toast.makeText(MainActivity.this, "返回堆狀態(tài)發(fā)生改變", 1).show(); 
 } 
}); 

(3).彈出返回棧
模擬用戶點(diǎn)擊返回鍵,將指定的fragment從返回棧中彈出,該操作為異步的。前提是該fragment對(duì)象使用.beginTransaction().addToBackStack("right")添加了進(jìn)返回棧

manager.popBackStack(); //Pop the top state off the back stack 
(4).FragmentTransaction事務(wù)
事務(wù)主要包含一些操作的集合,比如增加移除替換,動(dòng)畫(huà)設(shè)置等等

/* 
 * 通過(guò)manager開(kāi)啟一個(gè)事務(wù),該事務(wù)包含一些操作的集合,通事務(wù)可以 add(), remove(), replace() 
 * 完成對(duì)Fragment的操作,并使用commit()提交 
 */ 
FragmentTransaction transaction = manager.beginTransaction(); 
 
transaction.replace(R.id.right, new RightFrag(), "right"); 
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);//設(shè)置動(dòng)畫(huà) 
transaction.addToBackStack("right"); // 將該fragment加入返回堆 
// 提交事務(wù) 
transaction.commit(); 

(5).Fragment狀態(tài)管理

/* 
 * 管理Fragment的狀態(tài) 
 * 如果在一個(gè)主activityViewGroup中添加一個(gè)fragment, 
 * 如果手機(jī)屏幕旋轉(zhuǎn)了,當(dāng)前activity被銷(xiāo)毀重建,fragment也被activityManager創(chuàng)建 
 * 故在onCreate中,需要判斷一下 
 */ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 manager = getFragmentManager(); 
 if (manager.findFragmentByTag("right") == null) { 
  // if(savedInstanceState == null)也可判斷該fragment是否已經(jīng)加載 
 
  manager.beginTransaction() 
   .replace(R.id.right, new RightFrag(), "right") 
   .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)// 設(shè)置動(dòng)畫(huà) 
   .addToBackStack("right") // 將該fragment加入返回堆 
  // 提交事務(wù) 
  .commit(); 
 } 
} 


4.Fragment間信息交互
(1).取得對(duì)象

/* 
 * 點(diǎn)擊該Fragment的button按鈕,將該button的text設(shè)置為另一個(gè)fragment中Edittext的文本值 
 */ 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  Bundle savedInstanceState) { 
 View view = inflater.inflate(R.layout.list, null); 
 final Button button = (Button) view.findViewById(R.id.confirm); 
 button.setOnClickListener(new View.OnClickListener() { 
   
  @Override 
  public void onClick(View v) { 
   //通過(guò)FragmentManager找到另一個(gè)fragment中的edittext對(duì)象,并取得text內(nèi)容 
   EditText editText = (EditText)(getFragmentManager().findFragmentByTag("left").getView().findViewById(R.id.name)); 
   button.setText(editText.getText().toString()); 
  } 
 }); 
 return view; 
} 

 (2).通回掉函數(shù)

public class MainActivity extends Activity { 
 private FragmentManager manager; 
 private Button button; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
   
  button.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    RightFragment rightFrag = (RightFragment) (getFragmentManager().findFragmentByTag("right")); 
    /* 
     * 通過(guò)set方法,向其傳遞一個(gè)實(shí)例化對(duì)象,由于rightFrag.set()方法內(nèi)部執(zhí)行RightFragment.CallBack.get()方法,完成了參數(shù)的傳遞 
     */ 
    rightFrag.set(new RightFragment.CallBack() { 
     @Override 
     public void get(String str) { 
      button.setText(str); 
     } 
    }); 
   } 
  }); 
 } 
 
} 

public class RightFragment extends ListFragment { 
 private LoaderManager manager; 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  manager = getLoaderManager(); 
 } 
 
 /* 
  * 點(diǎn)擊該Fragment的button按鈕,將該button的text設(shè)置為另一個(gè)fragment中Edittext的文本值 
  */ 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) { 
  View view = inflater.inflate(R.layout.list, null); 
  return view; 
 } 
 
 /** 
  * 通過(guò)調(diào)用該方法,接收一個(gè)回掉函數(shù)對(duì)象,callBack.get(str); 
  * @param callBack 
  */ 
 public void set(CallBack callBack) { 
  EditText editText = (EditText) getView().findViewById(R.id.name); 
  callBack.get(editText.getText().toString()); 
 } 
 
 /* 
  * 回掉接口 
  */ 
 interface CallBack { 
  public void get(String str); 
 } 
} 

5.FragmentManage:
FragmentManager能夠?qū)崿F(xiàn)管理activity中fragment. 通過(guò)調(diào)用activity的getFragmentManager()取得它的實(shí)例.
FragmentManager可以做如下一些事情:
(1)使用findFragmentById() (用于在activity layout中提供一個(gè)UI的fragment)或findFragmentByTag()
   (適用于有或沒(méi)有UI的fragment)獲取activity中存在的fragment
(2)將fragment從后臺(tái)堆棧中彈出, 使用 popBackStack() (模擬用戶按下BACK 命令).
(3)使用addOnBackStackChangeListener()注冊(cè)一個(gè)監(jiān)聽(tīng)后臺(tái)堆棧變化的listener.
 
6.FragmentTransaction:
      FragmentTransaction對(duì)fragment進(jìn)行添加,移除,替換,以及執(zhí)行其他動(dòng)作。
從 FragmentManager 獲得一個(gè)FragmentTransaction的實(shí)例 :

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

每一個(gè)事務(wù)都是同時(shí)要執(zhí)行的一套變化.可以在一個(gè)給定的事務(wù)中設(shè)置你想執(zhí)行的所有變化,使用諸如 add(), remove(), 和 replace().然后, 要給activity應(yīng)用事務(wù), 必須調(diào)用 commit().

在調(diào)用commit()之前, 你可能想調(diào)用 addToBackStack(),將事務(wù)添加到一個(gè)fragment事務(wù)的back stack. 這個(gè)back stack由activity管理, 并允許用戶通過(guò)按下 BACK 按鍵返回到前一個(gè)fragment狀態(tài).

舉個(gè)例子, 這里是如何將一個(gè)fragment替換為另一個(gè), 并在后臺(tái)堆棧中保留之前的狀態(tài):

// Create new fragment and transaction 

Fragment newFragment = new ExampleFragment(); 

FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

 // Replace whatever is in the fragment_container view with this fragment, 

// and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); 

// Commit the transaction transaction.commit();

在這個(gè)例子中, newFragment 替換了當(dāng)前l(fā)ayout容器中的由R.id.fragment_container標(biāo)識(shí)的fragment.通過(guò)調(diào)用 addToBackStack(), replace事務(wù)被保存到back stack, 因此用戶可以回退事務(wù),并通過(guò)按下BACK按鍵帶回前一個(gè)fragment.

如果添加多個(gè)變化到事務(wù)(例如add()或remove())并調(diào)用addToBackStack(), 然后在你調(diào)用commit()之前的所有應(yīng)用的變化會(huì)被作為一個(gè)單個(gè)事務(wù)添加到后臺(tái)堆棧, BACK按鍵會(huì)將它們一起回退.

添加變化到 FragmentTransaction的順序不重要, 除以下例外:
必須最后調(diào)用 commit().
如果添加多個(gè)fragment到同一個(gè)容器, 那么添加的順序決定了它們?cè)趘iew hierarchy中顯示的順序.
當(dāng)執(zhí)行一個(gè)移除fragment的事務(wù)時(shí), 如果沒(méi)有調(diào)用 addToBackStack(), 那么當(dāng)事務(wù)提交后, 那個(gè)fragment會(huì)被銷(xiāo)毀,并且用戶不能導(dǎo)航回到它. 有鑒于此, 當(dāng)移除一個(gè)fragment時(shí),如果調(diào)用了 addToBackStack(), 那么fragment會(huì)被停止, 如果用戶導(dǎo)航回來(lái),它將會(huì)被恢復(fù).

提示: 對(duì)于每一個(gè)fragment事務(wù), 你可以應(yīng)用一個(gè)事務(wù)動(dòng)畫(huà), 通過(guò)在提交事務(wù)之前調(diào)用setTransition()實(shí)現(xiàn).

調(diào)用 commit() 并不立即執(zhí)行事務(wù).恰恰相反, 它將事務(wù)安排排期, 一旦準(zhǔn)備好, 就在activity的UI線程上運(yùn)行(主線程).如果有必要, 無(wú)論如何, 你可以從你的UI線程調(diào)用 executePendingTransactions() 來(lái)立即執(zhí)行由commit()提交的事務(wù). 但這么做通常不必要, 除非事務(wù)是其他線程中的job的一個(gè)從屬.

警告: 你只能在activity保存它的狀態(tài)(當(dāng)用戶離開(kāi)activity)之前使用commit()提交事務(wù).

如果你試圖在那個(gè)點(diǎn)之后提交, 會(huì)拋出一個(gè)異常.這是因?yàn)槿绻鸻ctivity需要被恢復(fù), 提交之后的狀態(tài)可能會(huì)丟失.對(duì)于你覺(jué)得可以丟失提交的狀況, 使用 commitAllowingStateLoss().

相關(guān)文章

最新評(píng)論