ButterKnife
ButterKnife的优势
强大的View绑定事件和资源文件的绑定
使用的便捷性上,剔除了原始绑定时候的复杂性
由于大多数的注解都是在编译期,所以不会影响程序运行的效率
代码清晰,可读性相对来说要强很多
ButterKnife的使用
- 导入依赖:(项目的build.gradle 中)
classpath 'com.jakewharton:butterknife-gradle-plugin:10.0.0'
classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta02'(运行时异常导入)
//在项目的build.gradle 中添加如下代码:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.jakewharton:butterknife-gradle-plugin:10.0.0' //我在这里
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
- 在module的build.grale中添加如下代码:
首行添加: apply plugin: 'com.jakewharton.butterknife'
android中添加: (defaultConfig 下面):
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
- 添加依赖: (dependencies 中)
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
ButterKnife注解添加:
- 声明时使用@BindView注解
@BindView(R.id.btn1)
Button btn1;
@BindView(R.id.tv1)
TextView tv;
- 在onCreate方法中绑定
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
- 单独创建点击事件方法,使用@OnClick({R.id.tv1, R.id.btn1})注解, 括号中的数组指明的是哪个组件响应点击
@OnClick({R.id.tv1,R.id.btn1})
public void Onclick(View view){
switch(view.getId()){
case R.id.tv1:
tv.setText("终于点到了");
break;
case R.id.btn1:
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
break;
}
}
- Fragment中使用绑定方法不同, 并且需要在onDestroyView中解除
Unbinder unbinder;
@BindView(R.id.btn2)
Button but2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frazgmentlayout1,container,false);
unbinder= ButterKnife.bind(this,view);
return view;
}
/**
* onDestroyView中进行解绑操作
*/
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
- ButterKnife插件的添加: 可以快速绑定
File -> Settings -> Plugins -> 搜索ButterKnife,找到Android ButterKnife Zeleany进行安装重启AndroidStudio
//
//
//
//
Dagger2
Dagger2是什么?
Dagger2是Dagger的升级版,是一个依赖注入框架,第一代由大名鼎鼎的Square公司共享出来,第二代则是由谷歌接手后推出的,现在由Google接手维护.
依赖
依赖是指某个模块A要实现某个功能需要其他模块B。
最原始也最简单的的方式是在模块内new一个B的对象出来,这样会造成B的构造函数修改,需要修改A中的代码,两者耦合度非常高。
依赖注入
依赖注入是不在A中去创建 B的实例,而是让上层调用者注入一个B的对象,这样可以让模块A不再因为B的构造方法的修改而改变
dagger2基本使用1
dagger2导包
implementation"com.google.dagger:dagger:2.16"
annotationProcessor"com.google.dagger:dagger-compiler:2.16"
dagger2基本使用
- 创建注入提供者类
- 提供者创建构造方法
- 给构造方法添加@Inject注解,表明对外提供依赖,并写个方法
public class Utils1 {
@Inject
//提供给其他类使用
public Utils1(){
}
public void show(){
Log.e("########","utils1---show");
}
}
- 在接收注解方,创建提供注解类的对象,并添加@Inject注解, 表明这个对象需要注入
@Inject
Utils1 utils1; //使用其他类
- 创建注解的桥梁,或者叫注入者。注入者是一个接口,需要用@Component注解,并且提供一个方法用来表示向谁注入。
@Component
//提供桥梁
public interface Utils1Compont {
void inject(Main2Activity main2Activity);
}
-
锤它, 如果不报错,就能在以下路径找到想要的内容
\app\build\generated\source\apt\debug\com.example.test\di\DaggerUtils1Compont.java -
在调用类中加载Dagger,然后就可以调用了
DaggerUtils1Compont.builder().build().inject(this);
utils1.show();//可以调用此方法了
/
dagger2基本使用2
1, 2, 3, 4同基本使用1中的1-4
5. 创建连接仓库类, 仓库类需要@Module来注解表示这是仓库
@Module
public class UserModule {
public UserModule() {
}
@Provides
public Utils1 useUtils() {
Log.e("######","仓库show");
return new Utils1();
}
}
- 带仓库的dagger也需要一个接口桥梁, 这个桥梁注解需要参数
@Component(modules={UserModule.class})
@Component(modules={UserModule.class})
public interface Utils1CompontWithModule {
//提供桥梁
void injectWithmodule(MainActivity mainActivity);
}
- 锤它
- 在调用类中加载Dagger,然后就可以调用了,加载Dagger时需要将仓库带
DaggerUtils1CompontWithModule.builder().userModule(new UserModule()).build().injectWithmodule(this);
dagger的局限
- Dagger不会自动注入字段。
- 它不能注入私有字段。
- 如果要使用字段注入,则必须在带@Component注释的接口中定义一个方法,该方法接受要注入成员变量的类的实例。