一、概述
ListView点击item显示菜单是要实现这样的效果:
需要实现的逻辑如下:
1)点击一个普通item,展开当前菜单,同时关闭其他菜单
2)点击一个已展开的菜单,隐藏当前菜单
3)将展开菜单滑到listview之外,再滑动回来,展开菜单状态不变
4)点击菜单中的按钮,能够根据不同item进行不同的处理
二、实现思路
1、UI布局上,对于这种每个listitem都包含动态显示菜单的场景,可以直接在listitem的xml布局里就包含两部分元素:item本身以及展开菜单
点击item的时候,动态控制展开菜单这部分元素的Visibility就可以了
2、逻辑控制上,需要额外记录当前展开菜单的item是谁,这样可以方便高效的实现概述中描述的逻辑
三、开始干活
实现的代码结构不复杂,这里直接贴代码了:
listview_menu.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
android:id="@+id/listview_menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" />
listview_menu_item.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@