Android Afinal framework Learning (2) an IOC framework of FinalActivity, androidafinal
Frame address: https://github.com/yangfuhai/afinal
Source code:
Net. tsz. afinal. annotation. view .*
FinalActivity
FinalActivity is a basic class, which is implemented in combination with annotations. Dependency injection (view resource id, common listener) is used for set injection.
You can bind the UI and events in full annotation mode, without the need for findViewById and set event Listener.
This process: initInjectedView> findViewById, setListener (activity, activity. field, eventLocalMethod, eventType)>
1. Get the view object based on the id and set it to the field corresponding to the activity.
2. switch (eventType ){
Case event:
View. set event listener (new EventListener (activity, eventLocalMethod ));
EventListener implements event listener>
In the corresponding event override method, eventLocalMethod is called for reflection.
These eventLocalMethod parameters must be consistent with the corresponding system event method parameters.
...
}
Types of Annotations:
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME) public @interface ViewInject {public int id();public String click() default "";public String longClick() default "";public String itemClick() default "";public String itemLongClick() default "";public Select select() default @Select(selected="") ;}
Call annotation:
Package com. stone. afinal; import net. tsz. afinal. finalActivity; import net. tsz. afinal. annotation. view. select; import net. tsz. afinal. annotation. view. viewInject; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. imageView; import android. widget. listView; public class MainActivity extends FinalActivity {@ ViewInject (id = R. id. imageview, click = "myClick", LongClick = "myLongClick") ImageView imageview; @ ViewInject (id = R. id. listview, click = "myClick", itemLongClick = "myItemLongClick", itemClick = "myItemClick", longClick = "myLongClick", select = @ Select (selected = "myItemSelected ", noSelected = "myNoSelected") ListView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main );// Inherit from FinalActivity.} public void myClick (View v) {System is initialized in setContentView. out. println ("-- myClick --");} public void myLongClick (View v) {System. out. println ("-- myLongClick --");} public void myItemLongClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {System. out. println ("-- myItemLongClick --");} public void myItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {System. out. println ("-- myItemClick --");} public void myItemSelected (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {System. out. println ("-- myItemSelected --");} public void myNoSelected (AdapterView <?> Arg0) {System. out. println ("-- myNoSelected --");}}
Use in other intrusive frameworks (such as ActionBarShelock)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(view); FinalActivity.initInjectedView(this); }
Use in Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View viewRoot = inflater.inflate(R.layout.map_frame, container, false); FinalActivity.initInjectedView(this,viewRoot); }