public abstract class BaseDialog2 extends Dialog implements LifecycleObserver, LifecycleOwner {
protected Context context;
protected BaseBuilder builder;
private Unbinder unbinder;
protected boolean isFirstLoad = true;
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
private View contentView;
private WeakHashMap<Integer, Animation> animMap = new WeakHashMap<>();
public BaseDialog2(@NonNull Context context, BaseBuilder builder, @StyleRes int style) {
super(context, style);
this.context = context;
this.builder = builder;
BaseUtils.registerLifecycleEvent(context, this);
}
public BaseDialog2(@NonNull Context context, BaseBuilder builder) {
this(context, builder, com.quanyou.baselibrary.R.style.DialogTheme_Base);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
//提前设置Dialog的一些样式
if (builder != null) {
contentView = 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, contentView);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(builder.width, builder.height);
layoutParams.gravity = builder.gravity;
setContentView(contentView, layoutParams);
playAnimation(true, null);
Window dialogWindow = getWindow();
if (dialogWindow != null) {
WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
attributes.height = ViewGroup.LayoutParams.MATCH_PARENT;
dialogWindow.setAttributes(attributes);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialogWindow.setStatusBarColor(Color.TRANSPARENT);
}
dialogWindow.setBackgroundDrawable(builder.getBackgroundDrawable());
}
setCancelable(builder.cancelable);
setCanceledOnTouchOutside(builder.canceledOnTouchOutside);
initView(contentView);
initData();
}
}
@Override
public void cancel() {
playAnimation(false, new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
cancelImpl();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
protected void playAnimation(boolean isEnter, Animation.AnimationListener listener) {
Resources.Theme theme;
if (builder.animationStyle == null
|| contentView == null
|| context == null
|| (theme = context.getTheme()) == null) {
if (listener != null) {
listener.onAnimationEnd(null);
}
return;
}
try {
int index = isEnter ? 0 : 1;
Animation anim = animMap.get(index);
if (anim != null) {
playAnim(anim, listener);
return;
}
int[] systemAttrs = {android.R.attr.windowEnterAnimation, android.R.attr.windowExitAnimation};
TypedArray array = theme.obtainStyledAttributes(builder.animationStyle, systemAttrs);
int resourceId = array.getResourceId(index, 0);
if (resourceId > 0) {
Animation animation = AnimationUtils.loadAnimation(context, resourceId);
if (animation != null) {
animMap.put(index, animation);
playAnim(animation, listener);
return;
}
}
if (listener != null) {
listener.onAnimationEnd(null);
}
array.recycle();
} catch (Resources.NotFoundException e) {
if (listener != null) {
listener.onAnimationEnd(null);
}
}
}
protected void playAnim(Animation anim, Animation.AnimationListener listener) {
if (listener != null) {
anim.setAnimationListener(listener);
}
contentView.startAnimation(anim);
}
private void cancelImpl() {
if (contentView != null) {
contentView.clearAnimation();
}
animMap.clear();
super.cancel();
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (isShowing() && shouldCloseOnTouch(event)) {
onBackPressed();
return true;
}
return super.onTouchEvent(event);
}
private boolean shouldCloseOnTouch(MotionEvent event) {
return builder.canceledOnTouchOutside
&& contentView != null
&& (event.getAction() == MotionEvent.ACTION_UP
&& ViewUtils.isOutsideTheView(contentView, event)
|| event.getAction() == MotionEvent.ACTION_OUTSIDE);
}
public View getContentView() {
return contentView;
}
public abstract void initView(View inflate);
public abstract void initData();
public void refreshData() {
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)//和activity生命周期绑定后可用
public void destroy() {
cancel();
if (mLifecycleRegistry != null) {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
BaseUtils.unregisterLifecycleEvent(context, this);
if (unbinder != null && unbinder != Unbinder.EMPTY) {
unbinder.unbind();
unbinder = null;
}
if (builder != null) {
builder.destroy();
builder = null;
}
context = null;
contentView = null;
mLifecycleRegistry = null;
}
@Override
protected void onStart() {
super.onStart();
if (!isFirstLoad) {
refreshData();
}
isFirstLoad = false;
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
@Override
protected void onStop() {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
super.onStop();
}
}
BaseDialog
最新推荐文章于 2023-12-29 14:27:19 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)