本文主要是介绍Android实现带有listview的PopupWindow,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
图一是没数据时点击标题栏后显示的PopupWindow菜单,图二有数据时点击标题栏后显示的PopupWindow菜单
接下来看看具体实现:
1.布局
activity和PopupWindow多只是简单的listview
activity:
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/zhagndan_list" />
PopupWindow:
<ListView android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/frame_list"></ListView>
2.点击标题栏后触发的方法
private void setpopupWindow() {// 一个自定义的布局,作为显示的内容 View contentView = LayoutInflater.from(this).inflate(R.layout.popup_account_listview, null);popupWindow = new PopupWindow(contentView,LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(contentView);popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);// 弹出窗口加载数据 initPopData(contentView);// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.white));popupWindow.setTouchable(true);popupWindow.setFocusable(true);popupWindow.setOutsideTouchable(true);popupWindow.showAsDropDown(zhangdan);//zhangdan 是标题栏的id}private void initPopData(View defaultView) { ListView frag_list = (ListView)defaultView.findViewById(R.id.frame_list); frag_list.setOnItemClickListener(this); mContext = this; mData=getData(); //获取PopupWindow中的listview数据 //AllcountAdapter adapter = new AllcountAdapter(mContext,mData); //给activity中的listview设置数据 //frag_list.setAdapter(adapter);}private List<HashMap<String,Object>> getData() { ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> map = null; for (int i = 0;i<title_name.length;i++){ map = new HashMap<String,Object>(); map.put("img",title_view[i]); //图片数据 map.put("text",title_name[i]); //标题数据 list.add(map); } return list;}int[] title_view = { //图片 R.mipmap.account_jinzhang,R.mipmap.account_chuzhang,R.mipmap.account_daiqueren,R.mipmap.account_meng,R.mipmap.account_kaidian,R.mipmap.account_daiqueren,R.mipmap.account_daiqueren,R.mipmap.account_daiqueren,R.mipmap.account_daiqueren }; String[] title_name = { //名称 "全部账单", "开店账单", "驾校账单","营销公司账单", "引进公司账单", "中奖账单","全额返账单","服务公司账单","品牌公司账单" };主要代码就是这些,over。
这篇关于Android实现带有listview的PopupWindow的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!