- Android入門基礎:從這裡開始
- Android分享操作
- Android多媒體
- Android圖像與動畫
- Android網絡連接與雲服務
- Android聯繫人與位置信息
- Android可穿戴應用
- Android TV應用
- Android企業級應用
- Android交互設計
- Android界面設計
- Android用戶輸入
- Android後臺任務
- Android性能優化
- Android安全與隱私
- Android測試程序
Fragments之間的交互
編寫:fastcome1985 - 原文:http://developer.android.com/training/basics/fragments/communicating.html
為了重用Fragment UI組件,我們應該把每一個fragment都構建成完全的自包含的、模塊化的組件,定義他們自己的佈局與行為。定義好這些模塊化的Fragment後,就可以讓他們關聯activity,使他們與application的邏輯結合起來,實現全局的複合的UI。
通常fragment之間可能會需要交互,比如基於用戶事件改變fragment的內容。所有fragment之間的交互需要通過他們關聯的activity,兩個fragment之間不應該直接交互。
定義一個接口
- 為了讓fragment與activity交互,可以在Fragment 類中定義一個接口,並在activity中實現。Fragment在他們生命週期的onAttach()方法中獲取接口的實現,然後調用接口的方法來與Activity交互。
下面是一個fragment與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傳遞消息。舉個例子,在fragment中的下面的方法在用戶點擊列表條目時被調用,fragment 用回調接口來傳遞事件給父Activity.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
實現接口
為了接收回調事件,宿主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的public方法來向fragment傳遞消息。
例如,假設上面所示的activity可能包含另外一個fragment,這個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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论