Fragement簡單使用方法

Fragement

//app包下的使用
靜態使用方法

  • 創建Fragment的XML佈局

  • 創建Fragment類(將XML佈局文件與Fragment類相結合)
    重寫OnCreateView方法,該方法主要定義Fragment的佈局,以View對象的方式返回Fragment的視圖()
    //生命週期方法,第一次繪製Fragment用戶界面時調用
    //LayoutInflater inflater表示佈局填充器或者加載器,將XML文件轉換成View對象
    ( View view = inflater(R.layout.fraament,null) return view;)//該中方式可以在Fragment類中獲取相應的實例
    (return inlater.inflate(R.layout.fargment.nul;l))//該方法用於無需獲取相應實例的時候
    //ViewGroup container表示當前Fragment插入Activity的佈局視圖對象
    //Bundle savedInstanceState表示存儲上一個Fragment的信息

  • 將Fragment從Activity的佈局文件中引入
    //android:name=”fragment的包名.類名”

動態使用方法

  • 創建Fragment的XML佈局文件
  • 創建Fragment類(將XML佈局文件與Fragment類相結合)
  • (Activity)創建Fragment的管理對象
    FragmentManager fragmentManager = getSupportFragmentManager();
  • (Activity)創建Fragmnet的事務對象並開啓
    FragmentTransaction transaction = fragmentManager.beginTransaction();
  • (Activity)調用事務的動態方法動態的添加、移除、替換Fragment
    transaction.add(R.id.acticity_title_layout,new TitleFragment);//將傳入的Fragment對象添加到指定位置
    transaction.replace(R.id.fragment_floor,fragment);//將指定位置的Fragment對象替換
    transaction.replace(oldFragment1,newFragment);//將指定的Fragment對象替換爲傳入Fragment對象
  • (Activity)提交事務
    transaction.commit();

<爲兼容低版本的設備,可以使用V4包下的Fragment>

  • 使用注意事項
    使用V4包下的Fragment時需要引入的Activity需要繼承FragmentActivity
    獲取FragmentManager對象時需要調用getSupportManager()方法獲取對象
    使用V4包下的Fragment時相關的對象需要導入對應的V4包下

生命週期

  • onAttach//當Activity和Fragment產生關聯時回調的方法
  • onCreate//Fragment**第一次**被創建時調用的方法
  • onCreateView//Fragment**第一次**繪製用戶界面的時候調用
  • onActivityCreated//當前Fragment所屬Acticity創建成功時調用的方法
  • onStart//Fragment**可見**時調用
  • onResum//Fragment**獲取**界面焦點時調用
  • onPause//Fragment**失去**界面焦點時調用
  • onStop//Fragment**不可見**時調用

    <當Fragment被替換時其餘Activity的關聯被銷燬包括其本身,重新切換回來時重新創建關聯以及其本身>
    <當用戶按下Home鍵退出界面時,Fragment實例與Activity的關聯以及其本身不會被銷燬,而是會調用onStop方法,回到程序是會調用reStar方法重新成爲可見狀態>
    <當用戶點擊返回按鈕時,Fragment與Activity的關聯以及自身都被銷燬>

Fragment傳值

- Activity向Fragment傳值

(Activity)
//創建Bundle類型的容器用於傳遞數據(指定類型:鍵值對型容器)
Bundle bundle = new Bundle();
bundle.putString(“info”,info);
//創建一個ResultFragment類實例,用於傳遞數據
ResultFragment resultFragment = new ResultFragment();
//調用ResultFragment實例的setArguments方法將Bundle數據傳入該實例中
resultFragment.setArgument(bundle);
//調用Transaction的增、刪、改、查方法時傳入Bundle數據
例:transaction.replace.(R.id.layout,bundle) ; transaction.commit();

(Fragment)
//在Fragment類中調用getArgument()方法
Bundle bundle = getArgument();
bundle.getString(info);

Activity向Fragment傳值

(1)Fragment中定義傳值的回調接口,在生命週期的onAttach()方法中獲取接口的實例
    //Fragment中定義接口,接口中定義回傳數據的函數
    例:public interface MyListener{
        public abstract void sendMessage( String str );
        }
    //Fragment中創建接口實例獲取Activity從傳入的監聽接口
    例:private MyListener myLitener;
    //重寫Fragment的onCreate或者onAttach方法獲取監聽實例
    例:public void onCreate(Bundle saveInstanceState){
       super.onCreate(saveInstanceState);
       mListener = (MyListener) getActivity();(獲取Activity中的接口)}

(2)Fragment需要傳值的位置調用接口回調方法傳值
    例:mListener.

(3)Activity實現回調接口重寫回調方法獲取傳遞的值
    //Activity實現監聽接口中的方法,對返回的數據執行相應的數據操作

Activity中的Fragment與Fragment互相傳值

(1)可通過將以上兩種方式結合的形式,實現Fragment間的傳值(略)

(2)可以通過在某個Fragment中獲取到同Activity中的其它Fragment實例,實現調用其它Fragment中的類方法
//(Fragment)
例:調用getFragmentManager的findFragmentById的方法獲取同一Activity中的其它Fragment實例
OtherFragment otherFragment = (OtherFragment) getFragmentManager().findFragmentById(R.id.otherFragment);

(3)類似於方法2獲取同Activity中的其它Fragment的控件實例(首先獲得FragmentManager對象,再獲取Fragment對象, 再獲取Fragment的View對象,再通過View的findViewById方法獲取實例)
//(Fragment)
例:TextView textView = getFragmentManager().findFragmentById(R.id.otherFragment)
.getView().findViewById(r.id.textView)

(4)直接調用getActivity()方法獲取Activity實例,再調用findViewById()方法直接獲取目標控件
//(Fragment)
TextView textView = getActivity().finfViewById(R.id.textView);

ListFragment的應用

<適用於Fragment單純的展示一個ListView>
(1)myFragment的佈局默認包含一個ListView
需要定義一個LsitView(其標籤必須指定爲系統id:@id/android:list 意爲:androidR.id.list)
(2)ListView綁定數據
例:public void onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragmentList,null);
list = new ArrayList [String] ();
for( int i =0 ; i<20 ; i++ ){
list.add(“item”+i)}
ArrayAdapter[String] adapter = new ArrayAdapter[String] (getActivity(),android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
return View ;
}
//設置點擊ListView點擊事件ListView點擊的ListView、View點擊的子項、position子項的位置
**public void onListItemClick(ListVew I,View v, int position , long id )
{**
super.onListItemClick(I,v,position,id);
}

(3)myFragment需要繼承ListFragment

DialogFragment的應用

(1)創建MyDialogFragment

public class MyDialogFragment extends DialogFragment{
//Fragment
public void onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builde builder = new AlertDialog.Builde
.builder.setTitle(“”);
.builder.setMessage(“”);
.builder.setIcon(R.id.drawable.picture);
.builder.setPositiveButton(“確定”,new OnClickListener(){
@overage
public void onClick(DialogInterface dialog, int which){
}
});
return builder.creater;//返回Dialog對象
}

(2)Activity中調用
MyDialogFragment fragment = new MyDialogFragment();
fragment.show(getFragmentManager(),”dialog”);
//使用MyDialogFragment的好處在於,可以在屏幕橫豎屏切換時保存數據,避免Dialog對話框在切換橫豎屏時由於銷燬重新創建對象時導致的數據丟失,適用於類似創建賬戶等對話框(避免由於用戶的橫豎屏切換導致用戶輸入的數據丟失)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章