PopWindow实现二级联动菜单

一、效果图:


PopWindow实现二级联动菜单_第1张图片

 二、实现思路:

 1 使用PopWindow控件,然后再定义一个布局,里面包含两个ListView,分别作为一级菜单和二级菜单


三、实现代码

  

public class PopWindowsActivity extends Activity {
    Button popmenu;
    Context mContext;
    PopupWindow main_menuPop;
    PopupWindow sub_menuPop;
   ArrayList mainMenuItems= new ArrayList(Arrays.asList( "全部类型", "生活", "购物"
            ));
    String[][] sub_main_menuArr=new String[][]{{ "全部1", "生活1", "购物1" },{ "全部2", "生活2", "购物2" }};

    ArrayList submenu1= new ArrayList(Arrays.asList("全部1", "生活1", "购物1\" "));
    ArrayList submenu2= new ArrayList(Arrays.asList("全部2", "生活2", "购物2\" "));
    ArrayList subMenueList=new  ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_level_menu);
        popmenu=(Button)findViewById(R.id.pop_two_level_menu_b);

        popmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMainMenu(view);
            }
        });
        mContext=this;
    }
    private void showMainMenu(final View view) {

        // 一个自定义的布局,作为显示的内容
        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.view_pop_menu, null);
      //主菜单List
       ListView mainMeneLV=(ListView)contentView.findViewById(R.id.menu_listview);
        ArrayAdapter mainMenuAdapter=new ArrayAdapter(mContext,android.R.layout.simple_list_item_1,mainMenuItems);

        mainMeneLV.setAdapter(mainMenuAdapter);

        // 二级菜单
      final  ListView subMeneLV=(ListView)contentView.findViewById(R.id.submenu_listview);
       final  ArrayAdapter subMenuAdapter = new ArrayAdapter(mContext,android.R.layout.simple_list_item_1,subMenueList);
        subMeneLV.setAdapter(subMenuAdapter);
       //监听一级菜单
        mainMeneLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Log.i("mainMenuCLick",mainMenuItems.get(position));
                if(position==0){
                    subMeneLV.setVisibility(View.INVISIBLE);
                }else{
                    subMeneLV.setVisibility(View.VISIBLE);
                    subMenueList.clear();
                    int lenth=sub_main_menuArr[position-1].length;
                    for(int i=0;i parent, View view, int position, long id) {
                Log.i("subMeneLV",""+position);

            }
        });
        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.empty_photo));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);

    }


}
布局文件view_pop_menu.xml


    
    


你可能感兴趣的:(android,UI)