通用对话弹窗CommonDialog
通用对话弹窗CommonDialog
Version 1.0
Created by chenchangjun on 18/1/12.
- 抽离普通基类见
BaseNormalDialog - 优化扩展方式 见
3.1 基本扩展 - 添加必要注释

项目中,对话框,种类繁多, 难以维护, 有的继承popwindow, 有的继承dialog, 有的继承dialogFragment....而且写法 各不相同.

效果图

调用
//通用弹框 生产类
public class DialogCreator {
/**
* 无标题,单btn弹窗
*
* @param mActivity
* @param msg
* @param strLeft
* @param leftListener
*/
public static BaseDialog showConfirmDialog(Context mActivity, String msg, String strLeft, OnLeftListener leftListener) {
CommonNormalDialog confrimDialog = new CommonNormalDialog(mActivity);
confrimDialog.setDialogType(DialogConfig.TYPE_HAS_NO_HEADER)
.setContent(msg)
.setContentGravity(DialogConfig.TYPE_GRIVITY_CENTER)
.setConfirmBtn(strLeft, leftListener)
.start();
return confrimDialog;
}
/**
* 无标题,单btn弹窗
*
* @param mActivity
* @param msg
* @param strLeft
* @param leftListener
*/
public static BaseDialog showTitleConfirmDialog(Context mActivity, String title, String msg, String strLeft, OnLeftListener leftListener) {
CommonNormalDialog confrimDialog = new CommonNormalDialog(mActivity);
confrimDialog.setDialogType(DialogConfig.TYPE_HAS_HEADER)
.setTitle(title)
.setContent(msg)
.setContentGravity(DialogConfig.TYPE_GRIVITY_CENTER)
.setConfirmBtn(strLeft, leftListener)
.start();
return confrimDialog;
}
....
}
思路
1.定义顶级抽象类 BaseDialog,
装饰AlertDialog, 统一dialog样式, 并且用 模板方法模式 进行抽象约束 子类型为.
为了达到 易扩展,代码复用的角度, 将整体dialog,根据 功能区域 布局和加载分为3部分----
- 头部header: 扩展头部区域,像图片logo头部,带背景,大标题.
- 中间content: 主内容显示区域,比如输入框,内容详情等等.
- 底部footer:底部扩展区,主要用来放置 确认按钮等等.
/**
* 子类必须实现该方法用于显示在界面上的Dialog的Title部分
*
* @return title部分的显示的View,返回的titleView为null的话,Dialog将不显示title部分
*/
protected abstract View initHeader();
/**
* 子类必须实现该方法用于显示在界面上的正文部分控件
* @return content部分显示的内容,如果返回null,将会抛出异常
*/
protected abstract View initContent();
/**
* 子类实现该方法用于显示底部的view
*
* @return bottom部分显示的view,如果返回null,bottom部分将不显示
*/
protected abstract View initBottom();
/**
* 模板方法
*/
protected abstract void loadHeader();
protected abstract void loadContent();
protected abstract void loadBottom();
public void start(){
loadHeader();
loadContent();
loadBottom();
};
2. 新建子类,CommonNormalDialog CommonImgHeaderDialog CommonLogoHeaderDialog等
子类 分别对模板方法进行实现, 以CommonNormalDialog为例
/**
* 普通样式无头部
* @return
*/
@Override
protected View initHeader() {
return null;
}
/**
* 创建内容布局
* @return 内容布局
*/
@Override
protected View initContent() {
View content = View.inflate(context, R.layout.common_dialog_content, null);
tv_title = (TextView) content.findViewById(R.id.tv_title);
tv_content = (TextView) content.findViewById(R.id.tv_content);
etv_content = (EditText) content.findViewById(R.id.etv_content);
v_vertical_line = content.findViewById(R.id.v_vertical_line);
tv_left = (TextView) content.findViewById(R.id.tv_left);
tv_right = (TextView) content.findViewById(R.id.tv_right);
iv_cancel = (ImageView) content.findViewById(R.id.iv_content_cancel);
iv_cancel.setOnClickListener(this);
return content;
}
/**
* 创建 底部 按钮布局
* @return
*/
@Override
protected View initBottom() {
View footer = View.inflate(context, R.layout.common_dialog_footer_btn, null);
v_vertical_line = footer.findViewById(R.id.v_vertical_line);
tv_left = (TextView) footer.findViewById(R.id.tv_left);
tv_right = (TextView) footer.findViewById(R.id.tv_right);
return footer;
3.扩展
3.1 普通扩展
如果想要 用如下的 弹窗样式. 那么只需要简单几步就可以实现.

3.1.1 学习并继承 NormalBaseDialog,并实现其中的三个抽象方法 即可.
/*************************构造子类的内容布局***********************/
/**
* 构造 实现类 内容布局
* @return
*/
public abstract View createContentView();
/**
* 处理 实现类的 view数据
* @param parents view 的ViewGroup容器
* @param v view本身
*/
public abstract void handleContentView(ViewGroup parents, View v);
/**
* 子类view 监听器
* @param v 事件分发 view
*/
public abstract void onContentClick(View v);
3.1.2 举例说明 在app module中, 有MyDialogDialog ,清爽的代码~ .
/**
* 自定义样式
* Created by chenchangjun on 17/10/11.
*/
public class MyDialogDialog extends NormalBaseDialog implements View.OnClickListener {
private ImageView card_pic;
private TextView card_price;
private TextView card_title;
/**
* 构造 实现类 内容布局
*
* @return
*/
@Override
public View createContentView() {
View myView = View.inflate(context, R.layout.dialog_content_focus_dialog_v1, null);
card_pic= (ImageView) myView.findViewById(R.id.card_pic);
card_price= (TextView) myView.findViewById(R.id.card_price);
card_title= (TextView) myView.findViewById(R.id.card_title);
card_pic.setOnClickListener(this);
return myView;
}
/**
* 处理 实现类的 view数据
*
* @param parents view 的ViewGroup容器
* @param v view本身
*/
@Override
public void handleContentView(ViewGroup parents, View v) {
card_pic.setImageResource(R.mipmap.ic_launcher_round);
}
/**
* 子类view 监听器
*
* @param v 事件分发 view
*/
@Override
public void onContentClick(View v) {
int i = v.getId();
if (i == R.id.card_pic) {
Toast.makeText(context,"hello MyDialog!!!!",Toast.LENGTH_SHORT).show();
}
}
public MyDialogDialog setMyView(String st) {
card_title.setText(st);
return this;
}
public MyDialogDialog(Context context) {
super(context);
}
}
3.2 其他扩展
如果想要扩展, 根据需求相互扩展即可.
比如 CommonImgHeaderDialog 可以选择继承CommonNormalDialog.共用CommonNormalDialog的 内容布局和 底部布局. 所以,只需要加载header布局即可.
/**
* 通用 带头部背景 样式
* Created by chenchangjun on 17/10/11.
*/
public class CommonImgHeaderDialog extends CommonNormalDialog implements View.OnClickListener {
private String url_header;
private ImageView iv_header_cancel;
private SimpleDraweeView iv_header;
@Override
protected void loadHeader() {
if (type== DialogConfig.TYPE_HAS_HEADER){
iv_header.setVisibility(View.VISIBLE);
iv_header_cancel.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(url_header)) {
iv_header.setVisibility(View.GONE);
} else {
//iv_header.setImageURI(url_header);
iv_header.setBackgroundResource(R.drawable.common_dialog_header_bg);
Uri uri = Uri.parse(url_header);
iv_header.setImageURI(uri);
}
}
}
public CommonImgHeaderDialog setHeaderUrl(String str) {
url_header = str;
return this;
}
public CommonImgHeaderDialog(Context context) {
super(context);
}
@Override
protected View initHeader() {
View header=null;
if (type==DialogConfig.TYPE_HAS_NO_HEADER){
return null;
}
header = View.inflate(context, R.layout.common_dialog_pic_header, null);
iv_header_cancel = (ImageView) header.findViewById(R.id.iv_header_cancel);
iv_header = (SimpleDraweeView) header.findViewById(R.id.iv_header);
iv_header_cancel.setOnClickListener(this);
return header;
}
}
4. 后期优化
1.优化为一个组件库 发布到 JCenter来使用
2.兼容更多的样式处理
3.精简lib中依赖的处理第三方库......通用对话弹窗CommonDialog
注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权
通用对话弹窗CommonDialog的更多相关文章
- 项目期复习:JS操作符,弹窗与调试,凝视,数据类型转换
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/huangyibin628/article/details/26364901 1.JS操作符 ① 除法 ...
- tkinter 基础教程
目录 介绍 模块 导入方式 API 使用 主窗口 运行窗口 组件列表介绍 Label 标签 Button 按钮 Options 属性选项 文本框 Entry 单行文本框 Text 多行文本框 文本框属 ...
- gulp工作流
小屁活动使用 gulp+less gulpfile.js var gulp = require('gulp'), cssmin = require('gulp-minify-css'), less = ...
- JS基础题
1.三目运算符(三元条件语句)的使用方法? 条件表达式?true表达式:false表达式 2.JS数据中哪些属于引用类型? 数组.对象 引用类型变量,变量名所储存的不是变量值,而是变量所在的地址. 3 ...
- 2017-12-22 日语编程语言"抚子"-第三版实现初探
前文日语编程语言"抚子" - 第三版特色初探仅对语言的语法进行了初步了解. 之前的语言原型实现尝试(如编程语言试验之Antlr4+JavaScript实现"圈4" ...
- 网页设计编辑利器——jQuery EasyUI所学整理(待编辑)
1, Messager弹窗信息 方法: $.messager.alert(...), 在网页中间弹出一个窗口 $.messager.confirm(...) 弹出一个确认窗口, 有确定和取消两个按钮, ...
- Teaching Machines to Understand Us 让机器理解我们 之一 引言
Teaching Machines to Understand Us By Tom Simonite MIT Technology Review Vol.118 No.5 2015 让机器理解我 ...
- 低于0.01%的极致Crash率是怎么做到的?
WeTest 导读 看似系统Bug的Crash 99%都不是系统问题!本文将与你一起探索Crash分析的科学方法. 在移动互联网闯荡多年的iOS手机管家,经过不断迭代创新,已经涵盖了隐私(加密相册). ...
- PB常用事件
1.window中的事件 事件名 触发的时机 01.Activate 在窗口激活之前触发 02.Clicked 当用户用 ...
随机推荐
- 如何防止Android反编译
转自: http://my.eoe.cn/sandking/archive/19772.html http://www.cnblogs.com/zdz8207/archive/2012/01/28/d ...
- ObjectInputStream&ObjectOutputStream工具类
序列化:将数据保存到文件:ObjectOutputStream; 反序列化:将文件中的数据显示出来:ObjectInputStream; 在反序列化程序中运行后能够正常输出Person的相关信息, ...
- JMeter乱码问题的解决
一.JMeter返回数据是乱码 解决办法是: 在JMeter安装路径的bin目录下,以记事本打开文件jmeter.properties, 找到Sampleresult.default.encoding ...
- 51nod 1090 3个数和为0【二分】
1090 3个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从 ...
- java中的BigInteger
头文件 import java.io.*; import java.math.*; 读入 Scanner cin = Scann(System.in); while(cin.hasNext()) &l ...
- bzoj 3864: Hero meet devil
bzoj3864次元联通们 第一次写dp of dp (:з」∠) 不能再颓废啦 考虑最长匹配序列匹配书转移 由于dp[i][j]的转移可由上一行dp[i-1][j-1],dp[i-1][j],dp[ ...
- 【bzoj4443】【[Scoi2015]小凸玩矩阵】二分+二分图最大匹配
(上不了p站我要死了,侵权度娘背锅) Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或同一列,现小凸 ...
- symbol(s) not found for architecture arm64
问题如下: 解决:更改环境 ok Standard architectures (armv7, arm7s)
- 【转载】GCC 预处理器选项
预处理器选项(Preprocessor Option) 下列选项针对C预处理器,预处理器用在正式编译以前,对C 源文件进行某种处理. 如果指定了`-E'选项, GCC只进行预处理工作.下面的某些选项必 ...
- EDM邮件群发十大技巧提升邮件群发效果
有很多人抱怨现在邮件群发没有什么效果,其实不然,每一种推广方式都有他的优势,没有看到效果说明你没有掌握好方法.个人觉得EDM邮件群发的优势在于传播速度快.不受地域限制.不受时间限制.邮件内容能够多元化 ...