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。


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值