美文网首页Android控件Android学习Android技术
Android用PopupWindow实现下拉菜单

Android用PopupWindow实现下拉菜单

作者: BugMyGod | 来源:发表于2018-03-07 19:06 被阅读3691次

实现效果:


initpintu_副本_副本.jpg

(注意:PopupWindow菜单Android5.0以下版本点击外部不消失问题 && 在Android 7.0上PopupWindow.showAsDropDown不起作用问题)

布局说明:


image.png

点击区域弹出区域菜单,并实现遮罩层效果:


image.png
以下为实现代码(仅菜单部分):
1,主页布局newpages_activity_risk_demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/gray_white">

    <FrameLayout
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:id="@+id/f1_frag"
        android:layout_width="match_parent"
        android:layout_height="105dip"
        android:layout_marginTop="50dip"
        android:background="@color/gray_white">
        <!-- 第一行 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:orientation="horizontal"
            android:background="@color/white">

            <!-- 第一列 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvRiskArea"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="区域"
                    android:textColor="@color/title_text_color"
                    android:textSize="@dimen/tb_body"/>

            </LinearLayout>
            <!-- 右下三角 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="0.1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="10dip"
                    android:layout_height="10dip"
                    android:layout_marginTop="30dip"
                    android:background="@drawable/trigon" />

            </LinearLayout>
            <!-- 第二列 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvRiskSort"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="类别"
                    android:textColor="@color/title_text_color"
                    android:textSize="@dimen/tb_body"/>

            </LinearLayout>
            <!-- 右下三角 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="0.1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="10dip"
                    android:layout_height="10dip"
                    android:layout_marginTop="30dip"
                    android:background="@drawable/trigon" />

            </LinearLayout>
            <!-- 第三列 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvRiskLevel"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="等级"
                    android:textColor="@color/title_text_color"
                    android:textSize="@dimen/tb_body"/>

            </LinearLayout>
            <!-- 右下三角 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="0.1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="10dip"
                    android:layout_height="10dip"
                    android:layout_marginTop="30dip"
                    android:background="@drawable/trigon" />

            </LinearLayout>
            <!-- 第四列 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvRiskGrade"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="来源"
                    android:textColor="@color/title_text_color"
                    android:textSize="@dimen/tb_body"/>

            </LinearLayout>
            <!-- 右下三角 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="0.1"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="10dip"
                    android:layout_height="10dip"
                    android:layout_marginTop="30dip"
                    android:background="@drawable/trigon" />

            </LinearLayout>
            <!-- 空白占位 -->
            <LinearLayout
                android:layout_width="0dip"
                android:layout_weight="0.1"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginTop="50dip"
            android:background="@color/gray_white"/>

        <!-- 第二行 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:layout_marginTop="50.5dip"
            android:orientation="horizontal"
            android:background="@color/white">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="fill_vertical"
                android:text="当前区域:全部区域"
                android:layout_marginLeft="10dip"
                android:textColor="@color/title_text_color"
                android:textSize="@dimen/tb_body"/>

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginTop="101dip"
            android:background="@color/gray_white"/>

    </FrameLayout>

    <ListView
        android:id="@+id/lvRisk"
        android:divider="@null"
        android:dividerHeight="1.0dip"
        android:scrollbars="none"
        android:listSelector="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:fadingEdge="none"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="155dip"
        android:layout_marginBottom="10dip"
        />

</RelativeLayout>

2,主页activity PageRiskActivity.java

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.andbase.R;
import com.andbase.global.MyApplication;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskAreaListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskGradeListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskLevelListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskSortListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.PageRiskAdapter;
import com.andbase.patrol.gtDemo.model.PageRiskFormDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:lmx
 * date:2018/3/6
 * description:风险清单--PopupWindow菜单、列表
 */
public class PageRiskActivity extends AbActivity implements View.OnClickListener{

