-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Gradle:
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'net.xkor.genaroid:core:1.2.4'
apt 'net.xkor.genaroid:compiler:1.2.4'
}
- @ViewById - inject view to Activity, Fragment or other object field;
- @InstanceState - save and restore a field value when Activity or Fragment are recreated;
- @BuilderParam - add a field to Activity or Fragment builder;
- @GActivity and @GFragment;
- @OnClick and other envents;
- @CustomListener - user defined events;
The @ViewById
annotation indicates that an activity field should be bound with the corresponding View component from the layout. It is the same as calling the findViewById() method. The view id must be set in the annotation parameter, ie @ViewById(R.id.myTextView)
.
Usage example:
public class MyActivity extends Activity {
@ViewById(R.id.labelView)
private TextView labelView;
}
You can save the instance state of your activity or fragment when it is destroyed by annotating your attributes :
public class MyActivity extends Activity {
@InstanceState
private int someField;
}
The attributes values are automatically saved when the system call onSaveInstanceState(Bundle). All of those values are restored when onCreate(Bundle) is called by the system.
The @BuilderParam
annotation indicates that an activity or fragment field should be injected with the corresponding Extra from the Intent that was used to start the activity or Fragment Arguments.
You must add annotation @GActivity
or @GFragment
to class if you want use @BuilderParam
in it.
Usage example:
@GActivity
public class MyActivity extends Activity {
@BuilderParam
String defaultLogin;
@BuilderParam(value = "defaultPassword", optional = true)
String defaultPass;
}
If you do not provide any value for the @BuilderParam
annotation, the name of the field will be used as key in a Bundle.
You can use the generated builder to pass field values:
new MyActivityBuilder(this, "default login").defaultPass("default pass").start();
By default a field is considered requilred and it must be set in the builder constructor. But you can mark it as optional and Genaroid will append a setter method for it: @BuilderParam(optional = true)
. The setter method in the generated builder will always have the same name as the argument. By default, the key used to bind the value is the field name, but you can change it by providing a value to the @BuilderParam
annotation: @BuilderParam("defaultPassword")
.
Annotate your activities and fragments with @GActivity
or @GFragment
to generating builders for it. Also you can set layout in the annotation value parameter.
By default in the first activity (or fragment) in class hierarchy annotated with @GActivity
(or @GFragment
) will be injected Genaroid calls for all methods. But you can set annotation injectCalls parameter to can add its manualy.
-
InjectGenaroidCall.ALL
- all calls will be injected, this is default value; -
InjectGenaroidCall.NONE
- no calls will not be injected; -
InjectGenaroidCall.BIND
-Genaroid.bind
will be injected toonContentChanged
method of activity or toonViewCreated
method of fragment,Genaroid.unbind
will be injected toonDestroyView
method of fragment; -
InjectGenaroidCall.INSTANCE_STATE
-Genaroid.saveInstanceState
will be injected toonSaveInstanceState
method of activity or fragment,Genaroid.restoreInstanceState
will be injected toonCreate
method of activity or fragment; -
InjectGenaroidCall.READ_PARAMS
-Genaroid.readParams
will be injected toonCreate
method of activity or fragment and toonNewIntent
method of activity; -
InjectGenaroidCall.INFLATE_LAYOUT
-Genaroid.setContentView
will be injected toonCreate
method of activity, methodonCreateView
of fragment will be overrided to return layout from annotation@GFragment
:Genaroid.inflate(this, inflater, container)
Currently, Genaroid supports the following events on views:
-
Clicks with
@OnClick
-
Long clicks with
@OnLongClick
-
Item clicks with
@OnItemClick
-
Item long clicks with
@OnItemLongClick
-
Item selected with
@OnItemSelected
and@OnNothingSelected
The view id must be set in the annotation parameter, ie @OnClick(R.id.myButton)
.
If the same method handles multiple views, multiple view ids can be given in the following format: @OnClick({R.id.myButton, R.id.myOtherButton})
.
The method may have any parameters of listener method, but their order should be equal with order of listener method parameters. Two or more different methods can handle the same view, but order of calls can be random.
Usage example:
@OnClick(R.id.myButton)
private void myButtonWasClicked() {
//...
}
@OnClick({R.id.myButton, R.id.myOtherButton})
private void handlesTwoButtons(View button) {
//...
}
@OnItemClick(R.id.listView)
private void onItemClick(View view, int position) {
//...
}
@OnItemClick(R.id.listView)
private void onItemClickById(View view, long id) {
//...
}
You can define your events that will work like built-in events of Genaroid. It is very simple. You need only create a new annotation and annotate it with @Retention
, @Target
and @CustomListener
:
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
@CustomListener(
targetClass = AdapterView.class,
listenerClass = AdapterView.OnItemSelectedListener.class,
listenerSetterName = "setOnItemSelectedListener",
listenerMethodName = "onNothingSelected")
public @interface OnNothingSelected {
@IdRes int[] value();
}
You must set following parameters of annotation @CustomListener
:
-
targetClass
- class of view that contains setter method for event listener; -
listenerClass
- class of event listener; -
listenerSetterName
- a name of setter method for event listener; -
listenerMethodName
- a name of listener method that should be used, can be omitted if listener contains only one method;
The new annotation must contains only one parameter with name value
and type int[]
.