仿照Xutils通过ioc实现findviewbyId,点击事件等
- 注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ViewById {
//ViewById(R.id.xx)
int value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Onclick {
int[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CheckNet {
}
- 工具类
public class ViewFinder {
private Activity mActivity;
private View mView;
public ViewFinder(Activity mActivity) {
this.mActivity = mActivity;
}
public ViewFinder(View mView) {
this.mView = mView;
}
public View findViewById(int viewId){
return mActivity != null?mActivity.findViewById(viewId):mView.findViewById(viewId);
}
}
public class NetworkUtil {
/**
* 检查网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (manager == null) {
return false;
}
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable()) {
return false;
}
return true;
}
}
- 最后
public class ViewUtils {
public static void inject(Activity activity){
inject(new ViewFinder(activity),activity);
}
/**
* 自定义view
* @param view
*/
public static void inject(View view){
inject(new ViewFinder(view),view);
}
/**
* fragment
* @param view
* @param object
*/
public static void inject(View view,Object object){
inject(new ViewFinder(view),object);
}
/**
* 兼容上面的方法
* @param viewFinder
* @param object 反射需要执行的类
*/
public static void inject(ViewFinder viewFinder,Object object){
injectFiled(viewFinder,object);
injectEvent(viewFinder,object);
}
private static void injectFiled(ViewFinder viewFinder, Object object) {
Class<?> clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
ViewById viewById = field.getAnnotation(ViewById.class);
if (viewById != null){
int viewId = viewById.value();
View view = viewFinder.findViewById(viewId);
if (view == null)continue;
//能够注入私有属性
field.setAccessible(true);
try {
field.set(object,view);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
private static void injectEvent(ViewFinder viewFinder, Object object) {
Class<?> clazz = object.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
Onclick onclick = method.getAnnotation(Onclick.class);
if (onclick == null)continue;
int[] viewIds = onclick.value();
for (int viewId : viewIds) {
View view = viewFinder.findViewById(viewId);
if (view != null){
//检测网络
boolean isCheckNet = method.getAnnotation(CheckNet.class) != null;
method.setAccessible(true);
view.setOnClickListener(new DeclareOnclickListener(method,object,isCheckNet));
}
}
}
}
private static class DeclareOnclickListener implements View.OnClickListener{
private Method mMethod;
private Object mObject;
private boolean mIsCheckNet;
public DeclareOnclickListener(Method method, Object object, boolean isCheckNet) {
this.mMethod = method;
this.mObject = object;
this.mIsCheckNet = isCheckNet;
}
@Override
public void onClick(View v) {
if (mIsCheckNet){
//如果没网
if (!NetworkUtil.isNetworkAvailable(v.getContext())){
Toast.makeText(v.getContext(), "没网亲", Toast.LENGTH_SHORT).show();
return;
}
}
try {
mMethod.invoke(mObject,v);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
用法
public class MainActivity extends AppCompatActivity {
@ViewById(R.id.btn)
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
User user = new User();
BeanUtils.set(user);
button.setText(user.getName());
}
@CheckNet
@Onclick({R.id.btn,R.id.btn2})
private void onClick(View view){
switch (view.getId()){
case R.id.btn:
Toast.makeText(this, "666666666", Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Toast.makeText(this, "777777777777", Toast.LENGTH_SHORT).show();
break;
}
}
}