[android]AndroidInject框架——我的第一个android小型框架

作为一个移动应用开发者,随着需求的日益增多,Android项目的越来越臃肿,代码量越来越大,

现在冷静下来回头看看我们的代码,有多少代码跟业务逻辑没什么关系的

 

所以,本人自不量力,在github上建了个开源项目,希望能一定程度地简化我的代码-。-

现在第一个版本完成,希望有兴趣的朋友能加入一起完善。

本人才疏学浅,代码中有写得不妥的地方希望大家不吝赐教哈!

github地址:

https://github.com/wangjiegulu/androidInject

androidInject_1.0.jar:

http://pan.baidu.com/s/1rcSiy

 

主要的思想就是通过注解的方式,把我们要做的事情直接注入进来给我们,“不是我去调用对象,而是对象自己来找我”

现在刚写完了10个注解:

@AINoTitle, @AIFullScreen, @AILayout, @AIView, @AIBean, @AISystemService, @AIClick, @AIItemClick, @AILongClick, @AIItemLongClick

使用方法如下:

Activity中使用:

package com.wangjie.androidinject;

import android.app.AlarmManager;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.wangjie.androidinject.annotation.annotations.*;
import com.wangjie.androidinject.annotation.present.AIActivity;
import com.wangjie.androidinject.model.Person;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AIFullScreen
@AINoTitle
@AILayout(R.layout.main)
public class MainActivity extends AIActivity{

    @AIView(id = R.id.btn1, clickMethod = "onClickCallback", longClickMethod = "onLongClickCallback")
    private Button btn1;

    @AIView(clickMethod = "onClickCallback", longClickMethod = "onLongClickCallback")
    private Button btn2;

//    @AIView(id = R.id.btn3)
//    private Button btn3;

//    @AIView(id = R.id.listView, itemClickMethod = "onItemClickCallback", itemLongClickMethod = "onItemLongClickCallbackForListView")
    @AIView(id = R.id.listView)
    private ListView listView;

    @AIBean
    private Person person;

    @AISystemService
    private AlarmManager alarmManager;
    @AISystemService
    private LocationManager locationManager;
    @AISystemService
    private LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        for(int i = 0; i < 10; i++){
            map = new HashMap<String, String>();
            map.put("title", "item_" + i);
            list.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.list_item, new String[]{"title"}, new int[]{R.id.list_item_title_tv});
        listView.setAdapter(adapter);

        person.setName("wangjie");
        person.setAge(23);
        System.out.println(person.toString());

