Support for FragmentActivity 对FragmentActivity的支持
Since AndroidAnnotations 2.1
Prior to AndroidAnnotations
2.6, there was no support for fragment injection. However, we made sure that at least extending
FragmentActivity
instead of
Activity
didn't break AndroidAnnotations:
在
AndroidAnnotations
2.6
之前,并没有对fragment注入的支持。但是我们保证至少继承
FragmentActivity
代替
Activity
不会削弱AndroidAnnotations:
@EActivity(R.id.main)
public class DetailsActivity extends FragmentActivity {
}
Fragment Support Fragment支持
Since AndroidAnnotations 2.6
AndroidAnnotations supports both android.app.Fragment
andandroid.support.v4.app.Fragment
, and automatically uses the right APIs based on the fragment types. AndroidAnnotations支持android.app.Fragment
和android.support.v4.app.Fragment
,并且基于fragment类型自动使用正确的API。
Enhanced Fragments 优化Fragments
To start using AndroidAnnotations features in a fragment, annotate it with @EFragment
:在fragment中使用AndroidAnnotations需要给它加@EFragment
注解:
@EFragment
public class MyFragment extends Fragment {
}
AndroidAnnotations will generate a fragment subclass with a trailing underscore, e.g.MyFragment_
. You should use the generated subclass in your xml layouts and when creating new instance fragments:AndroidAnnotations将生成一个尾部带下划线的fragment子类,比如MyFragment_
。你需要在布局xml文件和创建实例的时候使用生成的子类:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/myFragment"
android:name="com.company.MyFragment_"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Programmatically:代码方式:
MyFragment fragment = new MyFragment_();
You can now use all kind of annotations in your fragment:现在你可以在你的fragment中使用全部的注解:
@EFragment
public class MyFragment extends Fragment {
@Bean
SomeBean someBean;
@ViewById
TextView myTextView;
@App
MyApplication customApplication;
@SystemService
ActivityManager activityManager;
@Click
void myButton() {
}
@UiThread
void uiThread() {
}
@AfterInject
void calledAfterInjection() {
}
@AfterViews
void calledAfterViewInjection() {
}
}
View injection and event listener binding will only be based on views contained inside the fragment. Note, however, that it's isn't currently the case for
@EBean
injected inside fragments: they have access to the activity views.
视图注入和事件监听绑定将仅仅基于包含在fragment中的视图。然而请注意,这不是
@EBean
注入到fragment中的原因:它们有访问activity视图的权利。
Fragment Layout Fragment布局
The standard way to associate a view with a fragment is to override onCreateView()
:结合view和fragment的标准方法是重写onCreateView()
函数:
@EFragment
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
}
You can let AndroidAnnotations handle that for you by setting the value
param of the@EFragment
annotation:你可以让AndroidAnnotations为你处理这种情况,它会设置@EFragment
注解的 value
参数:
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}
If you need to override onCreateView()
, e.g. because you need to access savedInstanceState
, you can still let AndroidAnnotations handle the layout creation by returning null
:如果你需要重写onCreateView()
函数,可能因为你要使用 savedInstanceState
,你仍然可以让AndroidAnnotations 通过返回 null
来处理这个布局的创建:
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;
}
}
Injecting Fragments 注入Fragments
You may inject fragments in classes annotated with @EActivity
, @EFragment
,@EView
,@EViewGroup
, @EBean
, using @FragmentById
or @FragmentByTag
. If you don't specify any value on the annotation, the field name is used.你可能通过使用 @FragmentById
或者@FragmentByTag
将fragment注入到加了 @EActivity
, @EFragment
, @EView
,@EViewGroup
,@EBean
注解的类中。假如你没有指定任何值在注解上,将会使用默认字段名。
We recommend using @FragmentById
rather then @FragmentByTag
, because no compile time validation is performed for the latter.相对于@FragmentByTag ,我们更推荐使用 @FragmentById,因为后者不会执行编译时验证。
Please be aware that @FragmentById
and @FragmentByTag
can only inject fragments, not create them, so they must already exist in the activity (either by defining them in the layout or by creating them programmatically in onCreate()
.请注意, @FragmentById
和 @FragmentByTag
只能注入到fragment中,而不能创建它们,所以它们在activity中必须已经存在(不论是在布局中定义或者在 onCreate()
代码中创建。
You can inject fragments even if they are not annotated with @EFragment
.你可以注入fragment即使它们没有使用 @EFragment
注解。
@EActivity(R.layout.fragments)
public class MyFragmentActivity extends FragmentActivity {
@FragmentById
MyFragment myFragment;
@FragmentById(R.id.myFragment)
MyFragment myFragment2;
@FragmentByTag
MyFragment myFragmentTag;
@FragmentByTag("myFragmentTag")
MyFragment myFragmentTag2;
}