public class PopupWindow extends PopupWindow{
private View conentView;
private ListView popLv;
private Window window;
private WindowManager.LayoutParams layoutParams;
private Activity context;
private int type=0;
public PopupWindow(final Activity context) {
this.context=context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
conentView = inflater.inflate(R.layout.activity_popupwindow_list, null);
//屏幕的宽和高
int height = context.getWindowManager().getDefaultDisplay().getHeight();
int width = context.getWindowManager().getDefaultDisplay().getWidth();
// 设置SelectPicPopupWindow的View
this.setContentView(conentView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(width / 2 -80);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
this.setOutsideTouchable(true);
// 刷新状态
this.update();
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(/*context.getResources().getColor(R.color.white)*/0000000000);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(dw);
//设置窗口的透明度
lp =context.getWindow().getAttributes();
lp.alpha=0.7f;//0.0为黑色,1.0透明
context.getWindow().setAttributes(lp);
//设置pop隐藏时背景变成透明
this.setOnDismissListener(new poponDismissListener());
popLv= (ListView) conentView.findViewById(R.id. lv);
imageList.add(R.mipmap.ic);
textViewList.add("111");
imageList.add(R.mipmap.ic);
textViewList.add("2222");
popLv.setAdapter(new PopupWindowAdapter(context,imageList,textViewList));
popLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
PopupWindow.this.dismiss();
}else if (position==1){
context.startActivity(new Intent(UIUtils.getContext(), XXX.class));
PopupWindow.this.dismiss();
lp.alpha=1.0f;
context.getWindow().setAttributes(lp);
}
}
});
}
WindowManager.LayoutParams lp;
//调用此方法即可parent是显示在那个控件下
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 10);
} else {
this.dismiss();
context.getWindow().setAttributes(lp);
}
}
class PopupWindowAdapter extends BaseAdapter {
private Context context;
private List<Integer> imageList;
private List<String> textViewList;
PopupWindowAdapter(Context context,List<Integer> imageList,List<String> textViewList){
this.context=context;
this.imageList=imageList;
this.textViewList=textViewList;
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHoldr viewHoldr;
if (convertView==null){
viewHoldr=new ViewHoldr();
convertView= LayoutInflater.from(context).inflate(R.layout.item_ popupwindow,null);
viewHoldr.popImage= (ImageView) convertView.findViewById(R.id.pop _image);
viewHoldr.popTv= (TextView) convertView.findViewById(R.id.pop_tv);
convertView.setTag(viewHoldr);
}else{
viewHoldr = (ViewHoldr)convertView.getTag();
}
viewHoldr.popImage.setImageResource(imageList.get(position));
viewHoldr.popTv.setText(textViewList.get(position));
return convertView;
}
class ViewHoldr{
private ImageView popImage;
private TextView popTv;
}
}
}
PopupWindow自定义类
最新推荐文章于 2021-05-25 14:03:56 发布