AndroidAnnotations——Enhance Fragments 优化Fragments

翻译 2013年12月04日 15:55:43

Enhance Fragments 优化Fragments


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;
}

本文档的简单示例下载

【书籍推荐】《Creating Dynamic UI with Android Fragments》

相信你对Android 中的Fragment有所耳闻吧,这里推荐一pian
  • JiezhiG
  • JiezhiG
  • 2014年09月20日 11:14
  • 984

Android开发报错:记一次奇怪的Android Studio报错

今天做一个项目,编译时,Android Studio报错:Fragments should be static such that they can be re-instantiated by the...
  • gh8609123
  • gh8609123
  • 2016年07月03日 04:01
  • 659

Cause: java.lang.IllegalArgumentException: XML fragments parsed from previous mappers does not cont

### Cause: java.lang.IllegalArgumentException: XML fragments parsed from previous mappers does not c...
  • zshake
  • zshake
  • 2013年11月27日 21:32
  • 2964

Fragment:关于Avoid non-default constructors in fragments的错误

关于Avoid non-default constructors in fragments的错误
  • MaximusKiang
  • MaximusKiang
  • 2014年06月19日 10:30
  • 3022

Avoid non-default constructors in fragments

在使用Android Studio时Fragment类报 Avoid non-default constructors in fragments
  • yyqasg
  • yyqasg
  • 2014年11月13日 12:38
  • 2271

mybatis通配符引用报错的问题XML fragments parsed from previous mappers does not contain value

mybatis通配符引用报错的问题XML fragments parsed from previous mappers does not contain value...
  • chenkaichao
  • chenkaichao
  • 2017年02月27日 10:32
  • 3944

发现个Mybatis的bug: XML fragments parsed from previous mappers does not contain value for

XML fragments parsed from previous mappers does not contain value for
  • ruantao1989
  • ruantao1989
  • 2014年04月09日 13:57
  • 3215

Avoid non-default constructors in fragments的解决方法

今天在使用Android Studio打包的时候遇到了一个错误,错误的提示信息是这样子的:“Avoid non-default constructors in fragments: use a def...
  • runningampH
  • runningampH
  • 2016年04月06日 14:20
  • 3092

XML fragments parsed from previous mappers already contains value for

XML fragments parsed from previous mappers already contains value for  像这种原因导致的错误,是xml文件映射的方法名重复的问题...
  • liuxiaogangqq
  • liuxiaogangqq
  • 2014年09月28日 11:07
  • 10726

XML fragments parsed from previous mappers does not contain value for ……

错误信息:XML fragments parsed from previous mappers does not contain value for net.gbicc.deliveryNote.ma...
  • q512224549
  • q512224549
  • 2017年03月21日 10:54
  • 2522
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AndroidAnnotations——Enhance Fragments 优化Fragments
举报原因:
原因补充:

(最多只允许输入30个字)