PopupWindow的使用
来源:互联网 发布:医用软件 编辑:程序博客网 时间:2024/05/29 15:42
最近在项目中用到了PopupWindow,写出来备注下,方便以后使用。主要参考文章:后期找到之后会附上。
1、PopWindow基础类展示。
注意重点是外放接口。
// 数据接口抽象方法 public interface OnGetData { // 赋值 abstract ArrayList<PopDataBean> onArrayList(); //获取被选中的值 abstract int onSeclectItem(); //外放点击事件 abstract void onDataCallBack(int nSectlect, ArrayList<PopDataBean> mArrayList); }
具体展示如下:
public class PopWindow extends PopupWindow implements AdapterView.OnItemClickListener { // 根视图 private View mRootView; private LayoutInflater mInflater; // ArrayList数组,listview原始数据 private ArrayList<PopDataBean> mArrayList=new ArrayList<>(); // 数据接口 OnGetData mOnGetData; // listview适配器 PopMenuItemsAdapter groupAdapter; ListView listItemsView=null; private int mnSeclectItem = -1; public PopWindow(Activity context) { super(context); initData(context); initUI(); } // 数据接口抽象方法 public interface OnGetData { // 赋值 abstract ArrayList<PopDataBean> onArrayList(); //获取被选中的值 abstract int onSeclectItem(); //外放点击事件 abstract void onDataCallBack(int nSectlect, ArrayList<PopDataBean> mArrayList); } // 数据接口设置,数据源接口传入 public void setOnData(OnGetData sd) { mOnGetData = sd; mArrayList = new ArrayList<PopDataBean>(); if (mOnGetData != null) { //执行赋值操作 mArrayList = mOnGetData.onArrayList(); //获取上次被选中的值 mnSeclectItem = mOnGetData.onSeclectItem(); //给listview设置数值 groupAdapter.setDatas(mArrayList); //给listview设置被选中内容 groupAdapter.setSelected(mnSeclectItem); //更新listview数据展示 groupAdapter.notifyDataSetChanged(); } } private void initData(Context context) { mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mRootView = mInflater.inflate(R.layout.popuplayout_items, null); setContentView(mRootView); this.setWidth((int) (3 * LUtils.getWidthPixels(context) / 4)); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); //设置动画 setAnimationStyle(R.style.popup_window_anim); // 设置PopUpWindow弹出的相关属性 setTouchable(true); setOutsideTouchable(true); setFocusable(true); setBackgroundDrawable(new BitmapDrawable(context.getResources())); update(); getContentView().setFocusableInTouchMode(true); getContentView().setFocusable(true); } private void initUI() { listItemsView = (ListView) mRootView.findViewById(R.id.listItems); groupAdapter = new PopMenuItemsAdapter(mRootView.getContext()); listItemsView.setAdapter(groupAdapter); listItemsView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mOnGetData.onDataCallBack(position, mArrayList); dismiss(); }}
2、bean类展示。简单的实体bean,没有其他复杂内容。
public class PopDataBean { //对应key值 private String index; //对应名字 private String name; public PopDataBean(String index, String name) { this.index = index; this.name = name; } public String getIndex() { return index; } public void setIndex(String index) { this.index = index; } public String getName() { return name; } public void setName(String name) { this.name = name; }}3、具体引用展示:
注意setOnData的具体引用,点击事件、赋值、更新界面ui操作均在这执行。
public class MainActivity extends AppCompatActivity implements PopupWindow.OnDismissListener { @InjectView(R.id.tvJBZD) TextView tvJBZD; @InjectView(R.id.tv_patient_diseasecontent) TextView tvPatientDiseasecontent; private PopWindow popWindowJBZD = null; List<PopDataBean> datasJBZD = new ArrayList<>(); //选中单条 private int mnSeclectItem = -1; //单条id private String mnSeclectItemID = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); initListeners(); initDatas(); } /** * 初始化数据 */ private void initDatas(){ initPopDatas(); } /** * 监听事件 */ private void initListeners(){ tvPatientDiseasecontent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onPopwindowJBZD(); } }); } /** * 初始化Popwindow */ private void initPopDatas() { datasJBZD = new ArrayList<>(); datasJBZD.add(new PopDataBean("0", "脑外伤")); datasJBZD.add(new PopDataBean("1", "脑血管意外")); datasJBZD.add(new PopDataBean("2", "缺血缺氧性脑损伤")); datasJBZD.add(new PopDataBean("3", "中毒")); datasJBZD.add(new PopDataBean("4", "脑肿瘤")); datasJBZD.add(new PopDataBean("5", "其他")); // 实例化PopWindow popWindowJBZD = new PopWindow(this); // 设置点击其他位置mTestPopwindow2消失 popWindowJBZD.setOnDismissListener(this); } private void onPopwindowJBZD() { if (popWindowJBZD == null) { return; } //回到接受 popWindowJBZD.setOnData(new PopWindow.OnGetData() { //记录上一次选中的item @Override public int onSeclectItem() { return mnSeclectItem; } @Override public void onDataCallBack(int nSectlect, ArrayList<PopDataBean> mArrayList) { mnSeclectItem = nSectlect; String name = mArrayList.get(nSectlect).getName(); tvPatientDiseasecontent.setText(name); mnSeclectItemID = mArrayList.get(nSectlect).getIndex(); } //传递数据源过去 @Override public ArrayList<PopDataBean> onArrayList() { return (ArrayList<PopDataBean>) datasJBZD; } }); popWindowJBZD.showAtLocation(tvPatientDiseasecontent, Gravity.CENTER, 10, 10); } @Override public void onDismiss() { }}
附赠项目完整地址:
https://github.com/wt0428/PopWindowTest
阅读全文
0 0
- Android PopupWindow的使用
- Android PopupWindow的使用
- Android PopupWindow的使用!!!
- 关于popupWindow的使用
- PopupWindow的使用
- PopupWindow的使用例子
- PopupWindow的使用
- popupWindow的使用心得
- PopupWindow的使用
- PopupWindow的使用
- Android PopupWindow的使用!!!
- PopupWindow的使用
- PopupWindow的使用
- popupWindow的使用
- PopupWindow的使用
- PopupWindow的使用
- Popupwindow的使用
- PopupWindow的使用
- 精灵背景图片、音乐及金币的添加
- 摄像头图像质量常用指标的测试方法
- cpu占用高分析
- JS当前时间推迟半小时的算法
- PopupWindow
- PopupWindow的使用
- 高速缓存DNS
- Android USB 设备如何区分是打印机还是U盘
- Spring Cloud Eureka 增加权限认证
- 单链表判断是否相交
- JavaScript——组合使用构造函数模式和原型模式
- project Eulert 学习笔记 之 problem 22
- Ransac算法简介
- nova的pci 设备白名单