二级菜单实现

自己 写的一个小demo  用两个listview 实现的

 

// popuwindow出现
                showPopup();
                popupWindow.showAsDropDown(v);
 1 private void showPopup() {
 2         LinearLayout layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.item_popup_show, null);
 3         popupWindow = new PopupWindow(layout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
 4         popupWindow.setBackgroundDrawable(new BitmapDrawable());
 5         popupWindow.setOutsideTouchable(true);
 6         popupWindow.setFocusable(true);
 7         popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER | Gravity.CENTER, 0, 0);
 8         listView1 = (ListView) layout.findViewById(R.id.listView1);
 9         listView2 = (ListView) layout.findViewById(R.id.listView2);
10         LinearLayout  ll = (LinearLayout) layout.findViewById(R.id.ll);
11         ll.setOnClickListener(new OnClickListener() {
12             
13             @Override
14             public void onClick(View v) {
15                 popupWindow.dismiss();
16             }
17         });
18         listView1.setVisibility(View.INVISIBLE);
19         listView2.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.text, R.id.tv_text, title));
20 
21         listView2.setOnItemClickListener(new OnItemClickListener() {
22 
23             @Override
24             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
25                 // 二级菜单显示
26                 listView1.setVisibility(View.VISIBLE);
27                 for (int i = 0; i < parent.getCount(); i++) {
28                     if (i == position) {
29                         parent.getChildAt(i).setBackgroundResource(R.drawable.gray3);
30                     } else {
31                         parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
32                     }
33                 }
34                 switch (position) {
35                 case 0:
36                     listView1.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.text, R.id.tv_text, string1));
37                     break;
38                 case 1:
39                     listView1.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.text, R.id.tv_text, string2));
40                     break;
41                 case 2:
42                     listView1.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.text, R.id.tv_text, string3));
43                     break;
44                 case 3:// 复制,直接弹框
45                     listView1.setVisibility(View.INVISIBLE);
46                     AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
47                     dialog.setMessage("已完成复制");
48                     dialog.setButton(Dialog.BUTTON_NEGATIVE, "确 定", new DialogInterface.OnClickListener() {
49 
50                         @Override
51                         public void onClick(DialogInterface dialog, int which) {
52                             // 完成复制,待办事件
53                             popupWindow.dismiss();
54                         }
55                     });
56                     dialog.show();
57                     break;
58                 case 4:// 转到转发页面
59                     break;
60                 case 5:
61                     popupWindow.dismiss();
62                     break;
63                 default:
64                     break;
65                 }
66             }
67         });
68         listView1.setOnItemClickListener(new OnItemClickListener() {
69 
70             @Override
71             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
72                 // TODO Auto-generated method stub
73 
74             }
75         });
76     }

 

posted @ 2013-11-19 13:35  youmu178  阅读(381)  评论(0编辑  收藏  举报