zoukankan      html  css  js  c++  java
  • Android Fragment详解(五):Fragment与Activity通讯

    与activity通讯

    尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。

    Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:

    ViewlistView =getActivity().findViewById(R.id.list);同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。例如:

     
    1. ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment  

    activity响应fragment的事件

    有时,你可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。

    例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。如下例,OnArticleSelectedListener接口在fragmentA中定义:

     
    1. public static class FragmentA extends ListFragment{  
    2.   ...  
    3.   //Container Activity must implement this interface  
    4.   public interface OnArticleSelectedListener{  
    5.       public void onArticleSelected(Uri articleUri);  
    6.   }  
    7.   ...  

    然后activity实现接口OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,如下所示:

     
    1. public static class FragmentA extends ListFragment{  
    2.   OnArticleSelectedListener mListener;  
    3.   ...  
    4.   @Override  
    5.   public void onAttach(Activity activity){  
    6.       super.onAttach(activity);  
    7.       try{  
    8.           mListener =(OnArticleSelectedListener)activity;  
    9.       }catch(ClassCastException e){  
    10.           throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");  
    11.       }  
    12.   }  
    13.   ...  

    如果activity没有实现那个接口,fragment抛出ClassCastException异常。如果成功了,mListener成员变量保存OnArticleSelectedListener的实例。于是fragmentA就可以调用mListener的方法来与activity共享事件。例如,如果fragmentA是一个ListFragment,每次选中列表的一项时,就会调用fragmentA的onListItemClick()方法,在这个方法中调用onArticleSelected()来与activity共享事件,如下:

    1. public static class FragmentA extends ListFragment{  
    2.   OnArticleSelectedListener mListener;  
    3.   ...  
    4.   @Override  
    5.   public void onListItemClick(ListView l,View v,int position,long id){  
    6.       //Append the clicked item's row ID with the content provider Uri  
    7.       Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI,id);  
    8.       //Send the event and Uri to the host activity  
    9.       mListener.onArticleSelected(noteUri);  
    10.   }  
    11.   ...  

    onListItemClick()传入的参数id是列表的被选中的行ID,另一个fragment用这个ID来从程序的ContentProvider中取得标题的内容。

  • 相关阅读:
    Chrome浏览器扩展开发系列之三:Google Chrome浏览器扩展的架构
    Chrome浏览器扩展开发系列之一:初识Google Chrome扩展
    Chrome浏览器扩展开发系列之五:Page Action类型的Chrome浏览器扩展
    Chrome浏览器扩展开发系列之四:Browser Action类型的Chrome浏览器扩展
    鼠标定位问题总结
    Chrome浏览器扩展开发系列之八:Chrome扩展的数据存储
    Chrome浏览器扩展开发系列之七:override页面
    Chrome浏览器扩展开发系列之六:options 页面
    Chrome浏览器扩展开发系列之二:Google Chrome浏览器扩展的调试
    Chrome浏览器扩展开发系列之九:Chrome浏览器的chrome.alarms.* API
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4091847.html
Copyright © 2011-2022 走看看