写点什么

DialogFragment 探索与实现,音视频编解码技术

用户头像
Android架构
关注
发布于: 刚刚

}


@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (theme != 0){setStyle(DialogFragment.STYLE_NORMAL, theme);}}


@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {if (mRootView==null){mRootView = inflater.inflate(layoutResID, container, false);setDialogSize(widthPercentSize, heightPercentSize);}return mRootView;}


@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);mDialog = getDialog();


holder = EverythingDialogHolder.create(mRootView,this);if (viewConvertListener!=null){viewConvertListener.convertView(holder,this);}if (holder!=null && viewIds!=null && viewIds.length>0){holder.addChildClickListener(viewIds);}


if (mDialog != null) {mDialog.setCanceledOnTouchOutside(touchOutside);mDialog.setOnKeyListener((dialog, keyCode, event) -> cancel);Window window = mDialog.getWindow();window.setWindowAnimations(animations);}}


/**


  • 其他区域是否点击消失

  • @param touchOutside

  • @return*/public EverythingDialogFragment setCanceledOnTouchOutside(boolean touchOutside){this.touchOutside=touchOutside;return this;}


public EverythingDialogFragment setWindowAnimations(@StyleRes int resId) {this.animations=resId;return this;}


/**


  • 屏蔽返回键

  • @param cancel

  • @return*/public EverythingDialogFragment setCanceledOnTouch(boolean cancel){this.cancel=cancel;return this;}


/**


  • 设置 Style

  • @param theme

  • @return*/public EverythingDialogFragment setStyle(int theme){this.theme=theme;return this;}


/**


  • 设置布局

  • @param layoutResID

  • @return*/public EverythingDialogFragment setContentView(@LayoutRes int layoutResID) {this.layoutResID = layoutResID;return this;}


/**


  • 设置显示的宽

  • @param widthPercentSize

  • @return*/public EverythingDialogFragment setWidthPercentSize(float widthPercentSize) {this.widthPercentSize = widthPercentSize;return this;}


/**


  • 显示高

  • @param heightPercentSize

  • @return*/public EverythingDialogFragment setHeightPercentSize(float heightPercentSize) {this.heightPercentSize = heightPercentSize;return this;}


private void setFragmentManager(FragmentManager manager) {try {manager.beginTransaction().remove(this).commitAllowingStateLoss();super.show(manager, getClass().getSimpleName());} catch (Exception e) {e.printStackTrace();}}


public EverythingDialogFragment show(Object obj){if (obj!=null){if (obj instanceof FragmentActivity){FragmentActivity activity = (FragmentActivity) obj;setFragmentManager(activity.getSupportFragmentManager());}else if (obj instanceof Fragment){Fragment fragment = (Fragment) obj;setFragmentManager(fragment.getFragmentManager());}else {throw new IllegalArgumentException("not is FragmentManager");}}return this;}


/**


  • 设置显示屏幕大小*/private void setDialogSize(float widthPercent, float heightPercent) {DisplayMetrics dm = new DisplayMetrics();FragmentActivity activity = getActivity();Dialog dialog = getDialog();if (activity != null && dialog != null) {activity.getWindowManager().getDefaultDisplay().getMetrics(dm);Window window = dialog.getWindow();if (window != null) {window.getDecorView().setPadding(0, 0, 0, 0);WindowManager.LayoutParams lp = window.getAttributes();


lp.width = widthPercent == 0 ?WindowManager.LayoutParams.WRAP_CONTENT : widthPercent == 1 ?WindowManager.LayoutParams.MATCH_PARENT : (int) (dm.widthPixels * widthPercent);


lp.height = heightPercent == 0 ?WindowManager.LayoutParams.WRAP_CONTENT : heightPercent == 1 ?WindowManager.LayoutParams.MATCH_PARENT : (int) (dm.heightPixels * heightPercent);


window.setAttributes(lp);window.setBackgroundDrawable(new ColorDrawable());}}}


/**


  • 可以暴露所有的 view*/public interface ConvertViewListener{void convertView(EverythingDialogHolder holder, final EverythingDialogFragment dialog);}


private ConvertViewListener viewConvertListener;


public EverythingDialogFragment setConvertViewListener(ConvertViewListener l){viewConvertListener=l;return this;}


private OnChildClickListener childClickListener;


public OnChildClickListener getOnChildClickListener() {return childClickListener;}


/**


  • 设置子 View Listener*/public EverythingDialogFragment setOnChildClickListener(OnChildClickListener l, @IdRes final int ...viewIds){childClickListener=l;addIdRes(viewIds);return this;}


/**


  • 设置子 View Listener


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


*/public EverythingDialogFragment setOnChildClickListener(OnChildClickListener l){childClickListener=l;return this;}


/**


  • 子 view 添加

  • @param viewIds

  • @return*/public EverythingDialogFragment addIdRes(@IdRes final int ...viewIds){this.viewIds=viewIds;return this;}

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
DialogFragment探索与实现,音视频编解码技术