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

时间:2021-02-26 12:51:12

作为一个移动应用开发者,随着需求的日益增多,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;
} }

Fragment中使用:

 package com.wangjie.androidinject;

 import android.app.AlarmManager;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.wangjie.androidinject.annotation.annotations.*;
import com.wangjie.androidinject.annotation.present.AISupportFragment;
import com.wangjie.androidinject.model.Person; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created with IntelliJ IDEA.
* Author: wangjie email:wangjie@cyyun.com
* Date: 13-12-4
* Time: 下午4:37
*/
@AILayout(R.layout.fragment_a)
public class FragmentA extends AISupportFragment{ @AIView
private Button fragmentABtn1; @AIView
private GridView fragmentGv; @AIBean
private Person person; @AISystemService
private AlarmManager alarmManager; @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(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", "fragment_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});
fragmentGv.setAdapter(adapter); person.setName("androidInject");
person.setAge(1);
System.out.println(person.toString()); System.out.println("alarmManager: " + alarmManager); } @AIClick(R.id.fragmentABtn1)
private void btnOnclick(View view){
if(view instanceof Button){
Toast.makeText(context, "btnOnclick: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
}
} @AILongClick(R.id.fragmentABtn1)
private void btnOnLongClick(View view){
if(view instanceof Button){
Toast.makeText(context, "btnOnLongClick: " + ((Button)view).getText(), Toast.LENGTH_SHORT).show();
}
} @AIItemClick(R.id.fragmentGv)
private void gvOnItemClick(AdapterView<?> adapterView, View view, int i, long l){
Toast.makeText(context, "gvOnItemClick: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
} @AIItemLongClick(R.id.fragmentGv)
private void gvOnItemLongClick(AdapterView<?> adapterView, View view, int i, long l){
Toast.makeText(context, "gvOnItemLongClick: " + ((Map<String, String>)adapterView.getAdapter().getItem(i)).get("title"), Toast.LENGTH_SHORT).show();
} }

具体注解如下:

@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)