android开发实战系列(18)-- 与其他应用程序进行交互

为了重用Fragment UI组件,你应该将Fragment建立成完全独立,模块化并且定义了自己布局和行为的组件。一旦你定义了这些可重用的Fragment, 你可以通过activity,应用程序逻辑使它们关联,交互以组成一个整体复合型UI。 通常情况下,你希望一个Fragment可以与另一个交互。比如在用户事件的基础上去修改内容,所有Fragment到Fragment的交互都是通过相关联的activity来做的,两个fragment应该从不直接交互;

定义一个接口 Define an Interface


为了允许Fragment与它的activity交互,你可以在fragment类中定义一个接口并且在activity中实现它。fragment可以在onAttach()方法中获取接口的实现并调用接口的方法与activity交互。 以下是fragment到activity的交互例子:

    
    
  1. public class HeadlinesFragment extends ListFragment {
  2. OnHeadlineSelectedListener mCallback;
  3.  
  4. // Container Activity must implement this interface
  5. public interface OnHeadlineSelectedListener {
  6. public void onArticleSelected(int position);
  7. }
  8.  
  9. @Override
  10. public void onAttach(Activity activity) {
  11. super.onAttach(activity);
  12. // This makes sure that the container activity has implemented
  13. // the callback interface. If not, it throws an exception
  14. try {
  15. mCallback = (OnHeadlineSelectedListener) activity;
  16. } catch (ClassCastException e) {
  17. throw new ClassCastException(activity.toString()
  18. + " must implement OnHeadlineSelectedListener");
  19. }
  20. }
  21. ...
  22. }

现在fragment可以使用OnHeadlineSelectedListener的实例mCallback调用onArticleSelected()方法(或者其他接口内的方法)提供信息给activity了。 例如,当 用户点击list item(list子项)时就会调用下面在fragment的方法。fragment使用回调接口提供事件到父的activity。

    
    
  1. @Override
  2. public void onListItemClick(ListView l, View v, int position, long id) {
  3. // Send the event to the host activity
  4. mCallback.onArticleSelected(position);
  5. }

实现接口 Implement the Interface


为了接收来自fragment的事件回调,主activity(你需要用来与fragment交互的activity)必须实现定义在fragment类中的接口。 例如:下面这个activity就实现了上一例子中的接口:

    
    
  1. public static class MainActivity extends Activity
  2. implements HeadlinesFragment.OnHeadlineSelectedListener{
  3. ...
  4. public void onArticleSelected(Uri articleUri) {
  5. // The user selected the headline of an article from the HeadlinesFragment
  6. // Do something here to display that article
  7. }
  8. }

Deliver a Message to a Fragment


主activity可以使用findFragmentById()方法获取Fragment实例,然后直接调用fragment的公共方法提供信息给fragment。

例如,想象一下在上面显示的那个activity中可能包含另外一个fragment,并且用来显示由上面那个回调方法返回的数据指定的项目 在这个案例中,这个activity可以从回调函数中获得信息并且传递给其他显示项目的fragment

    
    
  1. public static class MainActivity extends Activity
  2. implements HeadlinesFragment.OnHeadlineSelectedListener{
  3. ...
  4.  
  5. public void onArticleSelected(int position) {
  6. // The user selected the headline of an article from the HeadlinesFragment
  7. // Do something here to display that article
  8.  
  9. ArticleFragment articleFrag = (ArticleFragment)
  10. getSupportFragmentManager().findFragmentById(R.id.article_fragment);
  11.  
  12. if (articleFrag != null) {
  13. // If article frag is available, we're in two-pane layout...
  14.  
  15. // Call a method in the ArticleFragment to update its content
  16. articleFrag.updateArticleView(position);
  17. } else {
  18. // Otherwise, we're in the one-pane layout and must swap frags...
  19.  
  20. // Create fragment and give it an argument for the selected article
  21. ArticleFragment newFragment = new ArticleFragment();
  22. Bundle args = new Bundle();
  23. args.putInt(ArticleFragment.ARG_POSITION, position);
  24. newFragment.setArguments(args);
  25. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  26.  
  27. // Replace whatever is in the fragment_container view with this fragment,
  28. // and add the transaction to the back stack so the user can navigate back
  29. transaction.replace(R.id.fragment_container, newFragment);
  30. transaction.addToBackStack(null);
  31.  
  32. // Commit the transaction
  33. transaction.commit();
  34. }
  35. }
  36. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值