使用DialogFragment实现对话框
2018-05-26 本文已影响0人
flycode
Talk is Cheap.
public abstract class BaseDialog extends DialogFragment {
protected View rootView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDialogStyle();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// 设置圆角需要
Window window = getDialog().getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
getDialog().setCanceledOnTouchOutside(getCanceledOnTouchOutside());
setCancelable(getCancelable());
rootView = inflater.inflate(getLayoutId(), container, false);
initView();
setListener();
return rootView;
}
@Override
public void onStart() {
super.onStart();
// 重置宽高
Window window = getDialog().getWindow();
if (window != null) {
window.setLayout(getWidth(), getHeight());
}
}
/**
* 显示Dialog
*/
public void show(FragmentManager fragmentManager) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment pre = fragmentManager.findFragmentByTag(getFragmentTag());
if (pre != null) {
transaction.remove(pre);
}
// onSaveInstance后commit无法提交
try {
show(transaction, getFragmentTag());
} catch (IllegalStateException e) {
// 容错处理,不做操作
}
}
/**
* 显示Dialog
*/
public void show(@NonNull FragmentActivity activity) {
show(activity.getSupportFragmentManager());
}
/**
* 避免莫名其妙的空指针
* onSaveInstance后commit无法提交
*/
@Override
public void dismiss() {
if (getFragmentManager() == null) {
return;
}
dismissAllowingStateLoss();
}
protected abstract int getLayoutId();
protected abstract void initView();
protected abstract void setListener();
protected abstract String getFragmentTag();
/**
* override
* 配置对话框风格
*/
protected void setDialogStyle() {
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
}
/**
* override
* 配置点击对话框外部是否消失
*/
protected boolean getCanceledOnTouchOutside() {
return false;
}
/**
* override
* 配置返回键是否可以关闭对话框
*/
protected boolean getCancelable() {
return true;
}
/**
* override
* 配置宽度
*/
protected int getWidth() {
return ViewGroup.LayoutParams.WRAP_CONTENT;
}
/**
* override
* 配置高度
*/
protected int getHeight() {
return ViewGroup.LayoutParams.WRAP_CONTENT;
}
}