    private MyApplication application;
    //加载不同布局
    private PageRiskActivity context;
    private ListView listview;
    public List<PageRiskFormDemo> listInfo = null;
    //实例
    private TextView tvRiskArea,tvRiskSort,tvRiskLevel,tvRiskGrade;
    //菜单显示PopupWindow
    private PopupWindow mPopWindow;
    private List<Map<String, Object>> riskAreaList = null;
    private List<Map<String, Object>> riskSortList = null;
    private List<Map<String, Object>> riskLevelList = null;
    private List<Map<String, Object>> riskGradeList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.newpages_activity_risk_demo);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("风险清单");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackground(R.drawable.top_bg);
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.drawable.line);
        this.setTitleBarOverlay(true);
        application = (MyApplication)abApplication;
        //加载不同布局
        this.listview = (ListView) findViewById(R.id.lvRisk);
        context = this;
        //获取实例--页头菜单
        tvRiskArea = (TextView)findViewById(R.id.tvRiskArea);
        tvRiskSort = (TextView)findViewById(R.id.tvRiskSort);
        tvRiskLevel = (TextView)findViewById(R.id.tvRiskLevel);
        tvRiskGrade = (TextView)findViewById(R.id.tvRiskGrade);
        //绑定点击事件
        tvRiskArea.setOnClickListener(this);
        tvRiskSort.setOnClickListener(this);
        tvRiskLevel.setOnClickListener(this);
        tvRiskGrade.setOnClickListener(this);

        //加载数据
        loadListView();
    }

    /**
     * 加载listView数据
     */
    public void loadListView(){
        try {
            listInfo = new ArrayList<PageRiskFormDemo>();

            PageRiskFormDemo form1 = new PageRiskFormDemo();
            form1.setPropertyType(0);//数值
            form1.setRiskName("风险点名称1");
            form1.setFirstRisk("一类风险点");
            form1.setaRisk("A级风险点");
            form1.setRiskControl("风险分级管控");
            listInfo.add(form1);

            PageRiskFormDemo form2 = new PageRiskFormDemo();
            form2.setPropertyType(0);
            form2.setRiskName("风险点名称2");
            form2.setFirstRisk("一类风险点");
            form2.setaRisk("A级风险点");
            form2.setRiskControl("风险分级管控");
            listInfo.add(form2);

            PageRiskFormDemo form3 = new PageRiskFormDemo();
            form3.setPropertyType(0);
            form3.setRiskName("风险点名称3");
            form3.setFirstRisk("一类风险点");
            form3.setaRisk("A级风险点");
            form3.setRiskControl("风险分级管控");
            listInfo.add(form3);

            PageRiskFormDemo form4 = new PageRiskFormDemo();
            form4.setPropertyType(0);
            form4.setRiskName("风险点名称4");
            form4.setFirstRisk("一类风险点");
            form4.setaRisk("A级风险点");
            form4.setRiskControl("风险分级管控");
            listInfo.add(form4);

            PageRiskFormDemo form5 = new PageRiskFormDemo();
            form5.setPropertyType(0);
            form5.setRiskName("风险点名称5");
            form5.setFirstRisk("一类风险点");
            form5.setaRisk("A级风险点");
            form5.setRiskControl("风险分级管控");
            listInfo.add(form5);

            PageRiskFormDemo form6 = new PageRiskFormDemo();
            form6.setPropertyType(0);
            form6.setRiskName("风险点名称6");
            form6.setFirstRisk("一类风险点");
            form6.setaRisk("A级风险点");
            form6.setRiskControl("风险分级管控");
            listInfo.add(form6);

        }catch(Exception ex)
        {
            Log.e("JSON Error: ", ex.toString());
        }

        ListView listView = (ListView) findViewById(R.id.lvRisk);
        PageRiskAdapter pageRiskAdapter = new PageRiskAdapter(context,listInfo);
        listView.setAdapter(pageRiskAdapter);
    }

    //菜单绑定点击事件
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.tvRiskArea:
                showRiskAreaPopupWindow();
            break;
            case R.id.tvRiskSort:
                showRiskSortPopupWindow();
            break;
            case R.id.tvRiskLevel:
                showRiskLevelPopupWindow();
            break;
            case R.id.tvRiskGrade:
                showRiskGradePopupWindow();
            break;
        }
    }
    //PopupWindow菜单详细内容显示
    //区域
    private void showRiskAreaPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_area_popup_demo, null);
        mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
        mPopWindow.setContentView(contentView);
        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllArea = (CheckBox)contentView.findViewById(R.id.cbAllArea);
        TextView tvAllArea = (TextView)contentView.findViewById(R.id.tvAllArea);
        ListView lvRiskArea = (ListView)contentView.findViewById(R.id.lvRiskArea);
        //区域列表加载
        riskAreaList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<10; i++)
        {
            map = new HashMap<String, Object>();
            map.put("tvAreaItem", "区域" + i);
            map.put("tvAreaNo", i+5);
            riskAreaList.add(map);
        }
        ListRiskAreaListsDemoAdapter listRiskAreaListsDemoAdapter = new ListRiskAreaListsDemoAdapter(PageRiskActivity.this, riskAreaList,
                R.layout.newpages_risk_area_popup_list_item_demo, new String[] { "tvAreaItem","tvAreaNo"}, new int[] { R.id.tvAreaItem,R.id.tvAreaNo});
        lvRiskArea.setAdapter(listRiskAreaListsDemoAdapter);

        tvAllArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.showAlertDialog(PageRiskActivity.this,"直接绑定点击事件");
                mPopWindow.dismiss();
            }
        });

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskArea);

    }
    //类别
    private void showRiskSortPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_sort_popup_demo, null);
        mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
        mPopWindow.setContentView(contentView);

        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllSort = (CheckBox)contentView.findViewById(R.id.cbAllSort);
        ListView lvRiskSort = (ListView)contentView.findViewById(R.id.lvRiskSort);
        //类别列表加载
        riskSortList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<4; i++)
        {
            map = new HashMap<String, Object>();
            switch (i){
                case 1:
                    map.put("tvSortItem", "一类");
                    riskSortList.add(map);
                    break;
                case 2:
                    map.put("tvSortItem", "二类");
                    riskSortList.add(map);
                    break;
                case 3:
                    map.put("tvSortItem", "三类");
                    riskSortList.add(map);
                    break;
            }
//            map.put("tvSortItem", i + "类");
//            riskSortList.add(map);
        }
        ListRiskSortListsDemoAdapter listRiskSortListsDemoAdapter = new ListRiskSortListsDemoAdapter(PageRiskActivity.this, riskSortList,
                R.layout.newpages_risk_sort_popup_list_item_demo, new String[] { "tvSortItem"}, new int[] { R.id.tvSortItem});
        lvRiskSort.setAdapter(listRiskSortListsDemoAdapter);

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskSort);
    }
    //等级
    private void showRiskLevelPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_level_popup_demo, null);
        mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
        mPopWindow.setContentView(contentView);

        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllLevel = (CheckBox)contentView.findViewById(R.id.cbAllLevel);
        ListView lvRiskLevel = (ListView)contentView.findViewById(R.id.lvRiskLevel);
        //等级列表加载
        riskLevelList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<4; i++)
        {
            map = new HashMap<String, Object>();
            switch (i){
                case 1:
                    map.put("tvLevelItem", "A级");
                    riskLevelList.add(map);
                    break;
                case 2:
                    map.put("tvLevelItem", "B级");
                    riskLevelList.add(map);
                    break;
                case 3:
                    map.put("tvLevelItem", "C级");
                    riskLevelList.add(map);
                    break;
            }

//            map.put("tvLevelItem", i + "级");
//            riskLevelList.add(map);
        }
        ListRiskLevelListsDemoAdapter listRiskLevelListsDemoAdapter = new ListRiskLevelListsDemoAdapter(PageRiskActivity.this, riskLevelList,
                R.layout.newpages_risk_level_popup_list_item_demo, new String[] { "tvLevelItem"}, new int[] { R.id.tvLevelItem});
        lvRiskLevel.setAdapter(listRiskLevelListsDemoAdapter);

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskLevel);
    }
    //来源
    private void showRiskGradePopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_grade_popup_demo, null);
        mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
        mPopWindow.setContentView(contentView);

        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllGrade = (CheckBox)contentView.findViewById(R.id.cbAllGrade);
        ListView lvRiskGrade = (ListView)contentView.findViewById(R.id.lvRiskGrade);
        //来源列表加载
        riskGradeList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<5; i++)
        {
            map = new HashMap<String, Object>();
            switch (i){
               case 1:
                   map.put("tvGradeItem", "风险分级管控系统");
                   riskGradeList.add(map);
                   break;
               case 2:
                   map.put("tvGradeItem", "巡检管理系统");
                   riskGradeList.add(map);
                   break;
               case 3:
                   map.put("tvGradeItem", "作业管理系统");
                   riskGradeList.add(map);
                   break;
               case 4:
                   map.put("tvGradeItem", "综合安全管理系统");
                   riskGradeList.add(map);
                   break;
            }
//            map.put("tvGradeItem", i + "系统");
//            riskGradeList.add(map);
        }
        ListRiskGradeListsDemoAdapter listRiskGradeListsDemoAdapter = new ListRiskGradeListsDemoAdapter(PageRiskActivity.this, riskGradeList,
                R.layout.newpages_risk_grade_popup_list_item_demo, new String[] { "tvGradeItem"}, new int[] { R.id.tvGradeItem});
        lvRiskGrade.setAdapter(listRiskGradeListsDemoAdapter);

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskGrade);
    }

}

