爲了可以複用一個fragment,所以在定義fragment的時候,要把它定義爲一個完全獨立和模塊化,它有它自己的layout和行爲。當你定義好了這些可複用的fragment,可以把他們和activity相關聯,在應用的邏輯基礎上把這些fragment相互關聯,從而組成一個完整的UI。
很多時候,我們需要fragment直接進行通信,比方說,根據用戶的動作交換內容。所有的fragment直接的通信,都是利用與之關聯的activity.2個fragment永遠不可能直接通信。
定義接口
要允許fragment和它所在的activity通信,你可以在Fragment類裏面定義一個接口,然後在activity裏面實現這個接口。Fragment會在它的生命週期函數:onAttach()期間捕獲這個接口的實現。然後就可以調用這個接口的方法和activity通信了。
下面是一個通信的例子:
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");
}
}
...
}
然後,這個fragment就可以利用OnHeadlineSelectedListener這個接口的實例
mCallback,來調用
onArticleSelected()(或者是其他接口裏面的方法)來給activity發送信息。
比方說,下面的例子就是,當用戶點擊一個list裏面的一項的時候,fragment裏面接口函數被調用,利用這個接口裏面的函數,給父activity發送消息。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
實現接口
爲了處理從fragment來的回調函數傳來的消息,那麼要處理這個回調消息的activity,也就是這個fragment所在的主體activity,必須實現在fragment裏面定義的接口。
比方說,下面就是activity對上面那個接口的實現:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
向Fragment發送消息
主體的activity可以通過使用findFragmentById()來獲取
Fragment的實例,然後發送消息到這個fragment,可以直接調用fragment的公有方法。
想象一下,顯示在最上面的activity可能還包括有另外一個fragment,用來顯示從上面的回調函數裏裏面返回的信息。那這種情況下,activity可以把從上面的回調函數裏面接口的信息,傳遞給要顯示這個信息的fragment.
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void 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 = new ArticleFragment();
Bundle args = new Bundle();
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();
}
}
}