标签:
最近有做fragment里弹出自定义popupWindow,
fragment里面调用:
// 点击加号按钮
@Click
protected void ll_add_pharmacy() {
mPopTempList.showAsDropDown(ll_add_pharmacy, 0, getActivity()); // 传给popupWindow getActivity();
mPopTempList.setOnClickListener(new OnOKClickListener() {
@Override
public void onOKClick(int which) {
switch (which) {
case 1: // 添加药品
Intent intent = new Intent();
// ONEALLBACTH 代表单品或批量添加 标识1代表单品
intent.putExtra("ONEALLBACTH", "1");
intent.setClass(getActivity(), AnnotationClassUtil.get(AddNewPharmacyActivity1.class));
getActivity().startActivityForResult(intent, MainActivity.CONNECT_GUN);
break;
}
}
});
}
自定义popupWindow:
package com.hospital.widget;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import com.hospital.R;
/**
*
* @ClassName: PopSelectPharmacy
* @Description: TODO
* @author wuc
*
*/
public class PopSelectPharmacy implements View.OnClickListener{
/**
* int which
* 1.添加药品 2.库存提醒 3.搜索药品
* @author Achen
*
*/
public static interface OnOKClickListener {
public void onOKClick(int which);
}
//生命接口对象
private OnOKClickListener mClickListener;
//设置监听器 也就是实例化接口
public void setOnClickListener(final OnOKClickListener clickListener) {
this.mClickListener = clickListener;
}
private Context context;
private PopupWindow popupWindow;
private TextView tv_addDrug, tv_storageRemind, tv_searchDrug;
private LayoutInflater inflater;
private ImageView iv_yfgl_addXiaoHongDianKuCun ;
public ImageView getIv_yfgl_addXiaoHongDianKuCun() {
return iv_yfgl_addXiaoHongDianKuCun;
}
public void setIv_yfgl_addXiaoHongDianKuCun(
ImageView iv_yfgl_addXiaoHongDianKuCun) {
this.iv_yfgl_addXiaoHongDianKuCun = iv_yfgl_addXiaoHongDianKuCun;
}
public PopSelectPharmacy(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pop_selectpharmacy, null);
tv_addDrug = (TextView) view.findViewById(R.id.tv_addDrug);
tv_storageRemind = (TextView) view.findViewById(R.id.tv_storageRemind);
tv_searchDrug = (TextView) view.findViewById(R.id.tv_searchDrug);
iv_yfgl_addXiaoHongDianKuCun = (ImageView) view.findViewById(R.id.iv_yfgl_addXiaoHongDianKuCun);
tv_addDrug.setOnClickListener(this);
tv_storageRemind.setOnClickListener(this);
tv_searchDrug.setOnClickListener(this);
popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)
popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.add_drugbackground));
}
// 下拉式 弹出 pop菜单 parent 右下角
public void showAsDropDown(View parent, String key, Activity activity) {
showAsDropDown(parent, 0, activity);
}
private View parent;
/**
* 为了解决 设置背景后padding无效的bug
* @param parent
* @param key
* @param padding
*/
public void showAsDropDown(View parent, int[] padding) {
this.parent = parent;
popupWindow.showAsDropDown(parent, 0, context.getResources().getDimensionPixelSize(R.dimen.popmenu_yoff));
popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.add_drugbackground));
parent.setPadding(padding[0], padding[1], padding[2], padding[3]);
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
}
});
// 使其聚集
popupWindow.setFocusable(true);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);
// 刷新状态
popupWindow.update();
}
public void showAsDropDown(View parent, int padding, final Activity activity) {
this.parent=parent;
int[] paddings = { padding, padding, padding, padding };
showAsDropDown(parent, paddings);
// 设置背景颜色变暗
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 0.7f;
activity.getWindow().setAttributes(lp);
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 1f;
activity.getWindow().setAttributes(lp);
}
});
}
public void dismissPop() {
if(parent!=null)
popupWindow.dismiss();
}
public PopupWindow getPopupWindow() {
return popupWindow;
}
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
final int id = v.getId();
switch (id) {
case R.id.tv_addDrug:
if (this.mClickListener != null) {
this.mClickListener.onOKClick(1);
popupWindow.dismiss();
}
break;
case R.id.tv_storageRemind:
if (this.mClickListener != null) {
this.mClickListener.onOKClick(2);
popupWindow.dismiss();
}
break;
case R.id.tv_searchDrug:
if (this.mClickListener != null) {
this.mClickListener.onOKClick(3);
popupWindow.dismiss();
}
break;
}
}
}
标签:
原文地址:http://www.cnblogs.com/achen0502/p/5520829.html