3,区域子菜单(其它相同),对应为主页activity中showRiskAreaPopupWindow()部分。
子菜单的表现样式:newpages_activity_risk_area_popup_demo.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/popup_shadow_bg_color">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        android:paddingBottom="2dp"
        android:layout_alignParentTop="true">

        <!-- 悬浮按钮 -->
        <FrameLayout
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:id="@+id/f1_frag"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_marginBottom="0dip"
            android:background="@color/popup_bg_color">

            <CheckBox
                android:id="@+id/cbAllArea"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dip"
                android:textColor="@color/details_text_color"
                android:textSize="@dimen/table_body"
                android:text="全部区域" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:gravity="right"
                android:layout_marginLeft="8dip"
                android:layout_marginRight="15dip">

                <TextView
                    android:id="@+id/tvAllArea"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:text="121"
                    android:gravity="center"
                    android:textColor="@color/details_text_color"
                    android:textSize="@dimen/table_body"/>

            </LinearLayout>


        </FrameLayout>

        <ListView
            android:id="@+id/lvRiskArea"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@color/table_line"
            android:dividerHeight="1.0dip"
            android:scrollbars="none"
            android:listSelector="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent"
            android:fadingEdge="none"
            />

    </LinearLayout>

</RelativeLayout>

其中:RelativeLayout的android:background="@color/popup_shadow_bg_color"为半透明色:#96000000,实现遮罩层效果
4,子菜单中ListView的item
newpages_risk_area_popup_list_item_demo.xml

