Skip to content

Commit

Permalink
add InjectNoView
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonLiuChn authored and MasonLiuChn committed Nov 25, 2018
1 parent f845e85 commit ac67163
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ repositories {
}
}
dependencies {
compile 'com.masonliu:arrow:1.1.5'
compile 'com.masonliu:arrow:1.1.7'
}
```

Expand All @@ -180,6 +180,7 @@ Arrow.inject(Activity object);
Arrow.injectFragmentOnCreateView(Object fragment, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Arrow.injectNoView(Object target)
Arrow.injectView(Object target)
Arrow.injectView(Object target,View view)
```


Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gmavenGitRepo {
gmavenLibrary {
group = 'com.masonliu'
artifactId = 'arrow'
version = '1.1.6'
version = '1.1.7'
packaging = 'aar'
description = "description"
}
6 changes: 4 additions & 2 deletions library/src/main/java/com/masonliu/arrow/Arrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ public static void injectView(Object target) {
} else if (target instanceof Fragment || isV4Fragment(target)) {
InjectViewHandler.inject(target);
OnClickHandler.inject(target);
} else if (target instanceof View) {
InjectViewHandler.inject(target);
}
}

public static void injectView(Object target,View view) {
InjectViewHandler.inject(target,view);
}

public static View injectFragmentOnCreateView(Object fragment, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return ContentViewHandler.inject(fragment, inflater, container, savedInstanceState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public static void inject(Object target) {
}
}

public static void inject(Object target, View view) {
//target.getClass() 是单例,fieldInfosMap.containsKey 判断的是hashcode
if (!fieldInfosMap.containsKey(target.getClass())) {
fieldInfosMap.put(target.getClass(), getFieldInfos(target.getClass()));
}
for (FieldInfo fieldInfo : fieldInfosMap.get(target.getClass())) {
Field field = fieldInfo.getField();
if (field.isAnnotationPresent(InjectView.class)) {
setField(target, view, field);
}
}
}


private static void setField(Object target, Field field) {
try {
InjectView annotation = field.getAnnotation(InjectView.class);
Expand Down Expand Up @@ -61,4 +75,14 @@ private static void setField(Object target, Field field) {
}
}

private static void setField(Object target, View viewParent, Field field) {
try {
InjectView annotation = field.getAnnotation(InjectView.class);
View view = viewParent.findViewById(annotation.value());
field.setAccessible(true);
field.set(target, view);
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 comments on commit ac67163

Please sign in to comment.