反射初始化ViewBinding和ViewModel
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package com.quyunshuo.base.mvvm.v
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
@ -8,6 +9,7 @@ import androidx.viewbinding.ViewBinding
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.base.utils.EventBusRegister
|
||||
import com.quyunshuo.base.utils.EventBusUtils
|
||||
import java.lang.reflect.ParameterizedType
|
||||
|
||||
/**
|
||||
* @Author: QuYunShuo
|
||||
@ -15,19 +17,33 @@ import com.quyunshuo.base.utils.EventBusUtils
|
||||
* @Class: BaseFrameActivity
|
||||
* @Remark: Activity基类 与项目无关
|
||||
*/
|
||||
abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel>(private val vmClass: Class<VM>) :
|
||||
abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel> :
|
||||
AppCompatActivity() {
|
||||
|
||||
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||
ViewModelProvider(this).get(vmClass)
|
||||
//init ViewModel | getActualTypeArguments [0]=是第一个泛型参数 | [1] = 是类的第二个泛型参数
|
||||
val tClass: Class<VM> =
|
||||
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
|
||||
ViewModelProvider(this).get(tClass)
|
||||
}
|
||||
|
||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { initViewBinding() }
|
||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||
getViewBindingReflex()
|
||||
}
|
||||
|
||||
protected abstract fun initViewBinding(): VB
|
||||
protected abstract fun initView()
|
||||
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?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(mBinding.root)
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
package com.quyunshuo.base.mvvm.v;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
|
||||
import com.quyunshuo.base.databinding.MyActivityBinding;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author DBoy
|
||||
* @date 2020/9/26
|
||||
* Class 描述 :
|
||||
*/
|
||||
public class MyActivity extends MyBaseActivity<MyActivityBinding, MyViewModel> {
|
||||
|
||||
@Override
|
||||
protected MyActivityBinding getViewBinding(LayoutInflater from) {
|
||||
return MyActivityBinding.inflate(from);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initViewAndData() {
|
||||
mViewModel.test();
|
||||
}
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
package com.quyunshuo.base.mvvm.v;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.viewbinding.ViewBinding;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
|
||||
/**
|
||||
* @author DBoy
|
||||
* @date 2020/9/26
|
||||
* Class 描述 :
|
||||
*/
|
||||
public abstract class MyBaseActivity<V extends ViewBinding, M extends ViewModel> extends AppCompatActivity {
|
||||
|
||||
protected V viewBinding;
|
||||
|
||||
protected M mViewModel;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
viewBinding = getViewBinding(LayoutInflater.from(this));
|
||||
setContentView(viewBinding.getRoot());
|
||||
|
||||
//init ViewModel | getActualTypeArguments[0] =是第一个泛型参数 [1] = 是类的第二个泛型参数
|
||||
Class<M> tClass = (Class<M>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
|
||||
mViewModel = new ViewModelProvider(this).get(tClass);
|
||||
|
||||
initViewAndData();
|
||||
}
|
||||
|
||||
protected abstract V getViewBinding(LayoutInflater from);
|
||||
|
||||
protected abstract void initViewAndData();
|
||||
|
||||
}
|
||||
@ -10,5 +10,5 @@ import com.quyunshuo.base.mvvm.v.BaseFrameActivity
|
||||
* @Class: BaseActivity
|
||||
* @Remark: 项目相关的Activity基类
|
||||
*/
|
||||
abstract class BaseActivity<VB : ViewBinding, VM : ViewModel>(vmClass: Class<VM>) :
|
||||
BaseFrameActivity<VB, VM>(vmClass)
|
||||
abstract class BaseActivity<VB : ViewBinding, VM : ViewModel> :
|
||||
BaseFrameActivity<VB, VM>()
|
||||
@ -65,6 +65,9 @@ android {
|
||||
kotlinOptions {
|
||||
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||
}
|
||||
buildFeatures {
|
||||
viewBinding = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@ -22,7 +22,7 @@
|
||||
android:theme="@style/base_AppTheme"
|
||||
tools:ignore="UnusedAttribute">
|
||||
<activity
|
||||
android:name="com.quyunshuo.base.mvvm.v.MyActivity"
|
||||
android:name=".MyActivity"
|
||||
android:screenOrientation="portrait"
|
||||
android:theme="@style/base_AppTheme">
|
||||
<intent-filter>
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
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,4 +1,4 @@
|
||||
package com.quyunshuo.base.mvvm.v;
|
||||
package com.quyunshuo.androidbaseframemvvm;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
<?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:layout_height="match_parent"
|
||||
android:background="#fff">
|
||||
|
||||
<TextView
|
||||
android:text="my"
|
||||
android:gravity="center"
|
||||
android:id="@+id/test_Tv"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="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