<?xml version="1.0" encoding="UTF-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:gravity="center_vertical"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        >

        <TextView
            android:id="@+id/tvAreaItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/table_padding"
            android:paddingTop="@dimen/table_padding"
            android:textColor="@color/title_text_color"
            android:textSize="@dimen/table_body"
            android:gravity="center"
            android:singleLine="false"
            android:ellipsize="none"
            android:inputType="textMultiLine"
            android:text="区域一"
            />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:gravity="right"
            android:layout_marginLeft="8dip">

            <TextView
                android:id="@+id/tvAreaNo"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="12"
                android:gravity="center_vertical"
                android:textColor="@color/title_text_color"
                android:textSize="@dimen/table_body"/>

        </LinearLayout>

    </LinearLayout>

</android.support.percent.PercentRelativeLayout>

5,区域子菜单Adapter
ListRiskAreaListsDemoAdapter.java

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.andbase.patrol.gtDemo.activity.pages.newPages.PageRiskActivity;

import java.util.List;
import java.util.Map;

/**
 * author:lmx
 * date:2018/3/7
 * description:
 */

public class ListRiskAreaListsDemoAdapter extends BaseAdapter {

    private PageRiskActivity mContext;
    //单行的布局
    private int mResource;
    //列表展现的数据
    private List<? extends Map<String, ?>> mData;
    //Map中的key
    private String[] mFrom;
    //view的id
    private int[] mTo;

    //点击变色
    private int selectedPosition = -1;// 选中的位置

    /**
     * 构造方法
     * @param context
     * @param data 列表展现的数据
     * @param resource 单行的布局
     * @param from Map中的key
     * @param to view的id
     */
    public ListRiskAreaListsDemoAdapter(PageRiskActivity context, List<? extends Map<String, ?>> data,
                                        int resource, String[] from, int[] to){
        mContext = context;
        mData = data;
        mResource = resource;
        mFrom = from;
        mTo = to;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    //点击变色
    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        final ListRiskAreaListsDemoAdapter.ViewHolder holder;
        if(convertView == null){
            //使用自定义的list_items作为Layout
            convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
            //使用减少findView的次数
            holder = new ListRiskAreaListsDemoAdapter.ViewHolder();
            holder.tvAreaItem = ((TextView) convertView.findViewById(mTo[0]));
            holder.tvAreaNo = ((TextView) convertView.findViewById(mTo[1]));

            //设置标记
            convertView.setTag(holder);
        }else{
            holder = (ListRiskAreaListsDemoAdapter.ViewHolder) convertView.getTag();
        }
        //设置数据
        final Map<String, ?> dataSet = mData.get(position);
        if (dataSet == null) {
            return null;
        }
        //获取该行数据
        final Object tvAreaItem = dataSet.get(mFrom[0]);
        final Object tvAreaNo = dataSet.get(mFrom[1]);

        holder.tvAreaItem.setText(tvAreaItem.toString());
        holder.tvAreaNo.setText(tvAreaNo.toString());

        return convertView;
    }

