刷新片段的内容

时间:2012-01-27 08:03:49

标签: java android android-fragments

这是一个android编程问题。 我按照the offical example编写了一个活动,其中包含两个显示列表菜单和内容的片段。 当用户单击列表中的项目时,详细信息将显示在内容片段中。 现在,我想在Actionbar中添加一个刷新按钮。当用户单击它时,将重新加载内容片段中的内容。

但我不知道如何获取正在运行的内容片段的引用并将其调用以刷新。 (假设所有内容片段都已实现了一个名为refresh()的方法)

public class FragmentLayout extends Activity {
   .....
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_reload:      //refresh button is clicked
                 //how can I call the DetailFragment to reload here?
            }
   public static class TitlesFragment extends ListFragment {
       ....
   }
   public static class DetailFragment extends Fragment {
       ...
   }    

简而言之,片段和活动如何包含片段进行交流?

1 个答案:

答案 0 :(得分:1)

覆盖片段中的onOptionsItemSelected而不是活动,并在内部对刷新事件做出反应。