MyBase
This commit is contained in:
@ -1 +1,7 @@
|
||||
<manifest package="com.quyunshuo.base" />
|
||||
<manifest package="com.quyunshuo.base"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
@ -0,0 +1,25 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
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();
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package com.quyunshuo.base.mvvm.v;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
12
Lib_Base/src/main/res/layout/my_activity.xml
Normal file
12
Lib_Base/src/main/res/layout/my_activity.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<?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">
|
||||
|
||||
<TextView
|
||||
android:text="my"
|
||||
android:gravity="center"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user