    /**
     * ViewHolder类
     */
    static class ViewHolder {
        TextView tvAreaItem;//区域名
        TextView tvAreaNo;//No
    }

}

完。

补充:解决5.0以下版本,点击外部PopupWindow菜单不消失的问题

需要在显示方式前加这两行代码

//解决5.0以下版本点击外部不消失问题
mPopWindow.setOutsideTouchable(true);
mPopWindow.setBackgroundDrawable(new BitmapDrawable());

补充:解决7.0版本PopupWindow.showAsDropDown不起作用

新建Solve7PopupWindow类

import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.PopupWindow;

/**
 * author:lmx
 * date:2018/3/27
 * description:解决在Android 7.0上PopupWindow.showAsDropDown不起作用
 */
public class Solve7PopupWindow extends PopupWindow {

    public Solve7PopupWindow(View mMenuView, int matchParent, int matchParent1) {
        super(mMenuView, matchParent,matchParent1);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }
}

在PageRiskActivity中使用如下:
将原来的

mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

替换为:

//适配7.0版本
mPopWindow = new Solve7PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

更新后的PageRiskActivity.java

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.andbase.R;
import com.andbase.global.MyApplication;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskAreaListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskGradeListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskLevelListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.ListRiskSortListsDemoAdapter;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.newPagesAdapter.PageRiskAdapter;
import com.andbase.patrol.gtDemo.model.PageRiskFormDemo;
import com.andbase.patrol.gtDemo.util.Solve7PopupWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:lmx
 * date:2018/3/6
 * description:风险清单--PopupWindow菜单、列表
 * 注意:PopupWindow菜单Android5.0以下版本点击外部不消失问题
 *      在Android 7.0上PopupWindow.showAsDropDown不起作用问题
 */
public class PageRiskActivity extends AbActivity implements View.OnClickListener{

