片段之間的通信
爲了重複使用片段UI組件,你應該將每個片段設計成一個完全獨立的,模塊化的組件,它有自己的佈局和行爲。一旦你定義了這些可重複使用的片段,你就能將它跟活動聯繫起來,並通過應用邏輯將他們連接,以組成全局的複合UI.
很多時候,你可能希望幾個片段之間來通信,例如根據用戶的選擇來改變內容。所有片段之間的通信都是通過跟他們想關聯的活動來完成的,兩個片段不能直接通信。
定義一個接口
要實現片段跟活動之間的通信,你可以在片段類中定義一個接口,然後在活動中來實現這個接口。片段在它的onAttach()生命週期方法中來捕獲對這個接口的實現,然後可以調用接口方法跟活動通信。
下面是一個可以跟活動通信的片段的例子:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
現在,片段就能夠像活動傳遞消息:通過OnHeadlineSelectedListener
接口的mCallback實例來調用onArticleSelected()方法(或者是接口裏面的其它方法)。
例如:當用戶點擊一個list選項時,片段內的如下方法被調用。片段利用這個回調接口傳遞事件到其活動中去。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
實現這個接口
爲了獲得片段的事件回調,片段的宿主活動必須實現片段裏面定義的接口。
如下活動實現了前面所定義的接口:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(Uri articleUri) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
向片段傳遞消息
宿主活動通過findFragmentById()來捕獲Fragment實例,然後直接調用Fragment的公開方法來傳遞信息。
例如:加入上面的活動中包含例外一個片段,它所顯示的內容是由第一個片段回調方法中傳遞的值所決定的。在這種情況下,活動就能把從回調方法中接收到的值傳遞給另外一個片段,然後顯示正確的內容。
publicstaticclassMainActivityextendsActivity
implementsHeadlinesFragment.OnHeadlineSelectedListener{
...
publicvoid onArticleSelected(int position){
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag =(ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if(articleFrag !=null){
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
}else{
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment =newArticleFragment();
Bundle args =newBundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}