写点什么

DialogFragment 探索与实现 (1),2021 年是做安卓开发人员的绝佳时机

用户头像
Android架构
关注
发布于: 13 小时前

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 ?WindowM


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


anager.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*/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;}


}


public class EverythingDialogHolder {


private final SparseArray<View> views;private final View convertView;private final LinkedHashSet<Integer> childClickViewIds;private final EverythingDialogFragment dialogFragment;


public EverythingDialogHolder(View convertView, EverythingDialogFragment dialogFragment) {this.convertView = convertView;this.views = new SparseArray<>();this.childClickViewIds =new LinkedHashSet<>();this.dialogFragment=dialogFragment;}


public static EverythingDialogHolder create(View view, EverythingDialogFragment dialogFragment) {return new EverythingDialogHolder(view,dialogFragment);}


public <T extends View> T findViewById(@IdRes int viewId) {View view = views.get(viewId);if (view == null) {view = convertView.findViewById(viewId);views.put(viewId, view);}return (T) view;

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
DialogFragment探索与实现(1),2021年是做安卓开发人员的绝佳时机