Modify: 基类调整,不再使用构造方法,采用反射进行初始化
This commit is contained in:
@ -1,7 +1 @@
|
|||||||
<manifest package="com.quyunshuo.base"
|
<manifest package="com.quyunshuo.base" />
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
|
||||||
|
|
||||||
<application>
|
|
||||||
|
|
||||||
</application>
|
|
||||||
</manifest>
|
|
||||||
@ -20,30 +20,22 @@ import java.lang.reflect.ParameterizedType
|
|||||||
abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel> :
|
abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel> :
|
||||||
AppCompatActivity() {
|
AppCompatActivity() {
|
||||||
|
|
||||||
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
|
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||||
//init ViewModel | getActualTypeArguments [0]=是第一个泛型参数 | [1] = 是类的第二个泛型参数
|
val vbClass: Class<VB> =
|
||||||
val tClass: Class<VM> =
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
|
||||||
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
|
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
|
||||||
ViewModelProvider(this).get(tClass)
|
inflate.invoke(null, layoutInflater) as VB
|
||||||
}
|
}
|
||||||
|
|
||||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||||
getViewBindingReflex()
|
val vmClass: Class<VM> =
|
||||||
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
|
||||||
|
ViewModelProvider(this).get(vmClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract fun initView()
|
protected abstract fun initView()
|
||||||
protected abstract fun initViewObserve()
|
protected abstract fun initViewObserve()
|
||||||
|
|
||||||
/**
|
|
||||||
* 反射初始化ViewBinding
|
|
||||||
*/
|
|
||||||
private fun getViewBindingReflex(): VB {
|
|
||||||
val tClass: Class<VB> =
|
|
||||||
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
|
|
||||||
val infater = tClass.getMethod("inflate", LayoutInflater::class.java)
|
|
||||||
return infater.invoke(null,layoutInflater) as VB
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(mBinding.root)
|
setContentView(mBinding.root)
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import androidx.viewbinding.ViewBinding
|
|||||||
import com.alibaba.android.arouter.launcher.ARouter
|
import com.alibaba.android.arouter.launcher.ARouter
|
||||||
import com.quyunshuo.base.utils.EventBusRegister
|
import com.quyunshuo.base.utils.EventBusRegister
|
||||||
import com.quyunshuo.base.utils.EventBusUtils
|
import com.quyunshuo.base.utils.EventBusUtils
|
||||||
|
import java.lang.reflect.ParameterizedType
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: QuYunShuo
|
* @Author: QuYunShuo
|
||||||
@ -18,16 +19,17 @@ import com.quyunshuo.base.utils.EventBusUtils
|
|||||||
* @Class: BaseFrameFragment
|
* @Class: BaseFrameFragment
|
||||||
* @Remark: Fragment基类 与项目无关
|
* @Remark: Fragment基类 与项目无关
|
||||||
*/
|
*/
|
||||||
abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel>(private val vmClass: Class<VM>) :
|
abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel> :
|
||||||
Fragment() {
|
Fragment() {
|
||||||
|
|
||||||
|
protected lateinit var mBinding: VB
|
||||||
|
|
||||||
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
|
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||||
|
val vmClass: Class<VM> =
|
||||||
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
|
||||||
ViewModelProvider(this).get(vmClass)
|
ViewModelProvider(this).get(vmClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { initViewBinding() }
|
|
||||||
|
|
||||||
protected abstract fun initViewBinding(): VB
|
|
||||||
protected abstract fun initView()
|
protected abstract fun initView()
|
||||||
protected abstract fun initViewObserve()
|
protected abstract fun initViewObserve()
|
||||||
|
|
||||||
@ -36,6 +38,10 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel>(private val v
|
|||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View? {
|
): View? {
|
||||||
|
val vbClass: Class<VB> =
|
||||||
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
|
||||||
|
val inflate = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
|
||||||
|
mBinding = inflate.invoke(null, layoutInflater, container, false) as VB
|
||||||
return mBinding.root
|
return mBinding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
package com.quyunshuo.base.mvvm.v
|
package com.quyunshuo.base.mvvm.v
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.viewbinding.ViewBinding
|
import androidx.viewbinding.ViewBinding
|
||||||
import com.alibaba.android.arouter.launcher.ARouter
|
import com.alibaba.android.arouter.launcher.ARouter
|
||||||
import com.quyunshuo.base.utils.EventBusRegister
|
import com.quyunshuo.base.utils.EventBusRegister
|
||||||
import com.quyunshuo.base.utils.EventBusUtils
|
import com.quyunshuo.base.utils.EventBusUtils
|
||||||
|
import java.lang.reflect.ParameterizedType
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: QuYunShuo
|
* @Author: QuYunShuo
|
||||||
@ -15,9 +17,12 @@ import com.quyunshuo.base.utils.EventBusUtils
|
|||||||
*/
|
*/
|
||||||
abstract class BaseFrameNotMVVMActivity<VB : ViewBinding> : AppCompatActivity() {
|
abstract class BaseFrameNotMVVMActivity<VB : ViewBinding> : AppCompatActivity() {
|
||||||
|
|
||||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { initViewBinding() }
|
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||||
|
val vbClass: Class<VB> =
|
||||||
protected abstract fun initViewBinding(): VB
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
|
||||||
|
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
|
||||||
|
inflate.invoke(null, layoutInflater) as VB
|
||||||
|
}
|
||||||
protected abstract fun initView()
|
protected abstract fun initView()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import androidx.viewbinding.ViewBinding
|
|||||||
import com.alibaba.android.arouter.launcher.ARouter
|
import com.alibaba.android.arouter.launcher.ARouter
|
||||||
import com.quyunshuo.base.utils.EventBusRegister
|
import com.quyunshuo.base.utils.EventBusRegister
|
||||||
import com.quyunshuo.base.utils.EventBusUtils
|
import com.quyunshuo.base.utils.EventBusUtils
|
||||||
|
import java.lang.reflect.ParameterizedType
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: QuYunShuo
|
* @Author: QuYunShuo
|
||||||
@ -18,9 +19,8 @@ import com.quyunshuo.base.utils.EventBusUtils
|
|||||||
*/
|
*/
|
||||||
abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
|
abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
|
||||||
|
|
||||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { initViewBinding() }
|
protected lateinit var mBinding: VB
|
||||||
|
|
||||||
protected abstract fun initViewBinding(): VB
|
|
||||||
protected abstract fun initView()
|
protected abstract fun initView()
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@ -28,6 +28,10 @@ abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
|
|||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View? {
|
): View? {
|
||||||
|
val vbClass: Class<VB> =
|
||||||
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
|
||||||
|
val inflate = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
|
||||||
|
mBinding = inflate.invoke(null, layoutInflater, container, false) as VB
|
||||||
return mBinding.root
|
return mBinding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,5 +10,4 @@ import com.quyunshuo.base.mvvm.v.BaseFrameActivity
|
|||||||
* @Class: BaseActivity
|
* @Class: BaseActivity
|
||||||
* @Remark: 项目相关的Activity基类
|
* @Remark: 项目相关的Activity基类
|
||||||
*/
|
*/
|
||||||
abstract class BaseActivity<VB : ViewBinding, VM : ViewModel> :
|
abstract class BaseActivity<VB : ViewBinding, VM : ViewModel> : BaseFrameActivity<VB, VM>()
|
||||||
BaseFrameActivity<VB, VM>()
|
|
||||||
@ -10,5 +10,4 @@ import com.quyunshuo.base.mvvm.v.BaseFrameFragment
|
|||||||
* @Class: BaseFragment
|
* @Class: BaseFragment
|
||||||
* @Remark: 项目相关的Fragment基类
|
* @Remark: 项目相关的Fragment基类
|
||||||
*/
|
*/
|
||||||
abstract class BaseFragment<VB : ViewBinding, VM : ViewModel>(vmClass: Class<VM>) :
|
abstract class BaseFragment<VB : ViewBinding, VM : ViewModel> : BaseFrameFragment<VB, VM>()
|
||||||
BaseFrameFragment<VB, VM>(vmClass)
|
|
||||||
@ -65,9 +65,6 @@ android {
|
|||||||
kotlinOptions {
|
kotlinOptions {
|
||||||
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||||
}
|
}
|
||||||
buildFeatures {
|
|
||||||
viewBinding = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@ -21,15 +21,7 @@
|
|||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/base_AppTheme"
|
android:theme="@style/base_AppTheme"
|
||||||
tools:ignore="UnusedAttribute">
|
tools:ignore="UnusedAttribute">
|
||||||
<activity
|
|
||||||
android:name=".MyActivity"
|
|
||||||
android:screenOrientation="portrait"
|
|
||||||
android:theme="@style/base_AppTheme">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MAIN" />
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
|
||||||
<!-- 屏幕适配基准DP -->
|
<!-- 屏幕适配基准DP -->
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="design_width_in_dp"
|
android:name="design_width_in_dp"
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
package com.quyunshuo.androidbaseframemvvm;
|
|
||||||
|
|
||||||
|
|
||||||
import com.quyunshuo.androidbaseframemvvm.databinding.MyActivityLayoutBinding;
|
|
||||||
import com.quyunshuo.base.mvvm.v.BaseFrameActivity;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author DBoy
|
|
||||||
* @date 2020/9/26
|
|
||||||
* Class 描述 :
|
|
||||||
*/
|
|
||||||
public class MyActivity extends BaseFrameActivity<MyActivityLayoutBinding, MyViewModel> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void initView() {
|
|
||||||
getMViewModel().test();
|
|
||||||
getMBinding().testTv.setText("反射初始化 ViewBinding");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void initViewObserve() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
package com.quyunshuo.androidbaseframemvvm;
|
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import androidx.lifecycle.ViewModel;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author DBoy
|
|
||||||
* @date 2020/9/26
|
|
||||||
* Class 描述 :
|
|
||||||
*/
|
|
||||||
public class MyViewModel extends ViewModel {
|
|
||||||
|
|
||||||
public void test(){
|
|
||||||
Log.e("DJC", "AAA");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:background="#fff">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/test_Tv"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:gravity="center"
|
|
||||||
android:textColor="#000"
|
|
||||||
android:textSize="20sp" />
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
||||||
Reference in New Issue
Block a user