        System.out.println("alarmManager: " + alarmManager + ", locationManager: " + locationManager + ", inflater: " + inflater);


    }

    @Override
    public void onClickCallback(View view){
        if(view instanceof Button){
            Toast.makeText(context, "onClickCallback: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onLongClickCallback(View view){
        if(view instanceof Button){
            Toast.makeText(context, "onLongClickCallback: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onItemClickCallback(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(context, "onItemClickCallback: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
    }

    @AIClick({R.id.btn3, R.id.toFragmentBtn})
    public void onClickCallbackForBtn3(View view){
        if(view instanceof Button){
            Toast.makeText(context, "onClickForBtn3: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
        }

        if(view.getId() == R.id.toFragmentBtn){
            startActivity(new Intent(context, SecendActivity.class));
        }

    }

    @AILongClick({R.id.btn3})
    public void onLongClickCallbackForBtn3(View view){
        if(view instanceof Button){
            Toast.makeText(context, "onLongClickCallbackForBtn3: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
        }
    }

    @AIItemClick({R.id.listView})
    public void onItemClickCallbackForListView(AdapterView<?> adapterView, View view, int i, long l){
        Toast.makeText(context, "onItemClickCallbackForListView: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
    }

    @AIItemLongClick(R.id.listView)
    public boolean onItemLongClickCallbackForListView(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(context, "onItemLongClickCallbackForListView: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
        return true;
    }



}
View Code

Fragment中使用:

 1 package com.wangjie.androidinject;
 2 
 3 import android.app.AlarmManager;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.*;
 7 import com.wangjie.androidinject.annotation.annotations.*;
 8 import com.wangjie.androidinject.annotation.present.AISupportFragment;
 9 import com.wangjie.androidinject.model.Person;
10 
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 
16 /**
17  * Created with IntelliJ IDEA.
18  * Author: wangjie  email:wangjie@cyyun.com
19  * Date: 13-12-4
20  * Time: 下午4:37
21  */
22 @AILayout(R.layout.fragment_a)
23 public class FragmentA extends AISupportFragment{
24 
25     @AIView
26     private Button fragmentABtn1;
27 
28     @AIView
29     private GridView fragmentGv;
30 
31     @AIBean
32     private Person person;
33 
34     @AISystemService
35     private AlarmManager alarmManager;
36 
37     @Override
38     public void onActivityCreated(Bundle savedInstanceState) {
39         super.onActivityCreated(savedInstanceState);
40 
41         List<Map<String, String>> list = new ArrayList<Map<String, String>>();
42         Map<String, String> map;
43         for(int i = 0; i < 10; i++){
44             map = new HashMap<String, String>();
45             map.put("title", "fragment_item_" + i);
46             list.add(map);
47         }
48 
49         SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.list_item, new String[]{"title"}, new int[]{R.id.list_item_title_tv});
50         fragmentGv.setAdapter(adapter);
51 
52         person.setName("androidInject");
53         person.setAge(1);
54         System.out.println(person.toString());
55 
56         System.out.println("alarmManager: " + alarmManager);
57 
58     }
59 
60     @AIClick(R.id.fragmentABtn1)
61     private void btnOnclick(View view){
62         if(view instanceof Button){
63             Toast.makeText(context, "btnOnclick: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
64         }
65     }
66 
67     @AILongClick(R.id.fragmentABtn1)
68     private void btnOnLongClick(View view){
69         if(view instanceof Button){
70             Toast.makeText(context, "btnOnLongClick: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
71         }
72     }
73 
74     @AIItemClick(R.id.fragmentGv)
75     private void gvOnItemClick(AdapterView<?> adapterView, View view, int i, long l){
76         Toast.makeText(context, "gvOnItemClick: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
77     }
78 
79     @AIItemLongClick(R.id.fragmentGv)
80     private void gvOnItemLongClick(AdapterView<?> adapterView, View view, int i, long l){
81         Toast.makeText(context, "gvOnItemLongClick: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
82     }
83 
84 
85 }
View Code

 

具体注解如下:

@AINoTitle: 类注解, 只适用于Activity(需继承于AIActivity), 设置Activity不显示Title

@AIFullScreen: 类注解, 只适用于Activity(需继承于AIActivity), 设置Activity全屏

@AILayout: 类注解
value[int]: 用于设置该Activity的布局 ---- setContentView(resId);


@AIView: 属性注解
id[int]: 用于绑定控件 ---- findViewById(resId);(default identifier[R.id.{field name}] if did not set id)
clickMethod[String]: 用于设置控件点击事件的回调方法, 可选, 方法名称任意, 参数必须为(View view)
longClickMethod[String]: 用于设置控件长按的回调方法, 可选, 方法名任意, 参数必须为(View view)
itemClickMethod[String]: 用于设置控件item点击的回调方法, 可选, 方法名任意, 参数必须为(AdapterView, View, int, long)
itemLongClickMethod[String]: 用于设置控件item长按的回调方法, 可选, 方法名任意, 参数必须为(AdapterView, View, int, long)

@AIBean: 属性注解, 为该属性生成一个对象并注入, 该对象必须有个默认的不带参数的构造方法

@AISystemService: 属性注解,为该属性注入系统服务对象


@AIClick: 方法注解
value[int[], 所要绑定控件的id]: 用于绑定控件点击事件的回调方法, 方法名称任意, 参数必须为(View view)

@AIItemClick: 方法注解
value[int[], 所要绑定控件的id]: 用于绑定控件item点击事件的回调方法, 方法名称任意, 参数必须为(AdapterView, View, int, long)

@AILongClick: 方法注解
value[int[], 所要绑定控件的id]: 用于绑定控件长按事件的回调方法, 方法名称任意, 参数必须为(View view)

@AIItemLongClick: 方法注解
value[int[], 所要绑定控件的id]: 用于绑定控件item长按事件的回调方法, 方法名称任意, 参数必须为(AdapterView, View, int, long)

 

 

 

posted @ 2013-12-05 11:17  天天_byconan  阅读(6886)  评论(0编辑  收藏  举报