框架地址:https://github.com/yangfuhai/afinal
对应的源码:
net.tsz.afinal.annotation.view.*
FinalActivity
FinalActivity是一个基础类,结合注解实现了,依赖注入(view的资源id,常用的监听器), 利用set方式注入
完全注解方式就可以进行UI绑定和事件绑定,无需findViewById和set event Listener
这个过程:initInjectedView>findViewById、setListener(activity, activity.field, eventLocalMethod, eventType)>
1.根据id得到view对象,设置到activity对应的field中
2. switch(eventType) {
case event:
view.set event listener(new EventListener(activity, eventLocalMethod));
EventListener implements many event listener>
在对应的event的重写方法中,反射调用eventLocalMethod
这些eventLocalMethod 的参数需要与 对应的系统 event method 的参数一致
...
}
注解有哪些类型:
[java] view plaincopyprint?
- @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="") ;
- }
调用注解:
[java] view plaincopyprint?
- 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 listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);//继承FinalActivity,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--");
- }
- }
在其他侵入式框架下使用(如ActionBarShelock)
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(view); FinalActivity.initInjectedView(this); }
在Fragment中使用
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View viewRoot = inflater.inflate(R.layout.map_frame, container, false); FinalActivity.initInjectedView(this,viewRoot); }
时间: 2024-10-25 08:48:36