Retrolambda
Lambda表达式是一种可用于创建委托或表达式目录树类型的匿名函数。AndroidStudio配置:
build.gradle(Project)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.3.1'
}
}
build.gradle(Module)
apply plugin: 'com.android.application' //or apply plugin: 'java'
apply plugin: 'me.tatarka.retrolambda'
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
语法:
input -> body
input类型:
无参:void() -> body
单参:x -> body
多参:(x, y) -> body
显型:(int x, int y) -> body
body类型:
空体:input -> {}
单行:input -> x + y
多行无返回:input -> {x++; y++;}
多行有返回:input -> {x++; y++; return x + y;}
Butterknife
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.AndroidStudio配置:
build.gradle(Module)
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
或则:Ctrl + Alt + Shift + S
使用方法:
Activity: ButterKnife.bind(this);
Fragment: ButterKnife.bind(this, rootView);
private Unbinder mUnbinder; mUnbinder = ButterKnife.bind(this); mUnbinder.unbind();
注意:
1、方法不能为private or static
2、参数可选
3、setContentView()不能注解
绑定属性:
@BindView(R.id.tv_show_main)
TextView mShowView;
@Nullable @BindInt()
@BindFloat()
@BindBool()
@BindString()
@BindArray()
@BindDrawable()
@BindBitmap()
@BindColor()
@BindDimen()
@BindViews()
绑定方法:
@OnClick(R.id.submit)
public void submit(View view) {
view.setText("Binded");
}
@OnTextChanged(value = R.id.test, callback = OnTextChanged.BEFORE_TEXT_CHANGED)
void beforeTextChanged(CharSequences s, int start, int count, int after) {
}
@BindViews({R.id.first_name, R.id.last_name})
List<EditText> nameViews;
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
@Override
public void apply(View view, int index) {
view.setEnabled(false);
}
};
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
@Override
public void set(View view, Boolean value, int index) {
view.setEnabled(value);
}
};
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
// TODO ...
}
TextView firstName = ButterKnife.findById(view, R.id.first_name);
RxBinding
RxBinding是Rx中处理控件异步调用的方式, 也是由Square公司开发, Jake负责编写. 通过绑定组件, 异步获取事件, 并进行处理.RxView.clicks(mShowView).subscribe(this::onShowClicked);
RxSnackbar.dismiss().subscribe();
RxTextView.textChanges().subscribe();
EventBus
EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。EventBus.getDefault().register(this);
public void onEventMainThread(LocalEvent event);
EventBus.getDefault().unregister(this);
EventBus.getDefault().post(new LocalEvent("hello"));
onEvent() 发布消费同一线程
onEventMainThread() UI线程消费
onEventBackgoundThread() 子线程消费,不是UI线程才会创建
onEventAsync() 子线程消费,每次都创建
Publisher-post(Event)->EventBus-onEvent(Event)->Subscribers