通常我们使用popUpwindow的时候例如都是写一个布局文件然后设置到popUpwindow的自定义布局当中,但有时候我们可能有这样的需求,底部的取消按钮是一定的,但是上面的选项不是写死的两个或者三个而是更加我们的需求动态来决定的,这个时候我们不可能去写死一个布局了,那么就需要我们自定义一个pop,动态添加同时要可以监听各项的点击事件。。。
先说一下思路,我们先定义一个basepopwindow,来抽象几个公用方法,然后通过子类来实现
具体代码:
A:抽象基类
public abstract class BasePopupWindow extends PopupWindow {
protected View popRootView;
public BasePopupWindow() {
super();
}
public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BasePopupWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BasePopupWindow(Context context) {
super(context);
}
public BasePopupWindow(int width, int height) {
super(width, height);
}
public BasePopupWindow(View contentView, int width, int height,
boolean focusable) {
super(contentView, width, height, focusable);
}
public BasePopupWindow(View contentView) {
super(contentView);
}
public BasePopupWindow(View contentView, int width, int height){
super(contentView, width, height,true);
this.popRootView = contentView;
setFocusable(true);
setOutsideTouchable(true);
setTouchable(true);
ColorDrawable dw = new ColorDrawable(0xb0000000);
this.setBackgroundDrawable(dw);
setAnimationStyle(R.style.AnimBottom);
initViews();
initEvents();
init();
}
public abstract void initViews();
public abstract void initEvents();
public abstract void init();
public View findViewById(int id) {
return popRootView.findViewById(id);
}
B: 实现子类:
里面加入我的一些说明:
public class CommBottomPopWindow extends BasePopupWindow implements
OnClickListener {
private Button cancleBtn;
private PopWindowListener listener;
private LinearLayout mLayout;
private Context mContext;
private boolean isHasSubTitle = false;
private LayoutInflater inflater;
/**
* 功能描述: 设置点击事件
* 〈功能详细描述〉
* 点击的自定义回调接口
*/
public void setPopListener(PopWindowListener listener) {
this.listener = listener;
}
public CommBottomPopWindow(Context context) {
//布局填充
super((LayoutInflater.from(context).inflate(
R.layout.comm_bottom_popwindow, null)),
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mContext = context;
}
/**
* 功能描述:初始化小标题
顶部是否需要提示的小标题
*/
public void initPopSubTitle(String notiTxt) {
mLayout.addView(createItem(notiTxt, true));
}
/**
* 功能描述: 初始化item
* 〈功能详细描述〉
动态添加的条目
*/
public void initPopItem(List list) {
if (list == null || list.size() == 0) {
return;
}
for (int i = 0; i < list.size(); i++) {
String title = list.get(i);
mLayout.addView(createItem(title, i, list.size()));
}
}
private View createItem(String itemTxt, boolean isSubTitle) {
return createItem(itemTxt, -1, -1, isSubTitle);
}
private View createItem(String itemTxt, final int index, int total) {
return createItem(itemTxt, index, total, false);
}
/**
* 功能描述: 创建item
* 〈功能详细描述〉
*
创建具体的条目
*/
private View createItem(String itemTxt, final int index, int total,
boolean isSubTitle) {
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.comm_bottom_popwindow_item, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.comm_popwindow_item_layout);
TextView textView = (TextView) view
.findViewById(R.id.comm_popwindow_item_txt);
textView.setText(itemTxt);
if (isSubTitle) {
isHasSubTitle = true;
layout.setBackgroundResource(R.drawable.selectpopwin_up);
textView.setTextColor(ResUtil.getColor(R.color.color_999999));
} else if (index == 0 && !isHasSubTitle) {
layout.setBackgroundResource(R.drawable.btn_selectpopwin_up);
} else if (index == total - 1) {
layout.setBackgroundResource(R.drawable.btn_selectpopwin_down);
} else {
layout.setBackgroundResource(R.drawable.btn_camp_selpopwin_center);
}
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (index == -1) {
return;
}
if (listener != null) {
listener.onPopSelected(index);
}
}
});
return view;
}
@Override
public void initViews() {
mLayout = (LinearLayout) findViewById(R.id.comm_bottom_popwindow_layout);
cancleBtn = (Button) findViewById(R.id.camp_pop_cancle);
isHasSubTitle = false;
}
@Override
public void initEvents() {
cancleBtn.setOnClickListener(this);
popRootView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
@Override
public void init() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.camp_pop_cancle:
dismiss();
break;
default:
break;
}
}
/**
* 功能描述: 显示pop window
* 〈功能详细描述〉
*/
public void show(View view) {
showAtLocation(view, Gravity.BOTTOM, 0, 0);
}
//回调接口定义
public interface PopWindowListener {
public void onPopSelected(int which);
}
C:贴下布局
comm_bottom_popwindow.xml
comm_bottom_popwindow_item.xml
ps 用法如下:
private void initPopWindow() {
if (mPopWindow == null) {
mPopWindow = new CommBottomPopWindow(getActivity());
List list = new ArrayList<>();
list.add("test1");
list.add("test2");
// 带显示小标题,可加可不加
mPopWindow.initPopSubTitle("sub title context");
mPopWindow.initPopItem(list);
mPopWindow.setPopListener(mPopListener);
}
// 显示pop window
mPopWindow.show(view);
}