Android 基础弹框DataBinding+DialogFragment的使用封装
针对目前弹框的使用比较频繁,现在动手封装一个弹框的基类,使用DataBinding+DialogFragment,希望能够帮到大家。
1.onCreate()方法中添加Style.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL,R.style.BaseDialogStyle);
}
BaseDialogStyle代码如下(大家根据自己的需求进行修改):
<style name="BaseDialogStyle" parent="Theme.MaterialComponents.DayNight.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowTranslucentStatus">true</item><!-- 解决全屏时状态栏变黑 -->
<item name="android:backgroundDimEnabled">true</item> <!-- 是否允许背景模糊 -->
<item name="android:windowIsTranslucent">true</item> <!-- 是否半透明 -->
<item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 -->
<item name="android:windowContentOverlay">@null</item> <!-- 是否有遮盖 -->
<item name="android:backgroundDimAmount">0.8</item> <!-- 设置背景模糊的透明度-->
</style>
-
弹框核心代码:
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { mContext = getActivity(); if (getDialog()!=null) getDialog().setCanceledOnTouchOutside(isOnTouchOutSide); viewBinding = DataBindingUtil.inflate(inflater,layoutResId(),container,false); viewBinding.setLifecycleOwner(this); initData(); return viewBinding.getRoot(); }
3.在onStart()方法中可以动态设置宽高和透明度以及弹框的位置(上下左右):
@Override
public void onStart() {
super.onStart();
setSite(viewSite);
}
private void setSite(int site) {
Window mWindow = Objects.requireNonNull(getDialog()).getWindow();
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.width = windowWidth;
mLayoutParams.height = windowHeight;
mLayoutParams.gravity = site;
mWindow.setAttributes(mLayoutParams);
mWindow.setDimAmount(alpha);// 0~1 , 1表示完全昏暗
}
4.根据自己的需求封装toast,例如:
protected void showToast(CharSequence msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
}
protected void showToast(@StringRes int msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
}
5.完整代码如下:
@SuppressLint("ValidFragment")
public abstract class BaseDialog<D extends ViewDataBinding> extends DialogFragment {
protected D viewBinding;
protected Context mContext;
private int viewSite = Gravity.CENTER;//布局位置
private boolean isOnTouchOutSide = false;
private float alpha = 0.8f;
private int windowWidth = ViewGroup.LayoutParams.MATCH_PARENT;
private int windowHeight = ViewGroup.LayoutParams.MATCH_PARENT;
public BaseDialog() {
super();
}
/**
* 是否点击外部消失
* @param isOnTouchOutSide
*/
public BaseDialog(boolean isOnTouchOutSide) {
this.isOnTouchOutSide = isOnTouchOutSide;
}
/**
* 布局位置
* @param site
*/
public BaseDialog(int site) {
viewSite = site;
}
public BaseDialog(int site, boolean isOnTouchOutSide) {
this(isOnTouchOutSide);
viewSite = site;
}
public BaseDialog(float alpha, int windowWidth, int windowHeight) {
this.alpha = alpha;
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
}
public BaseDialog(int site, int windowWidth, int windowHeight) {
this.viewSite = site;
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
}
protected abstract @LayoutRes
int layoutResId();
protected abstract void initData();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL,R.style.BaseDialogStyle);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
mContext = getActivity();
if (getDialog()!=null){
getDialog().setCanceledOnTouchOutside(isOnTouchOutSide);
//禁止返回关闭弹框可在此处处理
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
// @Override
// public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// if (keyCode == 4) return true;
// Log.i(TAG, "onKey: "+keyCode);
// return false;
// }
// });
// }
}
viewBinding = DataBindingUtil.inflate(inflater,layoutResId(),container,false);
viewBinding.setLifecycleOwner(this);
initData();
return viewBinding.getRoot();
}
public boolean isOnTouchOutSide() {
return isOnTouchOutSide;
}
public BaseDialog setOnTouchOutSide(boolean onTouchOutSide) {
isOnTouchOutSide = onTouchOutSide;
if (getDialog() != null) {
getDialog().setCanceledOnTouchOutside(onTouchOutSide);
}
return this;
}
public int getViewSite() {
return viewSite;
}
public BaseDialog setViewSite(int viewSite) {
this.viewSite = viewSite;
setSite(viewSite);
return this;
}
@Override
public void onStart() {
super.onStart();
setSite(viewSite);
}
private void setSite(int site) {
Window mWindow = Objects.requireNonNull(getDialog()).getWindow();
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.width = windowWidth;
mLayoutParams.height = windowHeight;
mLayoutParams.gravity = site;
mWindow.setAttributes(mLayoutParams);
mWindow.setDimAmount(alpha);// 0~1 , 1表示完全昏暗
}
protected void showToast(CharSequence msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
// ToastUtils.showShort(mContext,msg);
}
protected void showToast(@StringRes int msg){
Toast.makeText(mContext,msg, Toast.LENGTH_SHORT).show();
// ToastUtils.showShort(mContext,msg);
}
最后,希望对大家有所帮助,如果有不对的地方,欢迎大家指正。谢谢!