Skip to content

Commit

Permalink
6.9.5 update
Browse files Browse the repository at this point in the history
- BaseFragment 支持 DataBinding,要使用请先在初始化时开启 `BaseFrameworkSettings.useDataBinding = true` 然后在 BaseActivity 上设置对应的 ViewBinding 泛型,例如 `MainFragment extends BaseBindingFragment<MainActivity, ActivityMainBinding>` 然后直接使用 `binding.` 即可。
  • Loading branch information
kongzue committed Feb 6, 2024
1 parent 7c7a5cf commit f2ba092
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.kongzue.baseframeworkdemo"
minSdkVersion 15
targetSdkVersion 30
versionCode 132
versionName "6.9.3"
versionCode 134
versionName "6.9.5"
}
buildTypes {
release {
Expand Down
4 changes: 2 additions & 2 deletions baseframework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 34
versionCode 133
versionName "6.9.4"
versionCode 134
versionName "6.9.5"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
* @link: http://kongzue.com/
* @describe: 自动化代码流水线作业,以及对原生安卓、MIUI、flyme的透明状态栏显示灰色图标文字的支持,同时提供一些小工具简化开发难度,详细说明文档:https://github.com/kongzue/BaseFramework
*/
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity implements SwipeBackActivityBase {
public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActivity implements SwipeBackActivityBase {

private LifeCircleListener lifeCircleListener; //快速管理生命周期
private static GlobalLifeCircleListener globalLifeCircleListener; //全局生命周期
Expand Down Expand Up @@ -136,7 +136,7 @@ public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActiv
private Bundle savedInstanceState;
private SwipeBackActivityHelper mHelper;

protected T binding;
protected VB binding;

@Override
@Deprecated
Expand Down Expand Up @@ -243,7 +243,7 @@ private View userDataBindingCreateLayout() {
// 通过反射实例化Binding对象
Class<?> bindingClass = Class.forName(bindingClassName);
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
binding = (T) inflateMethod.invoke(null, getLayoutInflater());
binding = (VB) inflateMethod.invoke(null, getLayoutInflater());

return binding.getRoot();
} catch (Exception e) {
Expand Down Expand Up @@ -517,7 +517,7 @@ public void finishActivity() {
/**
* initViews会在启动时首先执行,建议在此方法内进行布局绑定、View初始化等操作
*/
public abstract void initViews();
public void initViews(){};

/**
* initDatas会在布局加载后执行,建议在此方法内加载数据和处理布局显示数据
Expand Down Expand Up @@ -1834,4 +1834,9 @@ public BaseActivity setDarkStatusAndNavBarTheme(boolean dark) {
this.darkNavigationBarThemeValue = dark;
return this;
}

public BaseActivity setTranslucentNavBarBackground(){
setNavigationBarBackgroundRes(R.color.transparentNavigationBackgroundColor);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kongzue.baseframework;

import android.view.LayoutInflater;
import android.view.View;

import androidx.viewbinding.ViewBinding;

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

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

public BaseBindingFragment(VB binding) {
this.binding = binding;
}

public BaseBindingFragment() {
}

protected VB binding;

@Override
public void initViews() {

}

@Override
public View resetContentView() {
return userDataBindingCreateLayout();
}

@Override
public abstract void initDatas();

@Override
public abstract void setEvents();

private View userDataBindingCreateLayout() {
if (binding == null) {
String bindingClassName = getViewBindClassName();
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();
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.kongzue.baseframework.interfaces.ActivityResultCallback;
import com.kongzue.baseframework.interfaces.BindView;
Expand Down
2 changes: 2 additions & 0 deletions baseframework/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
<color name="white25">#40ffffff</color>
<color name="white20">#33ffffff</color>
<color name="white10">#1Affffff</color>

<color name="transparentNavigationBackgroundColor">#01000000</color>
</resources>

0 comments on commit f2ba092

Please sign in to comment.