Android Fragment詳解(五):Fragment與Activity通訊

來源:互聯網
上載者:User

標籤:android   style   io   ar   os   java   sp   div   on   

與activity通訊

儘管fragment的實現是獨立於activity的,可以被用於多個activity,但是每個activity所包含的是同一個fragment的不同的執行個體。

Fragment可以調用getActivity()方法很容易的得到它所在的activity的對象,然後就可以尋找activity中的控制項們(findViewById())。例如:

ViewlistView =getActivity().findViewById(R.id.list);同樣的,activity也可以通過FragmentManager的方法尋找它所包含的frament們。例如:

 

 
  1. ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment  

 

activity響應fragment的事件

 

有時,你可能需要fragment與activity共用事件。一個好辦法是在fragment中定義一個回調介面,然後在activity中實現之。

例如,還是那個新聞程式的例子,它有一個activity,activity中含有兩個fragment。fragmentA顯示新聞標題,fragmentB顯示標題對應的內容。fragmentA必須在使用者選擇了某個標題時告訴activity,然後activity再告訴fragmentB,fragmentB就顯示出對應的內容(為什麼這麼麻煩?直接fragmentA告訴fragmentB不就行了?也可以啊,但是你的fragment就減少了可重用的能力。現在我只需把我的事件告訴宿主,由宿主決定如何處置,這樣是不是重用性更好呢?)。如下例,OnArticleSelectedListener介面在fragmentA中定義:

 
  1. public static class FragmentA extends ListFragment{  
  2.   ...  
  3.   //Container Activity must implement this interface  
  4.   public interface OnArticleSelectedListener{  
  5.       public void onArticleSelected(Uri articleUri);  
  6.   }  
  7.   ...  

 

然後activity實現介面OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。當fragment添加到activity中時,會調用fragment的方法onAttach(),這個方法中適合檢查activity是否實現了OnArticleSelectedListener介面,檢查方法就是對傳入的activity的執行個體進行類型轉換,如下所示:

 
  1. public static class FragmentA extends ListFragment{  
  2.   OnArticleSelectedListener mListener;  
  3.   ...  
  4.   @Override  
  5.   public void onAttach(Activity activity){  
  6.       super.onAttach(activity);  
  7.       try{  
  8.           mListener =(OnArticleSelectedListener)activity;  
  9.       }catch(ClassCastException e){  
  10.           throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");  
  11.       }  
  12.   }  
  13.   ...  

 

如果activity沒有實現那個介面,fragment拋出ClassCastException異常。如果成功了,mListener成員變數儲存OnArticleSelectedListener的執行個體。於是fragmentA就可以調用mListener的方法來與activity共用事件。例如,如果fragmentA是一個ListFragment,每次選中列表的一項時,就會調用fragmentA的onListItemClick()方法,在這個方法中調用onArticleSelected()來與activity共用事件,如下:

 

  1. public static class FragmentA extends ListFragment{  
  2.   OnArticleSelectedListener mListener;  
  3.   ...  
  4.   @Override  
  5.   public void onListItemClick(ListView l,View v,int position,long id){  
  6.       //Append the clicked item‘s row ID with the content provider Uri  
  7.       Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI,id);  
  8.       //Send the event and Uri to the host activity  
  9.       mListener.onArticleSelected(noteUri);  
  10.   }  
  11.   ...  

 

onListItemClick()傳入的參數id是列表的被選中的行ID,另一個fragment用這個ID來從程式的ContentProvider中取得標題的內容。

複製去Google翻譯翻譯結果 

Android Fragment詳解(五):Fragment與Activity通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.