便捷的創建Dialog
快速生成自定義彈窗
可鏈式
綁定activity/fragment生命週期
public abstract class BaseDialog<T, E> extends Dialog implements LifecycleObserver, LifecycleProvider<ActivityEvent> {
protected Context context;
protected Builder builder;
protected T confirmBtnClickListener;
private Unbinder unbinder;
protected E cancelBtnClickListener;
private final BehaviorSubject<ActivityEvent> mLifecycleSubject = BehaviorSubject.create();
private BaseDialog(@NonNull Context context, Builder builder, @StyleRes int style) {
super(context, style);
this.context = context;
this.builder = builder;
if (context instanceof LifecycleOwner) {
((LifecycleOwner) context).getLifecycle().addObserver(this);
}
}
public BaseDialog(@NonNull Context context, Builder builder) {
this(context, builder, com.quanyou.baselibrary.R.style.DialogTheme_Base);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//提前設置Dialog的一些樣式
if (builder != null) {
View inflate = LayoutInflater.from(context)
.inflate(CheckValueUtils.checkInteger(builder.layoutResId,
getClass().getSimpleName() + context.getString(com.quanyou.baselibrary.R.string.s_no_layout_id)), null);
unbinder = ButterKnife.bind(this, inflate);
setContentView(inflate);
Window dialogWindow = getWindow();
if (dialogWindow != null) {
dialogWindow.setGravity(builder.gravity);//設置dialog顯示居中
if (builder.animationStyle != null) {
dialogWindow.setWindowAnimations(builder.animationStyle);
}
WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
attributes.width = builder.with;
attributes.height = builder.height;
dialogWindow.setAttributes(attributes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialogWindow.setStatusBarColor(Color.TRANSPARENT);
}
}
setCancelable(builder.cancelable);
setCanceledOnTouchOutside(builder.canceledOnTouchOutside);
initView(inflate);
initData();
}
}
public abstract void initView(View inflate);
public abstract void initData();
public static class Builder {
private int gravity = Gravity.CENTER;
private @StyleRes
Integer animationStyle = null;
private int with = WindowManager.LayoutParams.WRAP_CONTENT;
private int height = WindowManager.LayoutParams.WRAP_CONTENT;
private boolean canceledOnTouchOutside = true;
private boolean cancelable = true;
private int layoutResId;
public Builder() {
}
public Builder(int with, int height) {
this.with = with;
this.height = height;
}
public Builder setGravity(int gravity) {
this.gravity = gravity;
return this;
}
public Builder setAnimation(@StyleRes Integer animationStyle) {
this.animationStyle = animationStyle;
return this;
}
public Builder setCancelable(boolean cancelable) {
this.cancelable = cancelable;
return this;
}
public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
this.canceledOnTouchOutside = canceledOnTouchOutside;
return this;
}
public Builder setWith(int with) {
this.with = with;
return this;
}
public Builder setHeight(int height) {
this.height = height;
return this;
}
public Builder setLayoutResId(@LayoutRes int layoutResId) {
this.layoutResId = layoutResId;
return this;
}
public void destroy() {
}
}
public BaseDialog setOnConfirmBtnClickListener(T listener) {
confirmBtnClickListener = listener;
return this;
}
public BaseDialog setOnCancelBtnClickListener(E listener) {
cancelBtnClickListener = listener;
return this;
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)//和activity生命週期綁定後可用
public void destroy() {
confirmBtnClickListener = null;
cancelBtnClickListener = null;
if (context instanceof LifecycleOwner) {
((LifecycleOwner) context).getLifecycle().removeObserver(this);
context = null;
}
if (unbinder != null && unbinder != Unbinder.EMPTY) {
unbinder.unbind();
unbinder = null;
}
if (builder != null) {
builder.destroy();
builder = null;
}
dismiss();
}
@NonNull
@Override
public Observable<ActivityEvent> lifecycle() {
return mLifecycleSubject.hide();
}
@NonNull
@Override
public <P> LifecycleTransformer<P> bindUntilEvent(@NonNull ActivityEvent event) {
return RxLifecycle.bindUntilEvent(mLifecycleSubject, event);
}
@NonNull
@Override
public <P> LifecycleTransformer<P> bindToLifecycle() {
return RxLifecycleAndroid.bindActivity(mLifecycleSubject);
}
@Override
public void dismiss() {
mLifecycleSubject.onNext(ActivityEvent.DESTROY);
super.dismiss();
}
@Override
public void show() {
super.show();
mLifecycleSubject.onNext(ActivityEvent.CREATE);
}
}
相關示例
仿支付寶密碼輸入彈窗:https://blog.csdn.net/TomCat0916/article/details/105776871