您的位置:首页 > 移动开发 > Android开发

跟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();}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