2011-12-21 109 views
3

如何获得View的FragmentManager如何通过视图ID获取FragmentManager?

假设我有一个这样的布局:

<?xml version="1.0" encoding="utf-8"?> 
... 
<fragment android:name="com.example.MyFragment" 
     android:id="@+id/thefragmentsid" > 
</fragment> 
... 

这里是我的伪代码(我知道,即使没有FragmentView存在,但我想你明白我的意思)

FragmentView fw = (FragmentView) activity.findViewById(R.id.thefragmentsid); 
FragmentManager goal = fw.getFragment().getFragmentManager(); 

这里是一个示例用例:

public class MyFragment extends Fragment{ 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.my_fragments_view, container, false); 
    } 
} 

public class MainActivity extends FragmentActivity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     findViewById(R.id.thefragmentsid).setOnTouchListener(new OnTouchListener(){ 
      @Override 
      public boolean onTouch(View v, MotionEvent event){ 
       FragmentView fw = (FragmentView) findViewById(R.id.thefragmentsid); 
       FragmentManager fm = fw.getFragment().getFragmentManager(); 
       // Do something with the fragment manager 
       return false; 
      } 
     }); 
    } 
} 

编辑:我无法以任何方式访问任何Fragment -Object。也许有一种隐藏的方式,但正是我所要求的!

下一个修改:(只是为了完成问题)我使用Google支持包! PS:我不想给太多介入的信息,因为这是一个普遍的问题,需要一个普遍的答案,偶尔会出现。

+0

不仅我发布的链接100%回答你的问题,但我发布了一个例子。你还想要什么? – 2011-12-29 19:35:05

+0

@willmel 1)当然,我是一名android开发人员,并且从未见过您正在谈论的链接 - 别名_ android开发站点_。梦想^^不,我无法在Android指南中找到答案。 2)我真的很抱歉,但我无法看到你回答的答案。这听起来比想象的更加讨厌,我只是想知道你是否仔细阅读我的问题(我已经做了一些编辑)。如果您确定您的答案是正确的,请您尝试以我能理解的方式对它进行配置? – Matmarbon 2011-12-29 21:12:59

+0

也许我真的不明白关于碎片的一些重要的事情,但在这种情况下,请尝试教给我,我愿意学习! – Matmarbon 2011-12-29 21:14:28

回答

3

好的,这真的是我自己的错误,我明白了。我认为它必须是一个名为getFragmentManager()的函数,但确切地说,这是错误的。

由于我使用Google支持包,因此正确的函数称为getSupportFragmentManager()

,但是不得不说有一个大陷阱,因为Fragment的甚至与支持包内一个可以使用Fragment S功能getFragmentManager()而是FragmentActivity内它的突然getSupportFragmentManager()

我承认这是很大的耻辱,特别是我认为这不是一个必要的信息,我使用支持包。我认为这会让问题更一般,但我看到我永远不应该保留任何信息。对于未来的问题:)

0

http://developer.android.com/guide/topics/fundamentals/fragments.html

在代码中创建您的碎片,并将它们分配以同样的方式你会分配一个活动图的图。要将片段添加到XML中,请使用您在代码中创建的片段。

即。

<fragment android:name="com.example.news.ArticleReaderFragment" 
      android:id="@+id/viewer" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 

然后在后面的代码,给它一个视图

public static class ExampleFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.example_fragment, container, false); 
    } 
} 

现在你已经在代码的片段,您可以使用:

getActivity().getFragmentManager(); 

getActivity是速记获得ActivityFragment属于。有可能Fragment未附加到Activity

因此,请等到onActivityCreated或查看getActivity是否为空。

+0

而“getActivity”是哪一类/对象? – Matmarbon 2011-12-21 01:34:05

+0

@MatteoBonaker是Fragment中的一个函数。 – 2011-12-21 01:36:30

+0

但我不在任何片段的情况下,我应该在哪里得到这个? – Matmarbon 2011-12-21 01:38:29