代码抽取
- 定义一个静态的工具类,工具类中有一个方法,主要是引入PopupWindow布局,以及PopupWindow的初始化工作,如下(数据比较多的时候迪调用这个方法):
public static PopupWindow mulPop;
private static TextView outTextView;
public static void mulItemPop(Context context,TextView navTvTitle,TextView outTextView,String title,String[] lists){
View customView = LayoutInflater.from(context).inflate(R.layout.layout_mul_popwindow, null);
PopwindowUtil.outTextView=outTextView;
ListView lvPop = (ListView) customView.findViewById(R.id.lv_pop);
((TextView) customView.findViewById(R.id.tv_pop_title)).setText(title);
lvPop.setAdapter(new CommonAdapter(context,lists));
mulPop = new PopupWindow(customView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
mulPop.update();
mulPop.setTouchable(true);
mulPop.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0xa0000000);
mulPop.setBackgroundDrawable(dw);
mulPop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
customView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (mulPop != null && mulPop.isShowing()) {
mulPop.dismiss();
mulPop = null;
}
return false;
}
});
mulPop.setAnimationStyle(R.style.popupwindow_anim_style);
mulPop.showAtLocation(navTvTitle, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
private static class CommonAdapter extends BaseAdapter {
private String[] lists;
private Context context;
public CommonAdapter(Context context,String[] lists) {
this.context=context;
this.lists = lists;
}
@Override
public int getCount() {
return lists.length;
}
@Override
public Object getItem(int position) {
return lists[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final CommonHolder holder;
if (convertView == null) {
holder = new CommonHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_pop, null);
holder.tv_item = (TextView) convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
} else {
holder = (CommonHolder) convertView.getTag();
}
holder.tv_item.setText(lists[position]);
holder.tv_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
outTextView.setText(lists[position]);
if (mulPop != null && mulPop.isShowing()) {
mulPop.dismiss();
mulPop = null;
}
}
});
return convertView;
}
}
private static class CommonHolder {
TextView tv_item;
}
其中参数navTvTitle是用于给PopupWindow设置位置的,outTextView是用于给弹框下面的界面设置数据的,title是PopupWindow的标题,lists是PopupWindow的数据。这个方法的逻辑就是先引入xml,然后对xml的控件初始化并且设置数据。
数据比较少时,封装的方法(3个以内)如:
public static PopupWindow littlePop;
private static TextView outTextView;
public static void littlePop(Context context, TextView navTvTitle, final TextView outTextView, String title, final String[] lists){
View customView = LayoutInflater.from(context).inflate(R.layout.layout_little_popwindow, null);
PopwindowUtil.outTextView=outTextView;
TextView tvContent1=(TextView)customView.findViewById(R.id.tv_content1);
TextView tvContent2=(TextView)customView.findViewById(R.id.tv_content2);
TextView tvContent3=(TextView)customView.findViewById(R.id.tv_content3);
if(lists.length==2){
tvContent1.setText(lists[0]);
tvContent2.setText(lists[1]);
tvContent3.setVisibility(View.GONE);
}else{
tvContent1.setText(lists[0]);
tvContent2.setText(lists[1]);
tvContent3.setText(lists[2]);
}
customView.findViewById(R.id.tv_content1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
outTextView.setText(lists[0]);
littlePop.dismiss();
}
});
customView.findViewById(R.id.tv_content2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
outTextView.setText(lists[1]);
littlePop.dismiss();
}
});
customView.findViewById(R.id.tv_content3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
outTextView.setText(lists[2]);
littlePop.dismiss();
}
});
((TextView) customView.findViewById(R.id.tv_pop_title)).setText(title);
littlePop = new PopupWindow(customView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
littlePop.update();
littlePop.setTouchable(true);
littlePop.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0xa0000000);
littlePop.setBackgroundDrawable(dw);
littlePop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
customView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (littlePop != null && littlePop.isShowing()) {
littlePop.dismiss();
littlePop = null;
}
return false;
}
});
littlePop.setAnimationStyle(R.style.popupwindow_anim_style);
littlePop.showAtLocation(navTvTitle, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}