Android Afinal framework Learning (2) an IOC framework of FinalActivity

Source: Internet
Author: User

Android Afinal framework Learning (2) an IOC framework of FinalActivity

 

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, initialized in setContentView} public void myClick (View v) {System. 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);    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.