通过上篇《十分钟学习建造者模式》相信大家对建造者有了一个基本的认识了,但有些人会有疑问,对于建造者模式,怎么灵活运用到项目中,有时候我也会有这样的体会,对于一个知识点或者技术,看了一遍后就觉得会了,但却不知道怎么灵活运用,才疏学浅,只是分享下自己一些微薄的见解,如有错误,还请多多留言指教。
不多说了,直奔主题,先说一个需求,在我目前手里的两个项目里面,都有大量的popupwindow,这些popupwindow都是大同小异,有的是一个按钮,有的是两个按钮,有的有标题,有的没有标题,本人比较懒,不太喜欢重复造轮子,当有同样代码多次调用的时候,我就不想拷贝了,我坚信会有更好更优雅的方式去解决,嗯,现在先看一串代码,如下:
/**
* created by zero on 2016-08-19
*/
public class BuilderActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(drawView());
}
// 不想写xml,就直接动态编码了
private View drawView() {
// TODO Auto-generated method stub
LayoutParams layout_params = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout ll_layout = new LinearLayout(this);
ll_layout.setOrientation(LinearLayout.VERTICAL);
ll_layout.setLayoutParams(layout_params);
LayoutParams btn_params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
btn_params.gravity = Gravity.CENTER_HORIZONTAL;
Button btn1 = new Button(this);
btn1.setLayoutParams(btn_params);
btn1.setText("click1");
btn1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CommonPopup.Builder(BuilderActivity.this)
.setContent("真的要删除历史记录吗?").setRightText("确定")
.setListenRight(new IListenRightCallBack()
{
@Override
public void rightListen() {
// TODO Auto-generated method stub
Toast.makeText(BuilderActivity.this, "right1",
Toast.LENGTH_LONG).show();
}
}).build().showPopup();
}
});
Button btn2 = new Button(this);
btn2.setLayoutParams(btn_params);
btn2.setText("click2");
btn2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CommonPopup.Builder(BuilderActivity.this)
.setTitle("提示")
.setContent("真的要删除历史记录吗?")
.setLeftText("取消")
.setListenLeft(new IListenLeftCallBack()
{
@Override
public void leftListen() {
// TODO Auto-generated method stub
Toast.makeText(BuilderActivity.this, "left2",
Toast.LENGTH_LONG).show();
}
}).setRightText("确定")
.setListenRight(new IListenRightCallBack()
{
@Override
public void rightListen() {
// TODO Auto-generated method stub
Toast.makeText(BuilderActivity.this, "right2",
Toast.LENGTH_LONG).show();
}
}).build().showPopup();
}
});
ll_layout.addView(btn1);
ll_layout.addView(btn2);
return ll_layout;
}
}
建造者
/**
*
* created by zero on 2016-08-19
*
* 通过建造者模式构建一个popupwindow
*
*/
public class CommonPopup extends PopupWindow
{
private final Context context;
private final String title;
private final String content;
private final IListenLeftCallBack iListenLeft;
private final IListenRightCallBack iListenRight;
private final String leftText;
private final String rightText;
public static class Builder{
private final Context context;
private String title;
private String content;
private IListenLeftCallBack iListenLeft;
private IListenRightCallBack iListenRight;
private String leftText;
private String rightText;
public Builder(Context context)
{
this.context = context;
}
public Builder setTitle(String title){
this.title = title;
return this;
}
public Builder setContent(String content){
this.content = content;
return this;
}
public Builder setListenLeft(IListenLeftCallBack iListenLeft){
this.iListenLeft = iListenLeft;
return this;
}
public Builder setLeftText(String leftText){
this.leftText = leftText;
return this;
}
public Builder setListenRight(IListenRightCallBack iListenRight){
this.iListenRight = iListenRight;
return this;
}
public Builder setRightText(String rightText){
this.rightText = rightText;
return this;
}
public CommonPopup build(){
return new CommonPopup(this);
}
}
public CommonPopup(Builder builder)
{
this.context = builder.context;
this.title = builder.title;
this.content = builder.content;
this.iListenLeft = builder.iListenLeft;
this.iListenRight = builder.iListenRight;
this.leftText = builder.leftText;
this.rightText = builder.rightText;
initPopup();
}
private PopupWindow popupWindow;
private View popView;
private TextView txt_title;
private TextView txt_info;
private void initPopup(){
popView = LayoutInflater.from(context).inflate(R.layout.popup_common_view, null);
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
popupWindow = new PopupWindow(popView, dm.widthPixels, dm.heightPixels);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
txt_title = (TextView) popView.findViewById(R.id.txt_title);
if(TextUtils.isEmpty(title)){
txt_title.setVisibility(View.GONE);
}else{
txt_title.setText(title);
}
txt_info = (TextView) popView.findViewById(R.id.txt_info);
if(TextUtils.isEmpty(content)){
txt_info.setVisibility(View.GONE);
}else{
txt_info.setText(content);
}
Button btn_right = (Button) popView.findViewById(R.id.btn_right);
if(!TextUtils.isEmpty(rightText)){
btn_right.setText(rightText);
btn_right.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
if(iListenRight!=null){
iListenRight.rightListen();
}
}
});
}else{
btn_right.setVisibility(View.GONE);
}
Button btn_left = (Button) popView.findViewById(R.id.btn_left);
if(!TextUtils.isEmpty(leftText)){
btn_left.setText(leftText);
btn_left.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
if(iListenLeft!=null){
iListenLeft.leftListen();
}
}
});
}else{
btn_left.setVisibility(View.GONE);
}
}
public void showPopup(){
if(popupWindow==null){
initPopup();
}
popupWindow.showAtLocation(popView, 0, 0, 0);
}
}
接口类
public interface IListenLeftCallBack
{
void leftListen();
}
public interface IListenRightCallBack
{
void rightListen();
}
方便点击按钮后接收回调,可以在左右键做自己想要的操作。
看了这些代码,是不是瞬间想到了AlertDialog,对于很多教程讲到builder的时候,都是用AlertDialog,这也确实是builder模式的典型案例,但是有时候一些比较复杂的页面,AlertDialog还是实现不了,我们还是需要popupwindow去实现,就比如以下的popupwindow
在我们的那个产品里面,这个类似效果的popupwindow还有很多,有的也是两个按钮,有的也有标题。。。
上述是对建造者模式的简单使用,下一篇会再进一步讲解建造者模式,敬请期待,O(∩_∩)O哈哈~