Android编程实现仿美团或淘宝的多级分类菜单效果示例【附demo源码下载】
作者:books1958 时间:2022-09-09 01:38:50
本文实例讲述了Android编程实现仿美团或淘宝的多级分类菜单效果。分享给大家供大家参考,具体如下:
这里要实现的是诸如美团/淘宝/百度糯米 多级分类菜单效果。当分类数量非常多时可以考虑采用两级分类,而诸如美团这种表现方式是一个不错的选择。
首先上效果图:
主要代码:
1. PopupWindow初始化过程:
popupWindow = new PopupWindow(this);
View view = LayoutInflater.from(this).inflate(R.layout.popup_layout, null);
leftLV = (ListView) view.findViewById(R.id.pop_listview_left);
rightLV = (ListView) view.findViewById(R.id.pop_listview_right);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(new PaintDrawable());
popupWindow.setFocusable(true);
popupWindow.setHeight(ScreenUtils.getScreenH(this) * 2 / 3);
popupWindow.setWidth(ScreenUtils.getScreenW(this));
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
darkView.startAnimation(animOut);
darkView.setVisibility(View.GONE);
leftLV.setSelection(0);
rightLV.setSelection(0);
}
});
2.左侧菜单点击事件:
//左侧ListView点击事件
leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//二级数据
List<SecondClassItem> list2 = firstList.get(position).getSecondList();
//如果没有二级类,则直接跳转
if (list2 == null || list2.size() == 0) {
popupWindow.dismiss();
int firstId = firstList.get(position).getId();
String selectedName = firstList.get(position).getName();
handleResult(firstId, -1, selectedName);
return;
}
FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter());
//如果上次点击的就是这一个item,则不进行任何操作
if (adapter.getSelectedPosition() == position){
return;
}
//根据左侧一级分类选中情况,更新背景色
adapter.setSelectedPosition(position);
adapter.notifyDataSetChanged();
//显示右侧二级分类
updateSecondListView(list2, secondAdapter);
}
});
3. 右侧菜单点击事件:
//右侧ListView点击事件
rightLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//关闭popupWindow,显示用户选择的分类
popupWindow.dismiss();
int firstPosition = firstAdapter.getSelectedPosition();
int firstId = firstList.get(firstPosition).getId();
int secondId = firstList.get(firstPosition).getSecondList().get(position).getId();
String selectedName = firstList.get(firstPosition).getSecondList().get(position)
.getName();
handleResult(firstId, secondId, selectedName);
}
});
4.顶部标签点击事件(即显示/隐藏 分类菜单)
if (popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
popupWindow.showAsDropDown(findViewById(R.id.main_div_line));
popupWindow.setAnimationStyle(-1);
//背景变暗
darkView.startAnimation(animIn);
darkView.setVisibility(View.VISIBLE);
}
5.根据左侧点击,刷新右侧ListView
//刷新右侧ListView
private void updateSecondListView(List<SecondClassItem> list2,
SecondClassAdapter secondAdapter) {
secondList.clear();
secondList.addAll(list2);
secondAdapter.notifyDataSetChanged();
}
完整实例代码点击此处本站下载。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,菜单
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何用.NETCore操作RabbitMQ
2022-06-20 04:11:04
![](https://img.aspxhome.com/file/2023/8/76378_0s.png)
解析如何在android中增加gsensor驱动(MMA7660)
2023-05-22 22:03:24
Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码
2023-11-23 08:23:43
![](https://img.aspxhome.com/file/2023/5/59865_0s.png)
SpringBoot整合Druid数据源过程详解
2023-06-03 19:47:14
![](https://img.aspxhome.com/file/2023/3/103973_0s.png)
Springboot 整合shiro实现权限控制的方法
2021-09-21 20:15:47
C#进程监控方法实例分析
2021-09-16 03:25:47
最优雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企业级应用(附源码)
2023-09-26 18:27:57
![](https://img.aspxhome.com/file/2023/2/77112_0s.png)
SpringBoot实现本地文件存储及预览过程
2022-07-31 05:30:58
![](https://img.aspxhome.com/file/2023/6/101996_0s.jpg)
详解SpringBoot JPA常用注解的使用方法
2023-12-09 17:10:31
![](https://img.aspxhome.com/file/2023/5/72535_0s.png)
c# this关键字用法代码详解
2022-06-07 15:22:09
android开发教程之android的handler使用方法
2022-04-29 08:25:43
![](https://img.aspxhome.com/file/2023/4/130434_0s.jpg)
Android自定义控件实现UC浏览器语音搜索效果
2022-06-01 06:59:40
![](https://img.aspxhome.com/file/2023/6/117146_0s.jpg)
C#集合之集(set)的用法
2023-05-18 06:04:53
IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解
2022-02-28 09:44:47
![](https://img.aspxhome.com/file/2023/0/81190_0s.png)
C#用Activex实现Web客户端读取RFID功能的代码
2021-10-28 21:07:36
![](https://img.aspxhome.com/file/2023/3/71733_0s.jpg)
Spring的Aware接口你知道多少
2023-01-19 12:44:20
![](https://img.aspxhome.com/file/2023/9/126349_0s.jpg)
Spring MVC学习教程之视图深入解析
2021-12-16 23:37:55
SpringMVC配置多个properties文件之通配符解析
2021-10-18 02:19:02
![](https://img.aspxhome.com/file/2023/8/81898_0s.png)
Java基础之关键字final详解
2022-01-28 00:55:31
Java实现的Base64加密算法示例
2023-10-29 00:37:08
![](https://img.aspxhome.com/file/2023/8/58618_0s.jpg)