    private MyApplication application;
    //加载不同布局
    private PageRiskActivity context;
    private ListView listview;
    public List<PageRiskFormDemo> listInfo = null;
    //实例
    private LinearLayout llPopBox;
    private TextView tvRiskArea,tvRiskSort,tvRiskLevel,tvRiskGrade;
    //菜单显示PopupWindow
    private PopupWindow mPopWindow;
    private List<Map<String, Object>> riskAreaList = null;
    private List<Map<String, Object>> riskSortList = null;
    private List<Map<String, Object>> riskLevelList = null;
    private List<Map<String, Object>> riskGradeList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.newpages_activity_risk_demo);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("风险清单");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackground(R.drawable.top_bg);
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.drawable.line);
        this.setTitleBarOverlay(true);
        application = (MyApplication)abApplication;
        //加载不同布局
        this.listview = (ListView) findViewById(R.id.lvRisk);
        context = this;
        //获取实例--页头菜单
        llPopBox = (LinearLayout)findViewById(R.id.llPopBox);
        tvRiskArea = (TextView)findViewById(R.id.tvRiskArea);
        tvRiskSort = (TextView)findViewById(R.id.tvRiskSort);
        tvRiskLevel = (TextView)findViewById(R.id.tvRiskLevel);
        tvRiskGrade = (TextView)findViewById(R.id.tvRiskGrade);
        //绑定点击事件
        tvRiskArea.setOnClickListener(this);
        tvRiskSort.setOnClickListener(this);
        tvRiskLevel.setOnClickListener(this);
        tvRiskGrade.setOnClickListener(this);

        //加载数据
        loadListView();
    }

    /**
     * 加载listView数据
     */
    public void loadListView(){
        try {
            listInfo = new ArrayList<PageRiskFormDemo>();

            PageRiskFormDemo form1 = new PageRiskFormDemo();
            form1.setPropertyType(0);//数值
            form1.setRiskName("风险点名称1");
            form1.setFirstRisk("一类风险点");
            form1.setaRisk("A级风险点");
            form1.setRiskControl("风险分级管控");
            listInfo.add(form1);

            PageRiskFormDemo form2 = new PageRiskFormDemo();
            form2.setPropertyType(0);
            form2.setRiskName("风险点名称2");
            form2.setFirstRisk("一类风险点");
            form2.setaRisk("A级风险点");
            form2.setRiskControl("风险分级管控");
            listInfo.add(form2);

            PageRiskFormDemo form3 = new PageRiskFormDemo();
            form3.setPropertyType(0);
            form3.setRiskName("风险点名称3");
            form3.setFirstRisk("一类风险点");
            form3.setaRisk("A级风险点");
            form3.setRiskControl("风险分级管控");
            listInfo.add(form3);

            PageRiskFormDemo form4 = new PageRiskFormDemo();
            form4.setPropertyType(0);
            form4.setRiskName("风险点名称4");
            form4.setFirstRisk("一类风险点");
            form4.setaRisk("A级风险点");
            form4.setRiskControl("风险分级管控");
            listInfo.add(form4);

            PageRiskFormDemo form5 = new PageRiskFormDemo();
            form5.setPropertyType(0);
            form5.setRiskName("风险点名称5");
            form5.setFirstRisk("一类风险点");
            form5.setaRisk("A级风险点");
            form5.setRiskControl("风险分级管控");
            listInfo.add(form5);

            PageRiskFormDemo form6 = new PageRiskFormDemo();
            form6.setPropertyType(0);
            form6.setRiskName("风险点名称6");
            form6.setFirstRisk("一类风险点");
            form6.setaRisk("A级风险点");
            form6.setRiskControl("风险分级管控");
            listInfo.add(form6);

        }catch(Exception ex)
        {
            Log.e("JSON Error: ", ex.toString());
        }

        ListView listView = (ListView) findViewById(R.id.lvRisk);
        PageRiskAdapter pageRiskAdapter = new PageRiskAdapter(context,listInfo);
        listView.setAdapter(pageRiskAdapter);
    }

    //菜单绑定点击事件
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.tvRiskArea:
                showRiskAreaPopupWindow();
            break;
            case R.id.tvRiskSort:
                showRiskSortPopupWindow();
            break;
            case R.id.tvRiskLevel:
                showRiskLevelPopupWindow();
            break;
            case R.id.tvRiskGrade:
                showRiskGradePopupWindow();
            break;
        }
    }
    //PopupWindow菜单详细内容显示
    //区域
    private void showRiskAreaPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_area_popup_demo, null);
        //适配7.0版本
        mPopWindow = new Solve7PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);
        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllArea = (CheckBox)contentView.findViewById(R.id.cbAllArea);
        TextView tvAllArea = (TextView)contentView.findViewById(R.id.tvAllArea);
        ListView lvRiskArea = (ListView)contentView.findViewById(R.id.lvRiskArea);
        //区域列表加载
        riskAreaList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<10; i++)
        {
            map = new HashMap<String, Object>();
            map.put("tvAreaItem", "区域" + i);
            map.put("tvAreaNo", i+5);
            riskAreaList.add(map);
        }
        ListRiskAreaListsDemoAdapter listRiskAreaListsDemoAdapter = new ListRiskAreaListsDemoAdapter(PageRiskActivity.this, riskAreaList,
                R.layout.newpages_risk_area_popup_list_item_demo, new String[] { "tvAreaItem","tvAreaNo"}, new int[] { R.id.tvAreaItem,R.id.tvAreaNo});
        lvRiskArea.setAdapter(listRiskAreaListsDemoAdapter);

        tvAllArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.showAlertDialog(PageRiskActivity.this,"直接绑定点击事件");
                mPopWindow.dismiss();
            }
        });

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskArea);

