跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(4)
2013-07-05 14:54
405 查看
4.4与其它碎片通信
为了重用碎片UI组件,你应该把它们每个建立成一个完全独立的、模块化的组件,让它定义自己的布局和行为。一旦你定义了这些可复用的碎片,您可以将它们与活动关联起来,并将它们连接到应用程序逻辑来实现整体复合UI通常情况下,你会想让一个碎片与另一个通信,例如根据用户事件改动内容。所有碎片到碎片的通信是通过相关联的活动进行的。两个碎片永远不应该直接通信。定义一个接口
为了让碎片与其活动通信,你可以在Fragment类中定义一个接口,并在活动内实现它。碎片在其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"); } } ... }现在碎片可以通过调用
onArticleSelected()方法(或其它接口中的方法)给活动分发消息,使用
OnHeadlineSelectedListener接口的
mCallback实例。例如,当用户点击一个列表项时碎片的下述方法会被调用。碎片使用回调接口来分发事件到父活动。
@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {// Send the event to the host activitymCallback.onArticleSelected(position);}
实现的接口
为了从碎片接收事件回调,装载碎片的活动必须实现碎片类中定义的接口。例如,以下活动实现了上面例子的接口。public static class MainActivity extends Activityimplements 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}}
分发信息到碎片
载体活动可以通过使用findFragmentById()捕捉Fragment实例,来分发消息到一个碎片,然后直接调用该碎片的公共方法。例如,想象一下上面展示的活动可能包含的另一个碎片,这个碎片用来显示由上面的回调方法返回的数据指定的项目。在这种情况下,活动可以传递在回调方法中的收到的信息到另一个将显示该项目的碎片:public static class MainActivity extends Activityimplements HeadlinesFragment.OnHeadlineSelectedListener{...public void onArticleSelected(int position) {// The user selected the headline of an article from the HeadlinesFragment// Do something here to display that articleArticleFragment 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 contentarticleFrag.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 articleArticleFragment 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 backtransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();}}}
相关文章推荐
- 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(3)
- 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(1)
- 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(2)
- 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(4)
- 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(3)
- 跟Google学习Android开发-起始篇-构建你的第一个应用程序(3)
- 跟Google学习Android开发-起始篇-构建你的第一个应用程序(4)
- 跟Google学习Android开发-起始篇-构建你的第一个应用程序(1)
- 跟Google学习Android开发-起始篇-构建你的第一个应用程序(2)
- 跟Google学习Android开发-起始篇-构建你的第一个应用程序(4)
- 跟Google学习Android开发-起始篇-管理活动的生命周期(4)
- 【Android开发】构建一个简单的用户界面
- 跟Google学习Android开发-起始篇-保存数据(2)
- Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment
- 跟Google学习Android开发-起始篇-与其它应用程序交互(2)
- Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment
- Android Training - 使用碎片创建一个动态UI(3) - 构建一个灵活的UI
- 跟Google学习Android开发-起始篇-支持不同的设备(1)
- 跟Google学习Android开发-起始篇-保存数据(1)
- 跟Google学习Android开发-起始篇-与其它应用程序交互(3)