使用roboguice与Android的碎片任何简单的例子吗? [英] Any simple examples using roboguice with fragments in android?

查看:137
本文介绍了使用roboguice与Android的碎片任何简单的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找利用碎片+ RoboGuice的工作示例问题。当您尝试了Android碎片事务管理器添加/删除片段的问题发生。一旦你告诉片段从RoboFragment继承事务管理器不再认为类是片段(因为它扩展RoboFragment)。但是,您可以使用RoboGuice自己的片段经理,但它也崩溃。是否有任何的例子在那里添加/删除动态片段RoboGuice的?

I'm having issues finding a working example of using fragments + RoboGuice. The problem happens when you attempt to add/remove fragments with the Android fragment transaction manager. Once you tell the fragment to inherit from RoboFragment the transaction manager no longer thinks the class is a fragment (because it extends RoboFragment). You can however use RoboGuice's own fragment manager but it also crashes. Is there any examples out there of adding/removing RoboGuice fragments dynamically?

推荐答案

我最近开始使用的碎片上一个新项目,下面是code我用

I've recently started using fragments on a new project, and the following is the code I'm using

我没有从RoboFragment类继承,但我做的完全一样,加入下面几行到我的onCreate和onViewCreated方法。从RoboFragment继承不应该表现什么不同,其实这是RoboFragment样子。

I'm not inheriting from the RoboFragment class, but I'm doing exactly the same by adding the following lines to my onCreate and onViewCreated methods. Inheriting from RoboFragment shouldn't behave any different, in fact this is what RoboFragment looks like.

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RoboGuice.getInjector(getActivity()).injectMembersWithoutViews(this);
}

public void onViewCreated(final View view, final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    RoboGuice.getInjector(getActivity()).injectViewMembers(this);
    // Do whatever with your injected views.
}

显然,我也实现onCreateView。

Obviously I've also implemented onCreateView.

然后,在我的活动我向FragmentActivity因为我使用的是兼容包。请注意,您必须使用FragmentActivity如果你用pre API级别11.想的兼容性如果你只是支持11再加上你不需要兼容性lib或使用FragementActivity。在我的活动我然后使用以下的片段添加到我的网页。

Then, in my Activity I extend FragmentActivity as I'm using the compatibility package. Note, you must use FragmentActivity if your wanting compatibility with pre API level 11. If you're just supporting 11 plus you don't need the compatibility lib or to use FragementActivity. In my activity I'm then using the following to add the fragment to my page.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragmentHolder, new MyFragment());
transaction.commit();

R.id.fragmentHolder的类型是一个的FrameLayout

The type of R.id.fragmentHolder is a FrameLayout.

一切工作正常,这点,我可以用我的所有注入的资源和观点在我的片段。对于完整性我使用的是最新的2.0-SNAPSHOT roboguice与反对的Andr​​oid 2.2.1的配伍-V4磁带库的版本R6。

Everything works fine with this and I'm able to use all my injected resources and views in my fragment. For completeness I'm using the latest 2.0-SNAPSHOT of roboguice with version r6 of the compatibity-v4 library against Android 2.2.1.

这篇关于使用roboguice与Android的碎片任何简单的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