使用PopupWindow + 2个ListView实现仿 美团/淘宝 多级分类菜单效果

本例要实现的是诸如美团/淘宝/百度糯米 多级分类菜单效果。当分类数量非常多时可以考虑采用两级分类,而诸如美团这种表现方式是一个不错的选择。

首先上效果图:

    


主要代码:

1. PopupWindow初始化过程:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. popupWindow = new PopupWindow(this);  
  2.         View view = LayoutInflater.from(this).inflate(R.layout.popup_layout, null);  
  3.         leftLV = (ListView) view.findViewById(R.id.pop_listview_left);  
  4.         rightLV = (ListView) view.findViewById(R.id.pop_listview_right);  
  5.   
  6.         popupWindow.setContentView(view);  
  7.         popupWindow.setBackgroundDrawable(new PaintDrawable());  
  8.         popupWindow.setFocusable(true);  
  9.   
  10.         popupWindow.setHeight(ScreenUtils.getScreenH(this) * 2 / 3);  
  11.         popupWindow.setWidth(ScreenUtils.getScreenW(this));  
  12.   
  13.         popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {  
  14.             @Override  
  15.             public void onDismiss() {  
  16.                 darkView.startAnimation(animOut);  
  17.                 darkView.setVisibility(View.GONE);  
  18.   
  19.                 leftLV.setSelection(0);  
  20.                 rightLV.setSelection(0);  
  21.             }  
  22.         });  

2.左侧菜单点击事件:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //左侧ListView点击事件  
  2.         leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  3.             @Override  
  4.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  5.                 //二级数据  
  6.                 List<SecondClassItem> list2 = firstList.get(position).getSecondList();  
  7.                 //如果没有二级类,则直接跳转  
  8.                 if (list2 == null || list2.size() == 0) {  
  9.                     popupWindow.dismiss();  
  10.   
  11.                     int firstId = firstList.get(position).getId();  
  12.                     String selectedName = firstList.get(position).getName();  
  13.                     handleResult(firstId, -1, selectedName);  
  14.                     return;  
  15.                 }  
  16.   
  17.                 FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter());  
  18.                 //如果上次点击的就是这一个item,则不进行任何操作  
  19.                 if (adapter.getSelectedPosition() == position){  
  20.                     return;  
  21.                 }  
  22.   
  23.                 //根据左侧一级分类选中情况,更新背景色  
  24.                 adapter.setSelectedPosition(position);  
  25.                 adapter.notifyDataSetChanged();  
  26.   
  27.                 //显示右侧二级分类  
  28.                 updateSecondListView(list2, secondAdapter);  
  29.             }  
  30.         });  

3. 右侧菜单点击事件:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //右侧ListView点击事件  
  2.         rightLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  3.             @Override  
  4.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  5.                 //关闭popupWindow,显示用户选择的分类  
  6.                 popupWindow.dismiss();  
  7.   
  8.                 int firstPosition = firstAdapter.getSelectedPosition();  
  9.                 int firstId = firstList.get(firstPosition).getId();  
  10.                 int secondId = firstList.get(firstPosition).getSecondList().get(position).getId();  
  11.                 String selectedName = firstList.get(firstPosition).getSecondList().get(position)  
  12.                         .getName();  
  13.                 handleResult(firstId, secondId, selectedName);  
  14.             }  
  15.         });  

4.顶部标签点击事件(即显示/隐藏 分类菜单)

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (popupWindow.isShowing()) {  
  2.            popupWindow.dismiss();  
  3.        } else {  
  4.            popupWindow.showAsDropDown(findViewById(R.id.main_div_line));  
  5.            popupWindow.setAnimationStyle(-1);  
  6.            //背景变暗  
  7.            darkView.startAnimation(animIn);  
  8.            darkView.setVisibility(View.VISIBLE);  
  9.        }  

5.根据左侧点击,刷新右侧ListView

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //刷新右侧ListView  
  2.     private void updateSecondListView(List<SecondClassItem> list2,  
  3.                                       SecondClassAdapter secondAdapter) {  
  4.         secondList.clear();  
  5.         secondList.addAll(list2);  
  6.         secondAdapter.notifyDataSetChanged();  
  7.     }  

源码下载(免积分哦):

http://download.csdn.net/detail/books1958/7992863       

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值