【转】微信中MMAlert(半透明底部弹出菜单)的使用介绍

原文地址:http://blog.csdn.net/singwhatiwanna/article/details/8892930

果大家时常用过微信或者用过iphone,就会发现有种从底部弹出的半透明菜单,这种菜单风格优美并且用户体验良好,先看一下效果。

MMAlert来自微信开放平台的sdk示例,其示例的代码有点乱,我做了删减和整理,只保留了MMAlert这个类的一部分功能,即只保留了实现上述效果的那个函数,因为其他函数比较简单,就是普通的AlertDialog,我觉得大家都懂,所以直接删掉了。

代码介绍

1 .  下面这段代码其实蛮好理解的,本质就是new一个对话框,然后将其放置在底部,为其设置theme和style,theme和style写的蛮好理解的, 具体大家可以看源码。数据呈现用的是Listview,为此我们需要new一个BaseAdapter对象来管理数据,BaseAdapter没什么特殊 之处,很好理解,具体请看代码。

 1     /**
 2      * @param context
 3      *            Context.
 4      * @param title
 5      *            The title of this AlertDialog can be null .
 6      * @param items
 7      *            button name list.
 8      * @param alertDo
 9      *            methods call Id:Button + cancel_Button.
10      * @param exit
11      *            Name can be null.It will be Red Color
12      * @return A AlertDialog
13      */
14     public static Dialog showAlert(final Context context, final String title, final String[] items, String exit, 
15             final OnAlertSelectId alertDo, OnCancelListener cancelListener)
16     {
17         String cancel = context.getString(R.string.app_cancel);
18         final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
19         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
20         LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.alert_dialog_menu_layout, null);
21         final int cFullFillWidth = 10000;
22         layout.setMinimumWidth(cFullFillWidth);
23         final ListView list = (ListView) layout.findViewById(R.id.content_list);
24         AlertAdapter adapter = new AlertAdapter(context, title, items, exit, cancel);
25         list.setAdapter(adapter);
26         list.setDividerHeight(0);
27 
28         list.setOnItemClickListener(new OnItemClickListener(){
29             @Override
30             public void onItemClick(AdapterView<?> parent, View view, int position, long id)
31             {
32                 if (!(title == null || title.equals("")) && position - 1 >= 0)
33                 {
34                     alertDo.onClick(position - 1);
35                     dlg.dismiss();
36                     list.requestFocus();
37                 }
38                 else
39                 {
40                     alertDo.onClick(position);
41                     dlg.dismiss();
42                     list.requestFocus();
43                 }
44 
45             }
46         });
47         // set a large value put it in bottom
48         Window w = dlg.getWindow();
49         WindowManager.LayoutParams lp = w.getAttributes();
50         lp.x = 0;
51         final int cMakeBottom = -1000;
52         lp.y = cMakeBottom;
53         lp.gravity = Gravity.BOTTOM;
54         dlg.onWindowAttributesChanged(lp);
55         dlg.setCanceledOnTouchOutside(true);
56         if (cancelListener != null)
57             dlg.setOnCancelListener(cancelListener);
58 
59         dlg.setContentView(layout);
60         dlg.show();
61 
62         return dlg;
63     }

2.  如何使用MMAlert?很简单!

 1 findViewById(R.id.send_img).setOnClickListener(new View.OnClickListener() {
 2 
 3     @Override
 4     public void onClick(View v) {
 5         MMAlert.showAlert(SendToWXActivity.this, getString(R.string.send_img), 
 6                 SendToWXActivity.this.getResources().getStringArray(R.array.send_img_item),
 7                 null, new MMAlert.OnAlertSelectId(){
 8 
 9             @Override
10             public void onClick(int whichButton) {                        
11                 switch(whichButton){
12                 case MMAlertSelect1: {
13 
14                     break;
15                 }
16                 case MMAlertSelect2: {
17 
18                     break;
19                 }
20                 case MMAlertSelect3: {
21 
22                     break;
23                 }
24                 default:
25                     break;
26                 }
27             }
28             
29         });
30     }
31 });

代码下载

http://download.csdn.net/detail/singwhatiwanna/5338394

下载地址二:

  http://pan.baidu.com/s/1i3l82eL

 

posted @ 2014-10-21 10:17  perfect亮  阅读(527)  评论(0编辑  收藏  举报