//        if (Build.VERSION.SDK_INT < 24) {
////            dropListPopupWindow.showAsDropDown(this, 0, 5);
//            mPopWindow.showAsDropDown(tvRiskArea);
//        } else {
//            // 适配 android 7.0
//            mPopWindow = new Solve7PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            mPopWindow.showAsDropDown(tvRiskArea);
//        }

    }


    //类别
    private void showRiskSortPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_sort_popup_demo, null);
        //适配7.0版本
        mPopWindow = new Solve7PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        mPopWindow = new PopupWindow(contentView,
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);

        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllSort = (CheckBox)contentView.findViewById(R.id.cbAllSort);
        ListView lvRiskSort = (ListView)contentView.findViewById(R.id.lvRiskSort);
        //类别列表加载
        riskSortList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<4; i++)
        {
            map = new HashMap<String, Object>();
            switch (i){
                case 1:
                    map.put("tvSortItem", "一类");
                    riskSortList.add(map);
                    break;
                case 2:
                    map.put("tvSortItem", "二类");
                    riskSortList.add(map);
                    break;
                case 3:
                    map.put("tvSortItem", "三类");
                    riskSortList.add(map);
                    break;
            }
//            map.put("tvSortItem", i + "类");
//            riskSortList.add(map);
        }
        ListRiskSortListsDemoAdapter listRiskSortListsDemoAdapter = new ListRiskSortListsDemoAdapter(PageRiskActivity.this, riskSortList,
                R.layout.newpages_risk_sort_popup_list_item_demo, new String[] { "tvSortItem"}, new int[] { R.id.tvSortItem});
        lvRiskSort.setAdapter(listRiskSortListsDemoAdapter);

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskSort);
    }
    //等级
    private void showRiskLevelPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_level_popup_demo, null);
        //适配7.0版本
        mPopWindow = new Solve7PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        mPopWindow = new PopupWindow(contentView,
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);

        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllLevel = (CheckBox)contentView.findViewById(R.id.cbAllLevel);
        ListView lvRiskLevel = (ListView)contentView.findViewById(R.id.lvRiskLevel);
        //等级列表加载
        riskLevelList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<4; i++)
        {
            map = new HashMap<String, Object>();
            switch (i){
                case 1:
                    map.put("tvLevelItem", "A级");
                    riskLevelList.add(map);
                    break;
                case 2:
                    map.put("tvLevelItem", "B级");
                    riskLevelList.add(map);
                    break;
                case 3:
                    map.put("tvLevelItem", "C级");
                    riskLevelList.add(map);
                    break;
            }

//            map.put("tvLevelItem", i + "级");
//            riskLevelList.add(map);
        }
        ListRiskLevelListsDemoAdapter listRiskLevelListsDemoAdapter = new ListRiskLevelListsDemoAdapter(PageRiskActivity.this, riskLevelList,
                R.layout.newpages_risk_level_popup_list_item_demo, new String[] { "tvLevelItem"}, new int[] { R.id.tvLevelItem});
        lvRiskLevel.setAdapter(listRiskLevelListsDemoAdapter);

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskLevel);
    }
    //来源
    private void showRiskGradePopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PageRiskActivity.this).inflate(R.layout.newpages_activity_risk_grade_popup_demo, null);
        //适配7.0版本
        mPopWindow = new Solve7PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        mPopWindow = new PopupWindow(contentView,
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);

        //获取实例,设置各个控件的点击响应
        //注意:PopupWindow中各个控件的所在的布局是contentView,而不是在Activity中,所以,要在findViewById(R.id.tv)前指定根布局
        CheckBox cbAllGrade = (CheckBox)contentView.findViewById(R.id.cbAllGrade);
        ListView lvRiskGrade = (ListView)contentView.findViewById(R.id.lvRiskGrade);
        //来源列表加载
        riskGradeList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        for(int i=1; i<5; i++)
        {
            map = new HashMap<String, Object>();
            switch (i){
               case 1:
                   map.put("tvGradeItem", "风险分级管控系统");
                   riskGradeList.add(map);
                   break;
               case 2:
                   map.put("tvGradeItem", "巡检管理系统");
                   riskGradeList.add(map);
                   break;
               case 3:
                   map.put("tvGradeItem", "作业管理系统");
                   riskGradeList.add(map);
                   break;
               case 4:
                   map.put("tvGradeItem", "综合安全管理系统");
                   riskGradeList.add(map);
                   break;
            }
//            map.put("tvGradeItem", i + "系统");
//            riskGradeList.add(map);
        }
        ListRiskGradeListsDemoAdapter listRiskGradeListsDemoAdapter = new ListRiskGradeListsDemoAdapter(PageRiskActivity.this, riskGradeList,
                R.layout.newpages_risk_grade_popup_list_item_demo, new String[] { "tvGradeItem"}, new int[] { R.id.tvGradeItem});
        lvRiskGrade.setAdapter(listRiskGradeListsDemoAdapter);

        //解决5.0以下版本点击外部不消失问题
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        //显示方式
        mPopWindow.showAsDropDown(tvRiskGrade);
    }
}

相关文章

网友评论

    本文标题:Android用PopupWindow实现下拉菜单

    本文链接:https://www.haomeiwen.com/subject/ehksfftx.html