Skip to content

Commit

Permalink
7.0.4.1
Browse files Browse the repository at this point in the history
- 尝试使用新方法获取ViewBinding对象;
  • Loading branch information
kongzue committed Nov 26, 2024
1 parent e12614f commit 69189de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
2 changes: 1 addition & 1 deletion baseframework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
minSdkVersion 15
targetSdkVersion 34
versionCode 140
versionName "7.0.4"
versionName "7.0.4.1"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.kongzue.baseframework.util.JumpParameter;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseBindingActivity<VB extends ViewBinding> extends BaseActivity{
public abstract class BaseBindingActivity<VB extends ViewBinding> extends BaseActivity {

protected VB binding;

Expand All @@ -30,21 +32,17 @@ public View resetContentView() {
}

private View userDataBindingCreateLayout() {
String className = getClass().getSimpleName();
if (className.endsWith("Activity")) {
className = className.substring(0, className.length() - 8);
}
String bindingClassName = getPackageName() + ".databinding.Activity" + className + "Binding";

try {
// 通过反射实例化Binding对象
Class<?> bindingClass = Class.forName(bindingClassName);
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
binding = (VB) inflateMethod.invoke(null, getLayoutInflater());

return binding.getRoot();
} catch (Exception e) {
e.printStackTrace();
Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superclass;
Type type = parameterizedType.getActualTypeArguments()[0];
try {
Class<VB> clazz = (Class<VB>) type;
binding = (VB) clazz.getMethod("inflate", getLayoutInflater().getClass()).invoke(null, getLayoutInflater());
return binding.getRoot();
} catch (Exception e) {
throw new RuntimeException("ViewBinding creation failed", e);
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseBindingFragment<ME extends BaseActivity, VB extends ViewBinding> extends BaseFragment<ME> {

Expand Down Expand Up @@ -36,30 +37,18 @@ public View resetContentView() {
public abstract void setEvents();

private View userDataBindingCreateLayout() {
if (binding == null) {
String bindingClassName = getViewBindClassName();
Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) superclass;
Type type = parameterizedType.getActualTypeArguments()[1];
try {
// 通过反射实例化Binding对象
Class<?> bindingClass = Class.forName(bindingClassName);
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
binding = (VB) inflateMethod.invoke(null, getLayoutInflater());
Class<VB> clazz = (Class<VB>) type;
binding = (VB) clazz.getMethod("inflate", getLayoutInflater().getClass()).invoke(null, getLayoutInflater());
return binding.getRoot();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("ViewBinding creation failed", e);
}
return null;
} else {
return binding.getRoot();
}
}

private String getViewBindClassName() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
String type = genericSuperclass.getActualTypeArguments()[1].toString();
if (type.contains(" ")) {
String[] splitType = type.split(" ");
type = splitType[splitType.length - 1];
}
return type;
return null;
}
}

0 comments on commit 69189de

Please sign in to comment.